Skip to content

Day 17 / Mar 23 (Wed)

Warm-Up Exercises

Consecutive Duplicates of Elements

def consecutiveDuplicates[T](ts: Seq[T], n: Int): Seq[T]
val ns = Seq(1,2,3,4,5)
val actual = consecutiveDuplicates(ns, 3)
val expected = Seq(1,2,2,3,3,3,4,5,5)
assert(actual == expected)
val ss = Seq("one", "two", "three", "four")
val actual = consecutiveDuplicates(ns, 3)
val expected = Seq(
    "one",
    "two", "two",
    "three", "three", "three",
    "four")
assert(actual == expected)

Review a solution in solutions/consecutiveDuplicates.sc.

Pair Programming

File Stats using scopt

Use scopt to handle command-line options in the File Stats application we did yesterday:

  1. --words - All the words and their occurences

  2. --lines - All the lines and their length (number of characters, incl. whitespaces), the number of words, and the output of --words

Stacked Boxes

Develop an application that takes variable number of strings (of odd lenght, e.g. 1, 3, 5, ...) and prints them out as stacked boxes.


Solutions:

  1. Adam + RafaƂ
  2. Ania + Grzegorz + Janek
  3. Daniel + Mateusz
  4. Krzysiek + Damian

Scala Theory

  1. Case classes and copy constructor

Exercises

  1. Use Option[String] and boolean types to define options using scopt

From or based on Scala for the Impatient book:

  1. sum(numbers) using foldLeft

  2. Ex 5 page 26

    Write a procedure countdown(n: Int) that prints the numbers from n to 0.

  3. Ex 2 page 38

    Write a loop that swaps adjancent elements of an array of integers.

    def swap[T](ts: Array[T]): Array[T]
    
    swap(Array(1,2,3,4,5)) == swap(Array(2,1,4,3,5))
    
  4. Ex 9 page 47

    Write a function lteqgt(values: Array[Int], v: Int) that returns a triple containing the counts of values less than v, equal to v, and greater than v.

Schedule

  1. 8:30 - 10:00
  2. 10:15 - 12:00pm
  3. 12:45pm - 1:15pm
  4. 1:30pm - 2:30pm
Back to top