Created
October 3, 2012 16:00
-
-
Save oxbowlakes/3827793 to your computer and use it in GitHub Desktop.
Scala Exercises
This file contains hidden or 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
This is how we create an inclusive/exclusive range of Ints: | |
val ie = 0 until 500 | |
//1. Fill in the missing item to create a range of Ints from 1 to 100 inclusive | |
val ints = 1 ??? 100 | |
//2. Find the sum of the integers in this range | |
val sum = ??? | |
//3. Write a function to calculate the remainder of an integer when divided by 3. | |
// Note we write % to denote remainder: e.g. 5 % 3 is 2 | |
val rem3: Int => Int = ??? | |
//4. Partition all ints between 1 and 100 into those that are divisible by 3 and those that are not | |
val (divisibleBy3, notDivisibleBy3) = ??? | |
//5. Calculate the sum of those ints between 1 and 100 which are not divisible by 3 | |
val result = ??? | |
//6. Construct a List[String] by taking each int j in the range 1 to 10 and making the String 'j + " men went to mow"' | |
val mowers = 1 to 10 map ??? | |
//7. Construct an Address class. It should have fields for house number (an Int), street name (a String), city (also a String), zip code (an Int) and country (a String) | |
case class Address(???) | |
//8. Given a List of addresses; find the Set of cities in which they reside | |
val cities = addresses ??? | |
//9. Show 3 different ways of writing a function which maps a String to its length: | |
val strlen1 = (s: String) => { s.length } | |
//10. Write a method which will group a Set of Strings by their length. The method's signature should look like this: | |
def groupStringsByLength(s: Set[String]): Map[Int, Set[String]] = ??? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment