How to read values of SparseMatrix_Double in Swift 4? - swift

How do I write a function for printing out the contents of SparseMatrix_Double for Swift 4?
I have the following code so far, however, the output is only correct some of the time.
Sometimes the array is accessing memory that's out of range giving [[1.06540896337e-313, 0.0], [0.0, 3.0]]
I suspect that the way I obtain the number of nonzeros is wrong.
import Accelerate
func toString(_ A: SparseMatrix_Double) throws -> String {
if A.structure.rowCount > 100 || A.structure.columnCount > 100 {
print("Matrix is too big to display")
throw NSError(domain: "Matrix is too big to display", code: -1, userInfo: nil)
}
let rows = Int(A.structure.rowCount)
let columns = Int(A.structure.columnCount)
let nonzeros = A.structure.columnStarts[columns]
print("Row indices")
for i in 0..<nonzeros {
print("\(i): \(A.structure.rowIndices[i])")
}
print("Column starts")
for c in 0...columns {
print("\(c): \(A.structure.columnStarts[c])")
}
var M = Array(repeating: Array(repeating: 0.0, count: columns), count: rows)
var i = 0
var currentColumn: Int = 0
var nextColStarts = A.structure.columnStarts[1]
while currentColumn < (columns - 1) {
if i == nextColStarts {
currentColumn += 1
nextColStarts = A.structure.columnStarts[currentColumn + 1]
}
let rowIndex = Int(A.structure.rowIndices[i])
M[rowIndex][currentColumn] = A.data[i]
print("Setting \(rowIndex),\(currentColumn) [\(i)]")
i += 1
}
return M.description
}
var rows: [Int32] = [1]
var columns: [Int32] = [1]
var values: [Double] = [3.0]
let blockSize: UInt8 = 1
let blockCount = 8
let A = SparseConvertFromCoordinate(
2, 2,
blockCount, blockSize,
SparseAttributes_t(),
&rows, &columns,
&values
)
print(try? toString(A))

Related

List all processes with arguments in Swift

I'm trying to convert some piece of code to Swift that will list all running processes. But since it requires calling some C specific APIs I'm a bit struggling.
Can someone tell me here what am I doing here incorrectly? print statement at the end is outputting incorrect values. I assume it should be process name. Also line info = malloc(length) gives me the creeps. How should I properly allocate it?
var maxArgumentsSize: Int = 0
if maxArgumentsSize == 0 {
var size: size_t = MemoryLayout<Int>.size
var mib: [Int32] = [CTL_KERN, KERN_ARGMAX]
let a = sysctl(&mib, 2, &maxArgumentsSize, &size, nil, 0)
if a == -1 {
maxArgumentsSize = 4096;
}
}
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL]
var info: UnsafeMutableRawPointer? = nil
var length: size_t = 0
var count: Int = 0
if sysctl(&mib, 3, nil, &length, nil, 0) < 0 {
exit(EXIT_FAILURE)
}
info = malloc(length)
if sysctl(&mib, 3, info, &length, nil, 0) < 0 {
free(info)
exit(EXIT_FAILURE)
}
count = length / MemoryLayout<kinfo_proc>.size
for index in 0...count {
let info = info?.assumingMemoryBound(to: kinfo_proc.self)
let pid: pid_t = info![index].kp_proc.p_pid
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: maxArgumentsSize)
var mib: [Int32] = [CTL_KERN, KERN_PROCARGS2, pid]
if sysctl(&mib, 3, buffer, &maxArgumentsSize, nil, 0) == 0 {
let str = String(cString: buffer, encoding: .utf8)
print(str)
}
free(buffer);
}
Basically I've changed my initial code to this and calling #MartinR solution (https://stackoverflow.com/a/72445381/1187415) at the end. Of course it's not complete and pasted from my code directly but it's working.
// Get all processess information:
var name: [CInt] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL]
var length: size_t = 0
if sysctl(&name, CUnsignedInt(name.count), nil, &length, nil, 0) < 0 {
return
}
var infoPtr = UnsafeMutableRawPointer.allocate(
byteCount: length,
alignment: MemoryLayout<kinfo_proc>.alignment
)
if sysctl(&name, CUnsignedInt(name.count), infoPtr, &length, nil, 0) < 0 {
infoPtr.deallocate()
return
}
let count = length / MemoryLayout<kinfo_proc>.size
for index in 0...count {
let info = infoPtr.assumingMemoryBound(to: kinfo_proc.self)
let pid: pid_t = info[index].kp_proc.p_pid
let arguments = self.processArguments(pid: pid)
}
infoPtr.deallocate()

Karatsuba multiplication in swift

I was trying to implement Karatsuba multiplication in swift. I wrote the below code and it is working fine for some smaller numbers but as the number gets bigger this code fails to give the correct answer. I have debugged in every possible way I can but could not find the bug. Algorithm wise I think I did correctly write the code. And the code is working fine for smaller numbers. But the final answer is wrong for bigger numbers. If anyone out there can crack down the mistake I'm making, pls do help me
func findMultiplication(x: String, y: String) -> String {
if isZero(str: x) || isZero(str: y) {
return "0"
}
var x = removeLeadingZeros(number: x)
var y = removeLeadingZeros(number: y)
if x.count < 2 || y.count < 2 {
let result = Int(x)!*Int(y)!
return String(result)
}
var middleIndexX: String.Index
var middleIndexY: String.Index
var middleIndex: Int
if x.count >= y.count {
y = addLeftPaddingZeros(numberOfZeros: x.count-y.count, for: y)
middleIndex = x.count / 2
if x.count % 2 != 0 {
middleIndex += 1
}
} else {
x = addLeftPaddingZeros(numberOfZeros: y.count-x.count, for: x)
middleIndex = y.count / 2
if y.count % 2 != 0 {
middleIndex += 1
}
}
middleIndexX = x.index(x.startIndex, offsetBy: middleIndex)
middleIndexY = y.index(y.startIndex, offsetBy: middleIndex)
let a = String(x[x.startIndex..<middleIndexX])
let b = String(x[middleIndexX..<x.endIndex])
let c = String(y[y.startIndex..<middleIndexY])
let d = String(y[middleIndexY..<y.endIndex])
let ac = findMultiplication(x: a, y: c)
let bd = findMultiplication(x: b, y: d)
let aPb = Int(a)! + Int(b)!
let cPd = Int(c)! + Int(d)!
let gauss = findMultiplication(x: String(aPb), y: String(cPd))
let thirdItem = String(Int(gauss)! - Int(ac)! - Int(bd)!)
var returnSum = 0
returnSum += Int(addLeftPaddingZeros(numberOfZeros: x.count, for: ac, isLeft: false)) ?? 0
returnSum += Int(addLeftPaddingZeros(numberOfZeros: middleIndex, for: thirdItem, isLeft: false)) ?? 0
returnSum += Int(bd) ?? 0
return String(returnSum)
}
print(findMultiplication(x: "123400", y: "123711"))
func removeLeadingZeros(number: String) -> String {
var number = number
while number.first == "0" {
number.removeFirst()
}
if number == "" {
return "0"
}
return number
}
//The function name is given like this only. BUt his will help to add padding zero in left and right also
func addLeftPaddingZeros(numberOfZeros: Int, for str: String, isLeft: Bool = true) -> String {
var padding = ""
for _ in 0 ..< numberOfZeros {
padding += "0"
}
if isLeft {
return padding+str
} else {
return str + padding
}
}
func isZero(str: String) -> Bool {
for char in str {
if char != "0" {
return false
}
}
return true
}

LeetCode 299. Bulls and Cows, Swift Code

The OJ page is: 299. Bulls and Cows, Swift Code
My Swift Code is:
func getHint(secret: String, _ guess: String) -> String
{
let count = secret.characters.count
var ans = ""
var countA = 0
var countB = 0
var numsCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in 0 ..< count
{
let curSecretIndex = secret.startIndex.advancedBy(i)
let curGuessIndex = guess.startIndex.advancedBy(i)
if secret[curSecretIndex] == guess[curGuessIndex]
{
countA += 1
}
else
{
let curSecretNum = Int(String(secret[curSecretIndex]))!
let curGuessNum = Int(String(guess[curGuessIndex]))!
if numsCount[curSecretNum] > 0
{
countB += 1
}
if numsCount[curGuessNum] < 0
{
countB += 1
}
numsCount[curSecretNum] -= 1
numsCount[curGuessNum] += 1
}
}
ans = String(countA) + "A" + String(countB) + "B"
return ans
}
The result it 'Time Limit Exceeded' when running the case:
"6342125515600209181500897947396070342608717883958593622428977819470145518981094482423670643602640743135109789842055897996388630146186752661167826378847934464616412304716111808304498782005822162883686211337836911445498225004774354586168838560300965168000411392051373086314099651372076489284613220040070817961103856849197569739439674339914883676284322398392068700339678002599137137279304395401279115346633764844174685348142841166612675248803215000249557405129671377750613405565000541484366826871257668988459106913268432182614110919996681746630972155917317871065083728781479655332598828637865325616648485574880796687189161689539391392553041342138974604486863793131125744568750189486989526831390549186801009323526430712299903383659261758477604285513561656265905238724724774327396452598472436892619082685728038313372432807929513713314602774582152430611189205157910570001968051407417723280898588867721259234562110839321097595400391525102339288526258798825449826942020614695348904788907661932993430488593552", "2029388157754123013579930824032835962698439709529058700566544658243563588105123607416485416240508396898633255420749620114578170997813662130792145648053315140036623409978307287859342924657549449161362779317296010762442132229389529024391729227555488965589257218766928737602934082420388840064521623805135789781907460246852282793026912370042044884703394961008247101686942091465646505425818246546319109188403876239911011831539311377016115552962442767907020896812732576449580620680669604714802174904961265522985701150408238410077557827782193833638029934745703495039208558090425795228240968930462177363142995202879750991368837565022531005343218914976811148294727154364844319794156224312278949985742616475018653587238917107571280831446981869075410523704462504535667036707669945712843129399060874345018414837070546836481691874559666339572723901027743847187340764130312322743860946054990407323560232897592869469337005471407593834874319157599015450827399558773751402417232829362967857998884017081049757447739946"
How can I improve the performance?
Anyone help? THANKS!
Not sure how did you get the 'Time Limit Exceeded'. Try this in playground:
import Foundation
func bowsAndBulls (guess: String, secret: String) -> String {
if guess.characters.count != secret.characters.count {
return "ERROR"
}
//use counter variables works too
var countA = [String]()
var countB = [String]()
for i in 0..<guess.characters.count {
let start = guess.startIndex.advancedBy(i)
let end = guess.startIndex.advancedBy(i+1)
let single = guess.substringWithRange(start..<end)
if secret.containsString(single) {
if secret.substringWithRange(start..<end) == single {
countA.append(single)
}else{
countB.append(single)
}
}
}
return String(countA.count) + "A" + String(countB.count) + "B"
}
print(bowsAndBulls("5234", secret: "5346"))

What's the best way to cut Swift string into 2-letter-strings?

I need to split a string into 2-letter pieces. Like “friend" -> "fr" "ie" "nd". (Okay, its a step for me to change HEX string to Uint8 Array)
My code is
for i=0; i<chars.count/2; i++ {
let str = input[input.startIndex.advancedBy(i*2)..<input.startIndex.advancedBy(i*2+1)]
bytes.append(UInt8(str,radix: 16)!)
}
But I don't know why I cannot use Range to do this split. And I have no idea what will happen when i*2+1 is bigger than string's length. So what's the best way to cut Swift string into 2-letter-strings?
Your range wasn't working because you need to use ... instead of ..<.
let input = "ff103"
var bytes = [UInt8]()
let strlen = input.characters.count
for i in 0 ..< (strlen + 1)/2 {
let str = input[input.startIndex.advancedBy(i*2)...input.startIndex.advancedBy(min(strlen - 1, i*2+1))]
bytes.append(UInt8(str,radix: 16) ?? 0)
}
print(bytes) // [255, 16, 3]
Here is another take on splitting the string into 2-letter strings. advancedBy() is an expensive O(n) operation, so this version keeps track of start and just marches it ahead by 2 each loop, and end is based on start:
let input = "friends"
var strings = [String]()
let strlen = input.characters.count
var start = input.startIndex
let lastIndex = strlen > 0 ? input.endIndex.predecessor() : input.startIndex
for i in 0 ..< (strlen + 1)/2 {
start = i > 0 ? start.advancedBy(2) : start
let end = start < lastIndex ? start.successor() : start
let str = input[start...end]
strings.append(str)
}
print(strings) // ["fr", "ie", "nd", "s"]
Alternate Answer:
Using ranges is probably overkill. It is easy just to add the characters to an array and make Strings from those:
let input = "friends"
var strings = [String]()
var newchars = [Character]()
for c in input.characters {
newchars.append(c)
if newchars.count == 2 {
strings.append(String(newchars))
newchars = []
}
}
if newchars.count > 0 {
strings.append(String(newchars))
}
print(strings) // ["fr", "ie", "nd", "s"]
And here is the new version for making [UInt8]:
let input = "ff103"
var bytes = [UInt8]()
var newchars = [Character]()
for c in input.characters {
newchars.append(c)
if newchars.count == 2 {
bytes.append(UInt8(String(newchars), radix: 16) ?? 0)
newchars = []
}
}
if newchars.count > 0 {
bytes.append(UInt8(String(newchars), radix: 16) ?? 0)
}
print(bytes) // [255, 16, 3]
Based on #LeoDabus' answer, we can make an extension with a method that will return substrings of any length, and a computed property that returns [UInt8]:
extension String {
func substringsOfLength(length: Int) -> [String] {
if length < 1 { return [] }
var result:[String] = []
let chars = Array(characters)
for index in 0.stride(to: chars.count, by: length) {
result.append(String(chars[index ..< min(index+length, chars.count)]))
}
return result
}
var toUInt8: [UInt8] {
var result:[UInt8] = []
let chars = Array(characters)
for index in 0.stride(to: chars.count, by: 2) {
let str = String(chars[index ..< min(index+2, chars.count)])
result.append(UInt8(str, radix: 16) ?? 0)
}
return result
}
}
let input = "friends"
let str2 = input.substringsOfLength(2) // ["fr", "ie", "nd", "s"]
let str0 = input.substringsOfLength(0) // []
let str3 = input.substringsOfLength(3) // ["fri", "end", "s"]
let bytes = "ff107".toUInt8 // [255, 16, 7]
Another option just for fun:
extension String {
var pairs:[String] {
var result:[String] = []
let chars = Array(characters)
for index in 0.stride(to: chars.count, by: 2) {
result.append(String(chars[index..<min(index+2, chars.count)]))
}
return result
}
}
let input = "friends"
let pairs = input.pairs
print(pairs) // ["fr", "ie", "nd", "s"]

How to split an Int to its individual digits?

I am trying to split an Int into its individual digits, e.g. 3489 to 3 4 8 9, and then I want to put the digits in an Int array.
I have already tried putting the number into a string and then iterating over each digit, but it doesn't work:
var number = "123456"
var array = [Int]()
for digit in number {
array.append(digit)
}
Any ideas?
We can also extend the StringProtocol and create a computed property:
edit/update: Xcode 11.5 • Swift 5.2
extension StringProtocol {
var digits: [Int] { compactMap(\.wholeNumberValue) }
}
let string = "123456"
let digits = string.digits // [1, 2, 3, 4, 5, 6]
extension LosslessStringConvertible {
var string: String { .init(self) }
}
extension Numeric where Self: LosslessStringConvertible {
var digits: [Int] { string.digits }
}
let integer = 123
let integerDigits = integer.digits // [1, 2, 3]
let double = 12.34
let doubleDigits = double.digits // // [1, 2, 3, 4]
In Swift 5 now we can use the new Character property wholeNumberValue
let string = "123456"
let digits = string.compactMap{ $0.wholeNumberValue } // [1, 2, 3, 4, 5, 6]
A solution without having to convert the int to string....
Example
1234%10 = 4 <-
1234/10 = 123
123%10 = 3 <-
123/10 = 12
12%10 = 2 <-
12/10 = 1
1%10 = 1 <-
var num = 12345
var arrayInt = [Int]()
arrayInt.append(num%10)
while num >= 10 {
num = num/10
arrayInt.insert(num%10, at: 0)
}
Swift 5
extension Int {
func digits() -> [Int] {
var digits: [Int] = []
var num = self
digits.append(num % 10)
while num >= 10 {
num = num / 10
digits.append(num % 10)
}
return digits.reversed()
}
}
You can try this:
var number = "123456"
var array = number.utf8.map{Int($0)-48}
You can make use of the utf8 property of String to directly access the ASCII value of the characters in the String representation of your number.
this code works:
var number = "123456"
var array = number.utf8.map{Int(($0 as UInt8)) - 48}
this might be slower:
var array = number.characters.map{Int(String($0))!}
and if your number is less or equal than Int.max which is 9223372036854775807 here is the fastest variant (and if your number>Int.max you can split your long string that represents your number into 18-digit groups to make this variant work on large strings)
var array2 = [Int]()
var i = Int(number)!
while i > 0 {array2.append(i%10); i/=10}
array2 = array2.reverse()
You can use this
extension Int {
func numberOfDigits() -> Int {
if abs(self) < 10 {
return 1
} else {
return 1 + (self/10).numberOfDigits()
}
}
func getDigits() -> [Int] {
let num = self.numberOfDigits()
var tempNumber = self
var digitList = [Int]()
for i in (0..<num).reversed() {
let divider = Int(pow(CGFloat(10), CGFloat(i)))
let digit = tempNumber/divider
digitList.append(digit)
tempNumber -= digit*divider
}
return digitList
}
}
if you need convert integer to Array
let num = 12345
let array = String(num).compactMap({$0.wholeNumberValue})
print("\(array), \(type(of: array))")
//[1,2,3,4,5], Array<Int>
extension Int {
var digits : [Int] {
var result = [Int]()
var remaining = abs(self)
while remaining > 0 {
result.insert(remaining % 10, at: 0)
remaining /= 10
}
return result
}
}
You can use this:
// input -> "123456"
// output -> [1, 2, 3, 4, 5, 6]
// Get the string, convert it in an Array(),
// use a loop (convert i in Int()), to add it into a container, then return it. Done!
func getDigitsFromString(for string: String) -> [Int]{
let stringInt = Array(string)
var array = [Int]()
for i in stringInt {
if let i = Int(String(i)) {
array.append(i)
}
}
return array
}
There is a simpler method to achieve this. Say x = 12345 , then
var s = String(x);
var numArray = Array(s);
clean, easy-to-read code.
You can use:
let number = -123
let regex = /-?\d/
let values = String(number).matches(of: regex).compactMap { Int($0.output) }
print(values) // [-1, 2, 3]
This code takes into account negative values