Last active
May 12, 2024 18:55
-
-
Save infomaven/735b72d3db70ad603ed2 to your computer and use it in GitHub Desktop.
Classic program that prints strings based on results of using MOD function against a List
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
/* | |
Write a program that prints out the numbers 1 to 100 (inclusive). | |
If the number is divisible by 3, print Crackle instead of the number. | |
If it's divisible by 5, print Pop. If it's divisible by both 3 and 5, | |
print CracklePop. You can use any language. | |
*/ | |
/// any number x where x % y is divisible by that number. We'll use that fact to match boolean patterns in Scala. | |
//// In Scala RePL, copy-paste the function first. Then use the function in a second call from the REPL. | |
def listEval( number: Int ): String = (number % 3, number % 5 ) match { | |
case( 0, 0 ) => "CracklePOP" | |
case(0, _ ) => "Crackle" | |
case( _, 0) => "Pop" | |
case _ => number.toString() | |
} | |
val myList = (1 to 100) map {listEval(_)} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment