From 2b5d843d8f4bcf44de3d137d941cfdd4a6f22a07 Mon Sep 17 00:00:00 2001 From: Larry Pham Date: Fri, 11 Nov 2016 17:32:44 +0700 Subject: [PATCH 1/2] Swift Language --- week0002/larrypham/swift/Spiral.swift | 52 +++++++++++++++++++++++++++ week0002/larrypham/swift/main.swift | 14 ++++++++ 2 files changed, 66 insertions(+) create mode 100644 week0002/larrypham/swift/Spiral.swift create mode 100644 week0002/larrypham/swift/main.swift diff --git a/week0002/larrypham/swift/Spiral.swift b/week0002/larrypham/swift/Spiral.swift new file mode 100644 index 0000000..88fa4b8 --- /dev/null +++ b/week0002/larrypham/swift/Spiral.swift @@ -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(repeating: 0, count: n), count: n) + var k = 1, i = 0 + while k <= n * n { + var j = i + while j < n - i { + 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 + } +} + + + diff --git a/week0002/larrypham/swift/main.swift b/week0002/larrypham/swift/main.swift new file mode 100644 index 0000000..eec0a33 --- /dev/null +++ b/week0002/larrypham/swift/main.swift @@ -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.. Date: Fri, 11 Nov 2016 17:44:58 +0700 Subject: [PATCH 2/2] Week1 Swift --- week0001/fizz_buzz_larry.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 week0001/fizz_buzz_larry.swift diff --git a/week0001/fizz_buzz_larry.swift b/week0001/fizz_buzz_larry.swift new file mode 100644 index 0000000..a43478d --- /dev/null +++ b/week0001/fizz_buzz_larry.swift @@ -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() \ No newline at end of file