Skip to content

Latest commit

 

History

History
102 lines (73 loc) · 3.93 KB

2013-02-12-book-problems.md

File metadata and controls

102 lines (73 loc) · 3.93 KB

Scala

Day 1: The Castle on the Hill

Find

Do

  • Write a game that will take a tic-tac-toe board with X, O, and blank characters and detect the winner or whether there is a tie or no winner yet. Use classes where appropriate.

  • Bonus problem: Let two players play tic-tac-toe.


Day 2: Clipping Bushes and Other New Tricks

Find

  • A discussion on how to use Scala files

Getting Started with Scala StackOverflow: Read entire file in Scala? StackOverflow: Load Scala file into interpreter to use functions?

Do

  • Use foldLeft to compute the total size of a list of strings.

foldLeft:

(0 /: words) { _ + _.size }
(0 /: words) { (sum, word) => sum + word.size }
words.foldLeft(0)( _ + _.size )
words.foldLeft(0) { (sum, word) => sum + word.size }

foldRight:

(words :\ 0) { _.size + _ }
(words :\ 0) { (word, sum) => word.size + sum }
words.foldRight(0)( _.size + _ )
words.foldRight(0) { (word, sum) => word.size + sum }
  • Write a Censor trait with a method that will replace the curse words Shoot and Darn with Pucky and Beans alternatives. Use a map to store the curse words and their alternatives.
trait Censor {
  val replacement = Map("Shoot" -> "Pucky", "Darn" -> "Bean")
  def censor(text: String) = (replacement :\ text) {
    case ((key, value), memo) => memo.replaceAll(key, value)
  }
}
new Censor{}.censor("Shoot, Darn")
  • Load the curse words and alternatives from a file.
val source = scala.io.Source.fromFile("censor.text")
val lines = source.mkString.split('\n')
val map = (lines :\ Map()) { (line, memo) =>
  println(line)
  val words = line.split(',')
  memo + (words(0) -> words(1))
}
source.close

Day 3: Day 3: Cutting Through the Fluff

Find

  • For the sizer program, what would happen if you did not create a new actor for each link you wanted to follow? What would happen to the performance of the application?

Do

  • Take the sizer application and add a message to count the number of links on the page.
  • Bonus problem: Make the sizer follow the links on a given page, and load them as well. For example, a sizer for “google.com” would compute the size for Google and all of the pages it links to.