-
Notifications
You must be signed in to change notification settings - Fork 17
Ben's (sample) solution #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
hmc-cs-dzhang
left a comment
There was a problem hiding this 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) | ||
|
|
There was a problem hiding this comment.
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 | ||
| } | ||
|
|
There was a problem hiding this comment.
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 | ||
| } |
There was a problem hiding this comment.
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" | ||
| } | ||
|
|
There was a problem hiding this comment.
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 | ||
| } |
There was a problem hiding this comment.
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.
No description provided.