Skip to content

Instantly share code, notes, and snippets.

@DanielAsher
Created January 18, 2020 09:51
Show Gist options
  • Select an option

  • Save DanielAsher/c3225e455208249ecfed323f6d61b591 to your computer and use it in GitHub Desktop.

Select an option

Save DanielAsher/c3225e455208249ecfed323f6d61b591 to your computer and use it in GitHub Desktop.
Saying hi in swift
func sayHi() -> String {
return "Hello Dubi"
}
@kelmerdu
Copy link

import Foundation
import Glibc

struct Array2D : CustomStringConvertible {
var numberRows: Int
var numberCols: Int
var matrix: [Int]
// var grid: String

init(numberRows: Int, numberCols: Int) {
self.numberRows = numberRows
self.numberCols = numberCols
matrix = Array(repeating: 0, count: numberRows * numberCols)
/* for d in 1...numberRows {
grid += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}
*/
}

func index(row: Int, col: Int) -> Int {
return (numberCols * (row - 1) + (col - 1))
}

func indexIsValid(row: Int, col: Int) -> Bool {
if col > numberCols || row > numberRows {
return false
} else {
return true
}
}

/* set(row: Int, col: Int, newValue) {
{
if indexIsValid(row: row, col: col) {
matrix[(row * numberCols) + col] = newValue
} else {
print("index is out of bounds")
exit(0)
}
}
}

}

get(row: Int, col: Int) -> Int {
return data[(index(row: row, col: col))]
}
*/
subscript(row: Int, col: Int) -> Int {
get {
if indexIsValid(row: row, col: col) {
return matrix[(row * numberCols) + col]
} else {
print("inxex is out of bounds")
exit(0)
}
}
set(newValue) {
if indexIsValid(row: row, col: col) {
matrix[(row * numberCols) + col] = newValue
} else {
print("inxex is out of bounds")
exit(0)
}
}
}
var description: String {
var grid: String
for d in 1...numberRows {
grid += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}
return grid
}

@kelmerdu
Copy link

We have two unrelated problems:

  1. while the get and set inside subscript work fine they do not work on their own so I guess we are not using the correct syntax
  2. For the custom convertible description we want it to be the 2D array however, when we try to set it up we run into a problem that we need to use the variable matrix (where the array data is stored) but it is not initialized until runtime so we get an error. Not sure how to correct it.

@kelmerdu
Copy link

import Foundation

import Foundation
import Glibc

struct Array2D: CustomStringConvertible {
var numberRows: Int
var numberCols: Int
var matrix: [Int]
var description: String = ""

init(numberRows: Int, numberCols: Int) {
self.numberRows = numberRows
self.numberCols = numberCols
matrix = Array(repeating: 0, count: numberRows * numberCols)
for d in 1...numberRows {
description += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}
}

func index(row: Int, col: Int) -> Int {
return (numberCols * (row - 1) + (col - 1))
}

func indexIsValid(row: Int, col: Int) -> Bool {
if col > numberCols || row > numberRows {
return false
} else {
return true
}
}

/* set(row: Int, col: Int, newValue) {
{
if indexIsValid(row: row, col: col) {
matrix[(row * numberCols) + col] = newValue
} else {
print("index is out of bounds")
exit(0)
}
}
}

}
get(row: Int, col: Int) -> Int {
return data[(index(row: row, col: col))]
}
*/

subscript(row: Int, col: Int) -> Int {
get {
if indexIsValid(row: row, col: col) {
return matrix[index(row: row, col:col)]
} else {
print("inxex is out of bounds")
exit(0)
}
}
set(newValue) {
if indexIsValid(row: row, col: col) {
matrix[index(row: row, col: col)] = newValue
description = ""
for d in 1...numberRows {
description += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}

} else {
print("index is out of bounds")
exit(0)
}
}
}

}

@kelmerdu
Copy link

Managed to solve the problem of the custom string convertible (in a very non efficient way I am sure).
So now the code is working and doing what it should. Still not sure how to implement the get and set not inside subscript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment