Skip to content

Day 18 / Mar 24 (Thu)

Warm-Up Exercise

Replace Words in Lines

Write a function that replaces words (case-insensitive) in a given collection of lines with the strings from replacements.

def replaceWords(
  lines: Seq[String],
  replacements: Map[String, String]): Seq[String] = ???
val actual = replaceWords(
  lines = Seq(
    "Good morning. Nice to see you",
    "Dzien dobry. Jak idzie?"),
  replacements = Map(
    "good" -> "Dobry",
    "see" -> "XXX",
    "jak" -> "How"
  )
)
val expected = Seq(
  "Dobry morning. Nice to XXX you",
  "Dzien dobry. How idzie?")
assert(actual == expected)

Review a solution in solutions/replaceWords.sc.

Pair Programming

MyList

Create a PriorityList class to mimic scala.collection.List that keeps numbers sorted. Define the following methods:

  1. add(n: Int): PriorityList
  2. foreach
  3. tail: PriorityList
  4. head: Int that returns the higest number

Review a solution in solutions/priorityList.sc.

Back to top