Add examples on Swift 3.0.2 (#11)
* 01_binary_search Swift 3.0.2 * 01_binary_search Swift 3.0.2 * add Chapter 2 - 01_selection_sort Swift 3.0.2 example * 01_binary_search cosmetic note updates Swift 3.0.2 * 03_recursion Swift 3.0.2 examples * 04_quicksort Swift 3.0.2 examples * fix typo on quicksort example. Swift 3.0.2 * add 05_hash_tables examples on Swift 3.0.2 * add 01_breadth-first_search Swift 3.0.2 example * 01_breadth-first_search fixing typo Swift 3.0.2 * add 01_dijkstras_algorithm on Swift 3.0.2 * add 08_greedy_algorithms Swift 3.0.2 example * 01_longest_common_subsequence Swift 3.0.2 example
This commit is contained in:
committed by
Aditya Bhargava
parent
62ed616954
commit
12265a8c61
16
03_recursion/swift/01_countdown.swift
Normal file
16
03_recursion/swift/01_countdown.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
import Foundation
|
||||
|
||||
func countDown(i : Int) {
|
||||
print(i)
|
||||
// base case
|
||||
if i <= 0 {
|
||||
return
|
||||
} else {
|
||||
// recursive case
|
||||
countDown(i: i-1)
|
||||
}
|
||||
}
|
||||
|
||||
countDown(i: 5)
|
||||
|
||||
|
||||
18
03_recursion/swift/02_greet.swift
Normal file
18
03_recursion/swift/02_greet.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
|
||||
func greet2(name: String) {
|
||||
print("how are you, \(name) ?")
|
||||
}
|
||||
|
||||
func bye() {
|
||||
print("ok bye!")
|
||||
}
|
||||
|
||||
func greet(name: String ) {
|
||||
print("hello, \(name)!")
|
||||
greet2(name: name)
|
||||
print("getting ready to say bye...")
|
||||
bye()
|
||||
}
|
||||
|
||||
greet(name: "adit")
|
||||
12
03_recursion/swift/03_factorial.swift
Normal file
12
03_recursion/swift/03_factorial.swift
Normal file
@@ -0,0 +1,12 @@
|
||||
import Foundation
|
||||
|
||||
func fact(x: Int) -> Int {
|
||||
if x == 1 {
|
||||
return 1
|
||||
} else {
|
||||
return x*fact(x: x-1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
print(fact(x: 5)) // => 120
|
||||
Reference in New Issue
Block a user