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
Lines changed: 34 additions & 9 deletions
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
classSolution{
57
func reverse(_ x:Int)->Int{
68
vartemp=abs(x)
79
varoutput=0
810
while temp >0{
9-
vartail= 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){return0}
1313
}
14-
15-
return output * x.signum() // Times back the sign of the original number
14+
guard output <Int32.max, output >Int32.min else{return0}
15+
return output * x.signum()
1616
}
1717

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

24-
letsolution=Solution()
25-
print("\(solution.reverse(-123))")
26-
print("\(solution.mySqrt(8))")
23+
classTests:XCTestCase{
24+
letsolution=Solution()
25+
26+
func testSample1(){
27+
letinput=-123
28+
letexpected=-321
29+
XCTAssertEqual(solution.reverse(input), expected)
30+
}
31+
32+
func testSample2(){
33+
letinput=123
34+
letexpected=321
35+
XCTAssertEqual(solution.reverse(input), expected)
36+
}
37+
38+
func testSample3(){
39+
letinput=120
40+
letexpected=21
41+
XCTAssertEqual(solution.reverse(input), expected)
42+
}
43+
44+
func testSample4(){
45+
letinput=1534236469
46+
letexpected=0
47+
XCTAssertEqual(solution.reverse(input), expected)
48+
}
49+
}
50+
51+
Tests.defaultTestSuite.run()
Lines changed: 44 additions & 0 deletions
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+
classSolution{
4+
func letterCombinations(_ digits:String)->[String]{
5+
varoutput:[String]=[]
6+
vardict=[
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+
fordigitin digits {
17+
iflet 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+
forstrin toAppend {
27+
output.append(String(str))
28+
}
29+
return
30+
}
31+
lettemp= output
32+
output =[]
33+
forstrin toAppend {
34+
varcurrentOut= temp
35+
forindexin0..<currentOut.count {
36+
currentOut[index].append(str)
37+
output.append(currentOut[index])
38+
}
39+
}
40+
}
41+
}
42+
43+
letsolution=Solution()
44+
print(solution.letterCombinations("23")) // ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 42 additions & 0 deletions
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+
classSolution{
6+
func productExceptSelf(_ nums:[Int])->[Int]{
7+
varzeroCount=0
8+
varsum=1
9+
foriin nums {
10+
if i !=0{
11+
sum = sum * i
12+
}else{
13+
zeroCount +=1
14+
}
15+
}
16+
if zeroCount >1{
17+
return nums.map{ _ in0}
18+
}elseif zeroCount ==1{
19+
return nums.map{ $0 ==0? sum :0}
20+
}else{
21+
return nums.map{ sum / $0 }
22+
}
23+
}
24+
}
25+
26+
classTests:XCTestCase{
27+
letsolution=Solution()
28+
29+
func testSample1(){
30+
letinput=[1,2,3,4]
31+
letexpected=[24,12,8,6]
32+
XCTAssertEqual(solution.productExceptSelf(input), expected)
33+
}
34+
35+
func testSample2(){
36+
letinput=[0,0]
37+
letexpected=[0,0]
38+
XCTAssertEqual(solution.productExceptSelf(input), expected)
39+
}
40+
}
41+
42+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playgroundversion='5.0'target-platform='macos'executeOnSourceChanges='false'>
3+
<timelinefileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Swift GCD
2+
3+
// Assign to var or constant
4+
letf={(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+
letclosures=[
14+
f,
15+
{(x:Int)->Intinreturn x *2},
16+
{x inreturn x -8},
17+
{x in x*x},
18+
{$0 *42}
19+
]
20+
// $0 means position zero
21+
22+
forfnin closures {
23+
fn(42)
24+
}
25+
26+
// Functions and closures are exactly the same thing
27+
func foo(x:Int)->Int{
28+
return42+ x
29+
}
30+
31+
letfoo={(x:Int)->Int
32+
in
33+
42+ x
34+
}
35+
36+
foo(30)
37+
38+
// MARK: - Typealias
39+
// Standard form: typealias newName = oldName
40+
typealiasInteger=Int
41+
42+
// Useful when used with functions and closures
43+
// (Int) -> Int
44+
typealiasIntToInt=(Int)->Int
45+
typealiasIntMaker=()->Int
46+
47+
func makeCounter()->IntMaker{
48+
varn=0
49+
50+
func adder()->Int{
51+
n = n +1
52+
return n
53+
}
54+
55+
return adder
56+
}
57+
58+
letcounter1=makeCounter()
59+
counter1()
60+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playgroundversion='5.0'target-platform='macos'>
3+
<timelinefileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
(0)