Skip to content

Conversation

@bwiedermann
Copy link
Contributor

No description provided.

Copy link

@hmc-cs-dzhang hmc-cs-dzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added critiques to the sample solution

1 / expt(n, e)
else
n * expt(n, e-1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the spacing in this function and in fib. It makes the code exponentially more readable than what I did.

case Some(Some("locked")) "not available"
case Some(Some(s)) s
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only class where I've seen non-ASCII symbols. I worry that certain computers won't be able to understand these characters (I remember there being a problem on Knuth with certain settings), but it also makes the code a bit nicer to read.

def occupancy(room : Option[String]) : Int = room match {
case None | Some("locked") 0
case Some(s) s.toInt
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often forget the case A | B syntax exists in Scala. It feels much nicer than the Java alternative, where this is implemented with fall-through cases unless you add a break. This code is much more readable, and I rarely ever do want cases to fall through, so I wonder why Java adopted this convention.

case null "A null value"
case _ "Some Scala class"
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this example because it shows the power of Scala pattern-matching. In particular, the case first :: second :: tail is a very clean and clear way of both checking the list's length and extracting the elements. Also, the string interpolation is quite readable, much more so than this code would be in a lower-level language like C++.

// remove punctuation and convert every letter to lowercase
val data = s.filter(_.isLetter).map(_.toLower)
data == data.reverse
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is much cleaner than what I did. I remember writing this function in CS70, where we first removed punctuation and converted everything to lower, as you did. But then we passed the result to a helper function that checked to see if the first and last letters were the same, stripped them away, and recursed. This is much more concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants