Skip to content

Instantly share code, notes, and snippets.

@PedroHLC
Last active August 3, 2016 00:00
Show Gist options
  • Save PedroHLC/04c69021919b36c3481aa24e379b5ea4 to your computer and use it in GitHub Desktop.
Save PedroHLC/04c69021919b36c3481aa24e379b5ea4 to your computer and use it in GitHub Desktop.
//ex1
import Foundation
func fibbo(alvo: UIntMax) -> IntMax? {
switch alvo {
case 0:
return nil
case 1...2:
return 1
default:
break
}
var penultimo:IntMax = 1, ultimo:IntMax = 1, atual:IntMax = 0
for i in 3...alvo {
atual = ultimo + penultimo
penultimo = ultimo
ultimo = atual
i
}
return atual
}
fibbo(8)
//ex2
extension Int {
func is_armstrong() -> Bool {
var outroeu = self, casas:UIntMax = 0
while(outroeu != 0) {
casas += 1
outroeu /= 10
}
outroeu = self
var resto:Int = 0, resultado = 0
while(outroeu != 0) {
resto = outroeu % 10
resultado += Int(pow(Double(resto), Double(casas)))
outroeu /= 10
}
return(resultado == self)
}
}
153.is_armstrong()
9926315.is_armstrong()
//ex3
class MaqKaraoke {
var registro: [String]
init() {
registro = [String]()
}
func insMusica(lista: [String]) {
registro.appendContentsOf(lista)
}
func remMusica(musica: String) {
registro.removeAtIndex(registro.indexOf(musica)!)
}
}
var olar = MaqKaraoke()
var lista1 = ["Don't Fear the Reaper", "Breaking All Illusions", "Plug in Baby", "Black"]
olar.insMusica(lista1)
olar.remMusica("Black")
olar.registro
//ex4
func ordena_macacos(vetor_lindo: [Int]) -> [Int] {
var resultado = vetor_lindo.sort()
var repetidos = [Int]()
var i:Int = 0
repeat {
var j = i
repeat {
j += 1
} while(j < resultado.count && resultado[j] == resultado[i])
j -= 1;
if(j != i) {
repetidos.appendContentsOf(resultado[i...j])
resultado.removeRange(i...j)
} else {
i += 1
}
} while(i < resultado.count)
resultado.appendContentsOf(repetidos)
return resultado
}
ordena_macacos([2, 4, 1, 3, 6, 4, 7])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment