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:
-
--words - All the words and their occurences
-
--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:
Scala Theory¶
Exercises¶
- Use
Option[String]
andboolean
types to define options usingscopt
From or based on Scala for the Impatient book:
-
sum(numbers)
usingfoldLeft
-
Ex 5 page 26
Write a procedure
countdown(n: Int)
that prints the numbers fromn
to 0. -
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))
-
Ex 9 page 47
Write a function
lteqgt(values: Array[Int], v: Int)
that returns a triple containing the counts of values less thanv
, equal tov
, and greater thanv
.
Schedule¶
- 8:30 - 10:00
- 10:15 - 12:00pm
- 12:45pm - 1:15pm
- 1:30pm - 2:30pm