- 博客(12)
- 资源 (8)
- 收藏
- 关注
原创 scala随机数生成,生成一组不重复随机数算法
scala随机数生成函数(new util.Random).nextInt(n) 返回一个0-n(不包括n) 的随机数比如scala> (new util.Random).nextInt(3)res7: Int = 1返回一个[0, 2]的随机数scala生成一组不重复的随机数1、循环获取随机数,再到 list中找,如果没有则添加def randomNew(n:Int)=
2015-09-30 16:28:59 25309
原创 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 464
原创 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 572
原创 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 388
原创 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 388
原创 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.csdn.net/springlustre/article/deta
2015-09-29 16:33:40 485
原创 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 419
原创 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 576
原创 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 517
原创 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 475
原创 scala中的函数组合器map,foreach,flatmap,flatten,filter,zip等用法
1、mapmap方法可以将某个函数应用到集合中的每个元素并产出其结果的集合,比如val names=List("a","b","c")可以用names.map(_.toUpperCase)得到List("A","B","C")2、foreachforeach和map相似,只不过它没有返回值,foreach只要是为了对参数进行作用。比如 nam
2015-09-29 12:37:49 18338
原创 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 456
MyEclipse2013破解工具
2014-03-08
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人