
S-99: Ninety-Nine Scala Problems
文章平均质量分 60
springlustre
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
P13 (**) 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.Example:scala> encodeDirect(原创 2015-09-29 15:44:15 · 617 阅读 · 0 评论 -
P22 (*) Create a list containing all integers within a given range
Example:scala> range(4, 9)res0: List[Int] = List(4, 5, 6, 7, 8, 9)def range(n1:Int,n2:Int)={ List(n1 to n2)}看了下答案,原来可以直接用List.range()递归解法def range(start: Int, end: Int): List[Int] = if (end <原创 2015-09-30 13:45:59 · 509 阅读 · 0 评论 -
P20 (*) P21 (*)Remove the Kth element from a list;Insert an element at a given position into a list.
Return the list and the removed element in a Tuple. Elements are numbered from 0.Example:scala> removeAt(1, List('a, 'b, 'c, 'd))res0: (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)def removeAt原创 2015-09-30 11:56:35 · 621 阅读 · 0 评论 -
P11 (*) 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.Example:原创 2015-09-29 14:31:06 · 517 阅读 · 0 评论 -
P16 (**)-P17 (*) Drop every Nth element from a list;Split a list into two parts.
16scala> drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))res0: List[Symbol] = List('a, 'b, 'd, 'e, 'g, 'h, 'j, 'k)参考答案:其中关于zip 和filter的用法见http://blog.youkuaiyun.com/springlustre/article/deta原创 2015-09-29 16:33:40 · 523 阅读 · 0 评论 -
P12 (**) Decode a run-length encoded list.
Given a run-length code list generated as specified in problem P10, construct its uncompressed version.Example:scala> decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e)))res0: Lis原创 2015-09-29 15:19:33 · 561 阅读 · 0 评论 -
P19 (**) Rotate a list N places to the left.
Examples:scala> rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))res0: List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c)scala> rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g,原创 2015-09-29 17:40:25 · 428 阅读 · 0 评论 -
P14 (*) -P15 (**)Duplicate the elements of a list,Duplicate the elements of a list a given number of
scala> duplicate(List('a, 'b, 'c, 'c, 'd))res0: List[Symbol] = List('a, 'a, 'b, 'b, 'c, 'c, 'c, 'c, 'd, 'd)scala> duplicateN(3, List('a, 'b, 'c, 'c, 'd))res0: List[Symbol] = List('a, 'a, 'a, 'b, 'b,原创 2015-09-29 15:58:21 · 465 阅读 · 0 评论 -
P18 (**) 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 elemen原创 2015-09-29 17:13:06 · 438 阅读 · 0 评论 -
1、Find the last element of a list.
Example:scala> last(List(1, 1, 2, 3, 5, 8))res0: Int = 8最直接的办法def s1(list:List[Any])={ list.last}def main(args: Array[String]) { println(s1(List(1,2,3,4,5)))}原创 2015-09-24 19:50:06 · 490 阅读 · 0 评论