Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Language #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions week0001/fizz_buzz_larry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

func printFizzBuzz() {
for var index in 1...100 {
if index % 3 == 0 {
print("Fizz")
}

if index % 5 == 0 {
print("Buzz")
}
}
}

printFizzBuzz()
52 changes: 52 additions & 0 deletions week0002/larrypham/swift/Spiral.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Given an integer N, generate a square matrix filled with elements from 1 to n*n in spiral order
* with n= 4, we have:
* 1 2 3 4
* 12 13 14 5
* 11 16 15 6
* 10 9 8 7
**/
import Foundation

class Solution {

func generateMatrix(n: Int) -> [[Int]] {
var res = Array<[Int]>(repeating: Array<Int>(repeating: 0, count: n), count: n)
var k = 1, i = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give some meaningful names to i, j & k.

while k <= n * n {
var j = i
while j < n - i {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly you are going to the right. Please add a comment before each while loop so people can easily understand your logic.

res[i][j] = k
j += 1
k += 1
}

j = i + 1
while j < n - i {
res[j][n-i-1] = k
j += 1
k += 1
}

j = n - i - 2
while j > i {
res[n-i-1][j] = k
j -= 1
k += 1
}

j = n - i - 1
while j > i {
res[j][i] = k
j -= 1
k += 1
}

i += 1
}
return res
}
}



14 changes: 14 additions & 0 deletions week0002/larrypham/swift/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

let size = Int(readLine()!)!
let solution = Solution()

let result: [[Int]] = solution.generateMatrix(n: size)
for var x in 0..<result.count {
var line = ""
for var y in 0..<result[x].count {
line += String(result[x][y])
line += " "
}
print(line)
}