Created
April 17, 2015 14:29
-
-
Save andrei512/87e622adfa8189013cd3 to your computer and use it in GitHub Desktop.
This file contains 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
var n = 12 | |
var bile = [3, 4, 6, 7, 8, 9, 2, 1, 10, 18, 22, 26] | |
var extrase = [2, 9, 3, 4, 22, 6] | |
extrase.sort(<) | |
bile.sort(<) | |
var minBall = extrase[0] | |
var maxBall = extrase[5] | |
// gasim bilele din seif | |
var seif = bile.filter { ball in | |
return contains(extrase, ball) == false | |
} | |
// gasim cea mai apropiata bila de cea mai mica | |
var bestMin = seif.reduce(seif[0]) { best, ball in | |
if abs(minBall - best) >= abs(minBall - ball) { | |
return ball | |
} else { | |
return best | |
} | |
} | |
// o scoatem din seif | |
seif = seif.filter { $0 != bestMin } | |
seif.sort(<) | |
// o inlocuim | |
extrase[0] = bestMin | |
// gasim cea mai apropiata bila de cea mai mare | |
var bestMax = seif.reduce(seif[0]) { best, ball in | |
if abs(maxBall - best) >= abs(maxBall - ball) { | |
return ball | |
} else { | |
return best | |
} | |
} | |
// o inlocuim si pe cea mare | |
extrase[5] = bestMax | |
// ordonam | |
extrase.sort(<) | |
for ball in extrase { | |
print(ball) | |
print(" ") | |
} | |
This file contains 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
// Playground - noun: a place where people can play | |
import Cocoa | |
var n = 10 | |
var bile = [231, 212, 32, 123, 453, 675, 1321, 54, 67, 567] | |
var extrase = [212, 32, 67, 567, 675, 1321] | |
func esteInExtrase(bila: Int) -> Bool { | |
for bilaExtrasa in extrase { | |
if (bilaExtrasa == bila) { | |
return false | |
} | |
} | |
return true | |
} | |
var bileNeextrase = bile.filter(esteInExtrase) | |
var ceaMaiMica = extrase.reduce(99999, combine: min) | |
var ceaMaiMare = extrase.reduce(0, combine: max) | |
var bileRamase = extrase.filter{$0 != ceaMaiMica && $0 != ceaMaiMare} | |
func ceaMaiApropiata(bila: Int) -> Int { | |
var distante = zip(bileNeextrase.map{abs($0 - bila)},bileNeextrase) | |
return reduce(distante,(9999,99999)) { | |
if $0.0 > $1.0 { | |
return $1 | |
} else { | |
return $0 | |
} | |
}.1 | |
} | |
var ceaMaiApropiataMica = ceaMaiApropiata(ceaMaiMica) | |
bileNeextrase = bileNeextrase.filter{$0 != ceaMaiApropiataMica} | |
var ceaMaiApropiataMare = ceaMaiApropiata(ceaMaiMare) | |
bileRamase.append(ceaMaiApropiataMica) | |
bileRamase.append(ceaMaiApropiataMare) | |
var sortate = bileRamase.sorted(<) | |
var final = sortate.reduce("", combine: { | |
$0 + " \($1)" | |
}) | |
println(final) |
This file contains 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
var n = 6 | |
var inaltimi = [1200, 1204, 1199, 1197, 1202, 1205] | |
var persoane = [3, 2, 8, 0, 10, 0] | |
var totalPers = persoane.reduce(0, combine: +) | |
var lastHeight = inaltimi[0] | |
var fuel = 0 | |
enum Mode { | |
case None | |
case Up | |
case Down | |
} | |
var lastMode = Mode.None | |
var special = 0 | |
for i in 2...n { | |
var height = inaltimi[i - 1] | |
var dif = height - lastHeight | |
var mode: Mode = .None | |
if dif > 0 { | |
fuel += dif * 3 | |
mode = .Up | |
} else { | |
fuel -= dif | |
mode = .Down | |
} | |
if lastMode != .None { | |
if lastMode != mode { | |
special += 1 | |
} | |
} | |
lastHeight = height | |
lastMode = mode | |
} | |
println(totalPers) | |
println(fuel) | |
println(special) | |
This file contains 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
var n = 6 | |
var inaltimi = [1200, 1204, 1199, 1197, 1202, 1205] | |
var persoane = [3, 2, 8, 0, 10, 0] | |
println(persoane.reduce(0,combine: +)) | |
var intaltimiCombinate = zip(inaltimi[1...inaltimi.count - 1],inaltimi[0...inaltimi.count - 2]) | |
var b = reduce(intaltimiCombinate,0,{ | |
if ($1.1 < $1.0) { | |
return $0 + 3 * abs($1.1 - $1.0) | |
} else { | |
return $0 + abs($1.1 - $1.0) | |
} | |
} | |
) | |
println(b) | |
func sign(x:Int) -> Int { | |
if (x >= 0) { | |
return 1 | |
} else { | |
return -1 | |
} | |
} | |
var diferente = map(intaltimiCombinate, { | |
sign($0.1 - $0.0) | |
}) | |
var diferenteCombinate = zip(diferente[1...diferente.count - 1],diferente[0...diferente.count - 2]) | |
var c = reduce(diferenteCombinate,0, { | |
if ($1.1 != $1.0) { | |
return $0 + 1 | |
} | |
return $0 | |
}) | |
println(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment