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
18
05_hash_tables/swift/01_price_of_groceries.swift
Normal file
18
05_hash_tables/swift/01_price_of_groceries.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
|
||||
var book = [String: Double]()
|
||||
// an apple costs 67 cents
|
||||
book["apple"] = 0.67
|
||||
//# milk costs $1.49
|
||||
book["milk"] = 1.49
|
||||
book["avacado"] = 1.49
|
||||
print(book) // => ["avacado": 1.49, "apple": 0.67000000000000004, "milk": 1.49]
|
||||
|
||||
// Qustion: Why is "apple" is 0.67000000000000004 intsted of 0.67?
|
||||
// Answer: Double cannot store the value 0.67 exactly. Swift uses (like many other languages) binary floating point numbers according to the IEEE 754 standard.
|
||||
// This topic is not related with Algorithms, but you can play with .description and .debugDescription for making workarrounds
|
||||
print(book["apple"]?.description ?? "Not exist") // => 0.67
|
||||
print(book["apple"]?.debugDescription ?? "Not exist") // => 0.67000000000000004
|
||||
|
||||
|
||||
|
||||
18
05_hash_tables/swift/02_check_voter.swift
Normal file
18
05_hash_tables/swift/02_check_voter.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
|
||||
var voter = [String : Bool]()
|
||||
|
||||
func checkVoter(_ name : String) {
|
||||
if voter[name] != nil {
|
||||
print("kick them out!")
|
||||
} else {
|
||||
voter[name] = true
|
||||
print("let them vote!")
|
||||
}
|
||||
}
|
||||
|
||||
checkVoter("tom")
|
||||
checkVoter("mike")
|
||||
checkVoter("mike")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user