-
-
Save akr4/1151256 to your computer and use it in GitHub Desktop.
S-99 blank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *** S-99: Ninety-Nine Scala Problems *** | |
// http://aperiodic.net/phil/scala/s-99/ | |
// | |
// wget http://www.scala-tools.org/repo-releases/org/scalatest/scalatest_2.9.0-1/1.6.1/scalatest_2.9.0-1-1.6.1.jar | |
// scala -cp scalatest-*.jar s99.scala | |
import org.scalatest.matchers.ShouldMatchers._ | |
// P01: Find the last element of a list. | |
def last[T](list: List[T]): T = { // TODO } | |
last(List(1, 2, 3)) should equal(3) | |
last(List("foo", "var", "baz")) should equal("baz") | |
// P02: Find the last but one element of a list. | |
def penultimate[T](list: List[T]): T = { // TODO } | |
penultimate(List(1, 1, 2, 3, 5, 8)) should equal(5) | |
penultimate(List("foo", "var", "baz")) should equal("var") | |
// P03: Find the Kth element of a list. | |
// By convention, the first element in the list is element 0. | |
def nth[T](n: Int, list: List[T]): T = { // TODO } | |
nth(0, List(0, 1, 2, 3, 5, 8)) should equal(0) | |
nth(1, List(0, 1, 2, 3, 5, 8)) should equal(1) | |
nth(2, List(1, 1, 2, 3, 5, 8)) should equal(2) | |
// P04: Find the number of elements of a list. | |
def length[T](list: List[T]): Int = { // TODO } | |
length(Nil) should equal(0) | |
length(List(1, 1, 2, 3, 5, 8)) should equal(6) | |
length(List("a", "b", "c")) should equal(3) | |
// P05: Reverse a list. | |
def reverse[T](list: List[T]): List[T] = { // TODO } | |
reverse(List(1, 1, 2, 3, 5, 8)) should equal(List(8, 5, 3, 2, 1, 1)) | |
reverse(List("foo", "var", "baz")) should equal(List("baz", "var", "foo")) | |
// P06: Find out whether a list is a palindrome. | |
def isPalindrome[T](list: List[T]): Boolean = { // TODO } | |
isPalindrome(List(1, 2, 3, 2, 1)) should equal(true) | |
isPalindrome(List(1, 2, 3, 3, 1)) should equal(false) | |
// P07: Flatten a nested list structure. | |
def flatten(list: List[Any]): List[Any] = { // TODO } | |
flatten(List(List(1, 1), 2, List(3, List(5, 8)))) should equal(List(1, 1, 2, 3, 5, 8)) | |
// P08: Eliminate consecutive duplicates of list elements. | |
// If a list contains repeated elements they should be replaced with a single copy of the element. | |
// The order of the elements should not be changed. | |
def compress[T](list: List[T]): List[T] = { // TODO } | |
compress(List('a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e)) should equal(List('a, 'b, 'c, 'a, 'd, 'e)) | |
// P09: Pack consecutive duplicates of list elements into sublists. | |
// If a list contains repeated elements they should be placed in separate sublists. | |
def pack[T](list: List[T]): List[List[T]] = { // TODO } | |
pack(List(1, 1, 1, 2, 3, 3, 2, 3, 3)) should equal(List(List(1, 1, 1), List(2), List(3, 3), List(2), List(3, 3))) | |
// P10: Run-length encoding of a list. | |
// Use the result of problem P09 to implement the so-called run-length encoding data compression method. | |
// Consecutive duplicates of elements are encoded as tuples (N, E) where N is the number of duplicates of the element E. | |
def encode[T](list: List[T]): List[(Int, T)] = { // TODO } | |
encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'd, 'd, 'd)) should equal(List((4, 'a), (1, 'b), (2, 'c), (3, 'd))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment