Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Last active August 29, 2015 13:59
Show Gist options
  • Save DeepSky8/10740649 to your computer and use it in GitHub Desktop.
Save DeepSky8/10740649 to your computer and use it in GitHub Desktop.
There must be an easier, non-compiler-breaking way
def wager(bet: Int, heads: Int, total: Int, flip: Int): (Boolean, Int, Int) = {
if(total > 1000) = heads match {
case 0 => (false, 1, total-bet)
case 1 => (false, 0, total-bet)
} else {
if(heads == flip) {
(true, heads, total+bet)
} else = heads match {
case 0 => (false, 1, total-bet)
case 1 => (false, 0, total-bet)
}
}
}
//edit
//new solution
def wager(bet: Int, heads: Int, total: Int, flip: Int): (Boolean, Int, Int) = {
if(total > 1000) {
(false, switch(heads), total - bet)
} else {
if(heads == flip) {
(true, heads, total+bet)
} else {
(false, switch(heads), total-bet)
}
}
}
def switch(head: Int): Int = head match {
case 0 => 1
case 1 => 0
}
// edit
// more newer solution
def wager(bet: Int, heads: Boolean, total: Int, flip: Int): (Boolean, Boolean, Int) = {
if(total > 1000) {
(false, !(heads), total - bet)
} else {
if(heads == flip) {
(true, heads, total+bet)
} else {
(false, !(heads), total-bet)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment