Skip to content

Commit b7b8ff2

Browse files
Added new solutions with tests
1 parent 1a9615b commit b7b8ff2

File tree

7 files changed

+192
-9
lines changed

7 files changed

+192
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
11
// LeetCode: https://leetcode.com/problems/reverse-integer/description/
2+
23
import Foundation
4+
import XCTest
35

46
class Solution {
57
func reverse(_ x: Int) -> Int {
68
var temp = abs(x)
79
var output = 0
810
while temp > 0 {
9-
var tail = temp % 10
11+
output = output * 10 + temp % 10
1012
temp = temp / 10
11-
output = output * 10 + tail
12-
if (output > Int32.max || output < Int32.min) { return 0 }
1313
}
14-
15-
return output * x.signum() // Times back the sign of the original number
14+
guard output < Int32.max, output > Int32.min else { return 0 }
15+
return output * x.signum()
1616
}
1717

1818
func mySqrt(_ x: Int) -> Int {
1919
return Int(sqrt(Double(x)))
20-
2120
}
2221
}
2322

24-
let solution = Solution()
25-
print("\(solution.reverse(-123))")
26-
print("\(solution.mySqrt(8))")
23+
class Tests: XCTestCase {
24+
let solution = Solution()
25+
26+
func testSample1() {
27+
let input = -123
28+
let expected = -321
29+
XCTAssertEqual(solution.reverse(input), expected)
30+
}
31+
32+
func testSample2() {
33+
let input = 123
34+
let expected = 321
35+
XCTAssertEqual(solution.reverse(input), expected)
36+
}
37+
38+
func testSample3() {
39+
let input = 120
40+
let expected = 21
41+
XCTAssertEqual(solution.reverse(input), expected)
42+
}
43+
44+
func testSample4() {
45+
let input = 1534236469
46+
let expected = 0
47+
XCTAssertEqual(solution.reverse(input), expected)
48+
}
49+
}
50+
51+
Tests.defaultTestSuite.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// LeetCode: https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
2+
3+
class Solution {
4+
func letterCombinations(_ digits: String) -> [String] {
5+
var output: [String] = []
6+
var dict = [
7+
"2": "abc",
8+
"3": "def",
9+
"4": "ghi",
10+
"5": "jkl",
11+
"6": "mno",
12+
"7": "pqrs",
13+
"8": "tuv",
14+
"9": "wxyz",
15+
]
16+
for digit in digits {
17+
if let str = dict[String(digit)] {
18+
appendStr(output: &output, toAppend: str)
19+
}
20+
}
21+
return output
22+
}
23+
24+
func appendStr(output: inout [String], toAppend: String) {
25+
if output.count == 0 {
26+
for str in toAppend {
27+
output.append(String(str))
28+
}
29+
return
30+
}
31+
let temp = output
32+
output = []
33+
for str in toAppend {
34+
var currentOut = temp
35+
for index in 0..<currentOut.count {
36+
currentOut[index].append(str)
37+
output.append(currentOut[index])
38+
}
39+
}
40+
}
41+
}
42+
43+
let solution = Solution()
44+
print(solution.letterCombinations("23")) // ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// LeetCode: https://leetcode.com/problems/product-of-array-except-self/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func productExceptSelf(_ nums: [Int]) -> [Int] {
7+
var zeroCount = 0
8+
var sum = 1
9+
for i in nums {
10+
if i != 0 {
11+
sum = sum * i
12+
} else {
13+
zeroCount += 1
14+
}
15+
}
16+
if zeroCount > 1 {
17+
return nums.map { _ in 0 }
18+
} else if zeroCount == 1 {
19+
return nums.map { $0 == 0 ? sum : 0 }
20+
} else {
21+
return nums.map { sum / $0 }
22+
}
23+
}
24+
}
25+
26+
class Tests: XCTestCase {
27+
let solution = Solution()
28+
29+
func testSample1() {
30+
let input = [1, 2, 3, 4]
31+
let expected = [24, 12, 8, 6]
32+
XCTAssertEqual(solution.productExceptSelf(input), expected)
33+
}
34+
35+
func testSample2() {
36+
let input = [0, 0]
37+
let expected = [0, 0]
38+
XCTAssertEqual(solution.productExceptSelf(input), expected)
39+
}
40+
}
41+
42+
Tests.defaultTestSuite.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Swift GCD
2+
3+
// Assign to var or constant
4+
let f = { (x: Int) -> Int
5+
in
6+
return x + 42
7+
}
8+
9+
f(9)
10+
f(76)
11+
12+
// Closures in an array (or a dictionary, or a set, etc...)
13+
let closures = [
14+
f,
15+
{(x: Int) -> Int in return x * 2},
16+
{x in return x - 8},
17+
{x in x*x},
18+
{$0 * 42}
19+
]
20+
// $0 means position zero
21+
22+
for fn in closures {
23+
fn(42)
24+
}
25+
26+
// Functions and closures are exactly the same thing
27+
func foo(x: Int) -> Int {
28+
return 42 + x
29+
}
30+
31+
let foo = {(x: Int) -> Int
32+
in
33+
42 + x
34+
}
35+
36+
foo(30)
37+
38+
// MARK: - Typealias
39+
// Standard form: typealias newName = oldName
40+
typealias Integer = Int
41+
42+
// Useful when used with functions and closures
43+
// (Int) -> Int
44+
typealias IntToInt = (Int) -> Int
45+
typealias IntMaker = () -> Int
46+
47+
func makeCounter() -> IntMaker {
48+
var n = 0
49+
50+
func adder() -> Int {
51+
n = n + 1
52+
return n
53+
}
54+
55+
return adder
56+
}
57+
58+
let counter1 = makeCounter()
59+
counter1()
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)