The full list: http://aperiodic.net/phil/scala/s-99/
-
Find the last element of a list.
last(List(1, 1, 2, 3, 5, 8)) res0: Int = 8 -
Find the last but one element of a list.
penultimate(List(1, 1, 2, 3, 5, 8)) res0: Int = 5 -
Find the Kth element of a list. By convention, the first element in the list is element 0.
nth(2, List(1, 1, 2, 3, 5, 8)) res0: Int = 2 -
Find the number of elements of a list.
length(List(1, 1, 2, 3, 5, 8)) res0: Int = 6 -
Reverse a list.
reverse(List(1, 1, 2, 3, 5, 8)) res0: List[Int] = List(8, 5, 3, 2, 1, 1) -
Find out whether a list is a palindrome.
isPalindrome(List(1, 2, 3, 2, 1)) res0: Boolean = true -
Flatten a nested list structure.
flatten(List(List(1, 1), 2, List(3, List(5, 8)))) res0: List[Any] = List(1, 1, 2, 3, 5, 8) -
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.
compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[Symbol] = List('a, 'b, 'c, 'a, 'd, 'e) -
Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.
pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e)) -
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.
encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[(Int, Symbol)] = List((4,'a), (1,'b), (2,'c), (2,'a), (1,'d), (4,'e)) -
Modified run-length encoding. Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N, E) terms.
encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) List[Any] = List((4,'a), 'b, (2,'c), (2,'a), 'd, (4,'e)) -
Decode a run-length encoded list. Given a run-length code list generated as specified in problem P10, construct its uncompressed version.
decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e))) List[Symbol] = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e) -
Run-length encoding of a list (direct solution). Implement the so-called run-length encoding data compression method directly. I.e. don't use other methods you've written (like P09's pack); do all the work directly.
encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) List[(Int, Symbol)] = List((4,'a), (1,'b), (2,'c), (2,'a), (1,'d), (4,'e)) -
Duplicate the elements of a list.
duplicate(List('a, 'b, 'c, 'c, 'd)) List[Symbol] = List('a, 'a, 'b, 'b, 'c, 'c, 'c, 'c, 'd, 'd) -
Duplicate the elements of a list a given number of times.
duplicateN(3, List('a, 'b, 'c, 'c, 'd)) List[Symbol] = List('a, 'a, 'a, 'b, 'b, 'b, 'c, 'c, 'c, 'c, 'c, 'c, 'd, 'd, 'd) -
Drop every Nth element from a list.
drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) List[Symbol] = List('a, 'b, 'd, 'e, 'g, 'h, 'j, 'k) -
Split a list into two parts.
split(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) (List[Symbol], List[Symbol]) = (List('a, 'b, 'c),List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) -
Extract a slice from a list. Given two indices, I and K, the slice is the list containing the elements from and including the Ith element up to but not including the Kth element of the original list. Start counting the elements with 0.
slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) List[Symbol] = List('d, 'e, 'f, 'g) -
Rotate a list N places to the left.
rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c) rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)) List[Symbol] = List('j, 'k, 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i) -
Remove the Kth element from a list. Return the list and the removed element in a Tuple. Elements are numbered from 0.
removeAt(1, List('a, 'b, 'c, 'd)) (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)