Created
January 24, 2017 18:55
-
-
Save Quaggie/952f2aa0b2b469a8d8f090382f9d8885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| // MAKING CHANGE --------------------------------------------- | |
| func makingChange (amount: Int, denominations: [Int]) -> Int { | |
| var waysOfDoingNCents: [Int] = [] | |
| for _ in 0...amount { | |
| waysOfDoingNCents.append(0) | |
| } | |
| waysOfDoingNCents[0] = 1 | |
| for coin in denominations { | |
| for higherAmount in coin...amount { | |
| let remainder = higherAmount - coin | |
| waysOfDoingNCents[higherAmount] = waysOfDoingNCents[remainder] | |
| } | |
| } | |
| return waysOfDoingNCents[amount] | |
| } | |
| // makingChange(amount: 5, denominations: [1, 3, 5]) | |
| // FIZZBUZZ --------------------------------------------- | |
| func fizzBuzz (quantity: Int) { | |
| for index in 1...quantity { | |
| if index % 3 == 0 && index % 5 == 0 { | |
| print("Fizz Buzz") | |
| } else if index % 3 == 0 { | |
| print("Fizz") | |
| } else if index % 5 == 0 { | |
| print("Buzz") | |
| } else { | |
| print(index) | |
| } | |
| } | |
| } | |
| // fizzBuzz(quantity: 20) | |
| func fizzBuzzAlternative (quantity: Int) { | |
| for index in 1...quantity { | |
| var result = "" | |
| if index % 3 == 0 { | |
| result += "Fizz" | |
| } | |
| if index % 5 == 0 { | |
| result += (result.isEmpty ? "" : " ") + "Buzz" | |
| } | |
| if result.isEmpty { | |
| result += "\(index)" | |
| } | |
| print(result) | |
| } | |
| } | |
| // fizzBuzzAlternative(quantity: 20) | |
| // QUICKSORT --------------------------------------------- | |
| func quickSort<T: Comparable>(array: [T]) -> [T] { | |
| guard array.count > 1 else { return array } | |
| let pivot = array[array.count / 2] | |
| let lesser = array.filter { $0 < pivot } | |
| let equal = array.filter { $0 == pivot } | |
| let greater = array.filter { $0 > pivot } | |
| return quickSort(array: lesser) + equal + quickSort(array: greater) | |
| } | |
| // quickSort(array: [1, 5, 52, 5, 123, 754, 12, 4, 2, 676, 123, 5, 1234]) | |
| // REVERSE STRING IN PLACE --------------------------------------------- | |
| func reverseStringInPlace (_ string: String) -> String { | |
| var strArray: [String] = [] | |
| for char in string.characters { | |
| strArray.insert(String(char), at: 0) | |
| } | |
| return strArray.joined() | |
| } | |
| func reverseStringInPlace (_ string: inout String) { | |
| var strArray: [String] = [] | |
| for char in string.characters { | |
| strArray.insert(String(char), at: 0) | |
| } | |
| string = strArray.joined() | |
| } | |
| //var string = "Hello, world" | |
| //reverseStringInPlace(&string) | |
| // reverseStringInPlace("Hello, World") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment