Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Created April 17, 2015 20:55
Show Gist options
  • Save dcbriccetti/76fde0ccb9c72b996b9c to your computer and use it in GitHub Desktop.
Save dcbriccetti/76fde0ccb9c72b996b9c to your computer and use it in GitHub Desktop.
/* Display the numbers from 1 to 100 that are not divisible by both
4 and 7, but for those numbers divisible by 4, show ÷4 instead, and
for those divisible by 7, show ÷7 instead. */
object Solution1 extends App {
println(
(1 to 100 map(n => {
def divBy(div: Int) = n % div == 0
(divBy(4), divBy(7)) match {
case (false, false) => Some(n)
case (false, true ) => Some("÷7")
case (true, false) => Some("÷4")
case (true, true ) => None
}
})).flatten.mkString("\n")
)
}
object Solution2 extends App {
println(
(1 to 100 map(n => {
def divBy(div: Int) = n % div == 0
if (divBy(4) && divBy(7)) None
else if (divBy(4)) Some("÷4")
else if (divBy(7)) Some("÷7")
else Some(n)
})).flatten.mkString("\n")
)
}
@swartzrock
Copy link

I like the first one... easier to read!

@SethTisue
Copy link

pattern matching +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment