Skip to content

Instantly share code, notes, and snippets.

View joanmolinas's full-sized avatar
🎯
Focusing

Joan Molinas joanmolinas

🎯
Focusing
View GitHub Profile
@joanmolinas
joanmolinas / Crackle.swift
Last active August 29, 2015 14:17
HackerSchool
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;
let word = "detartrated"
func palindromeWord(word : String) ->Bool {
return String(reverse(word)) == word ? true : false
}
palindromeWord(word) // OUTPUT -> True
func recursiveFibonacci(number : Int) -> Int {
return number <= 1 ? 1 : recursiveFibonacci(number - 2) + recursiveFibonacci(number - 1)
}
var fibonacci : Int -> Int = recursiveFibonacci
fibonacci(5)
func addPrefix(c : Character) -> (String) -> String {
return {
(currentString : String) -> String in
let result = String(c) + currentString
return result
}
}
//OR
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))
}
}
}
extension Double {
static var min : Double {
get {
return -2147483648
}
}
static var max : Double {
get {
return 2147483647
}
//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
}
@joanmolinas
joanmolinas / Polydivisible.swift
Last active August 29, 2015 14:18
Polidisible Number
//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
//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
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
}