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
for i in 1...100 { | |
switch (i) { | |
case let x where x%15 == 0: | |
println("CracklePop") | |
case let x where x%3 == 0: | |
println("Crackle") | |
case let x where x%5 == 0: | |
println("Pop") | |
default: | |
break; |
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
let word = "detartrated" | |
func palindromeWord(word : String) ->Bool { | |
return String(reverse(word)) == word ? true : false | |
} | |
palindromeWord(word) // OUTPUT -> True |
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
func recursiveFibonacci(number : Int) -> Int { | |
return number <= 1 ? 1 : recursiveFibonacci(number - 2) + recursiveFibonacci(number - 1) | |
} | |
var fibonacci : Int -> Int = recursiveFibonacci | |
fibonacci(5) |
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
func addPrefix(c : Character) -> (String) -> String { | |
return { | |
(currentString : String) -> String in | |
let result = String(c) + currentString | |
return result | |
} | |
} | |
//OR |
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
extension String { | |
subscript(index : Int) -> Character{ | |
get { | |
return self[advance(self.startIndex, index)] | |
} set(newValue) { | |
self = self.stringByReplacingCharactersInRange(Range<String.Index>(start: advance(startIndex, index), end: advance(startIndex, index+1)), withString: String(newValue)) | |
} | |
} | |
} |
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
extension Double { | |
static var min : Double { | |
get { | |
return -2147483648 | |
} | |
} | |
static var max : Double { | |
get { | |
return 2147483647 | |
} |
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
//Select prime number from array. | |
let primeNumbers = numbers.filter{ | |
(number) in | |
for var i = 2; i <= number/2; i++ { | |
if number%i == 0 { return false } | |
} | |
return true | |
} |
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
//At this moment, this is my solution. I working for a better solution. | |
var n1 = 381654729 | |
var n2 = 102 | |
var n3 = 9876 | |
var n4 = 67 | |
extension Int { | |
func numberOfDigits () -> Int { | |
var cont = 0, num = self |
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
//Note : this is just an experimental, only works with "1", working for works with any number | |
//In mathematics a polydivisible number is a number with digits abcde... that has the following properties : | |
//Its first digit a is not 0. | |
//The number formed by its first two digits ab is a multiple of 2. | |
//The number formed by its first three digits abc is a multiple of 3. | |
//The number formed by its first four digits abcd is a multiple of 4..etc | |
extension Int { | |
func numberOfDigits () -> Int { | |
var cont = 0, num = self |
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
infix operator ** { associativity left precedence 160 } | |
postfix operator ** {} | |
func ** (lhs: Double, rhs: Double) -> Double { | |
return pow(lhs, rhs) | |
} | |
postfix func **(rhs : Int) -> Int { | |
return rhs * rhs | |
} |