第190页 第13章 集合(习题)
- 章节名:第13章 集合(习题)
- 页码:第190页 2013-12-29 14:03:48
// 习题1 def indexes(str: String) = { str.zipWithIndex.groupBy(_._1).map { tuple => tuple._1 -> tuple._2.map(_._2).toSet } } // 要求mutable map和mutable set以及排序集 import scala.collection.mutable def indexesMutable(str: String) = { var res = mutable.HashMap[Char, mutable.LinkedHashSet[Int]]() for ((c, i) <- str.zipWithIndex) { val set = res.getOrElseUpdate(c, mutable.LinkedHashSet()) set += i } res } // 习题2 def indexes2(str: String) = { str.zipWithIndex.groupBy(_._1).map { tuple => tuple._1 -> tuple._2.map(_._2).toList } } // 习题3 def filterZero(l: List[Int]) = l.filter(_ != 0) // 习题4 def getMapValue(a: Array[String], m: Map[String, Int]) = a.map(m.get(_)).flatMap(x => x) // 习题5 def mkString[T](s: Seq[T], sep: String =",") = s.map(_.toString).reduceLeft(_.toString + sep + _.toString) // 习题6 // (lst :\ List[Int]())(_::_) 得到和lst一样的整数List // (List[Int]() /: lst)(_:+_) 得到和lst一样的整数List // 要反序排列,可以 // (lst :\ List[Int]())((elem, coll) => coll :+ elem) 或者 // (List[Int]() /: lst)((coll, elem) => elem +: coll) // 习题7 val prices = List(5.0, 20.0, 9.95) val quantities = List(10, 2, 1) prices.zip(quantities).map(Function.tupled(_*_)) // 习题8 def mkTwoDimArray(a: Array[Double], cols: Int) = a.grouped(cols).toArray // 并发相关部分没看 // 习题9 // 习题10
7人阅读
atlarge对本书的所有笔记 · · · · · ·
-
第162页 第12章 高阶函数(习题)
// 习题1 def values(fun: (Int) => Int, low: Int, high: Int) = { (low to high).map(v ...
-
第190页 第13章 集合(习题)
-
第208页 第14章 模式匹配与样例类(习题)
// 习题2 def swap(v: (Int, Int)) = { v match { case (x, y) => (y, x) } } // 习题3 im...
-
第256页 第17章 类型参数(习题)
// 习题1 class ImmutablePair[T, S](fst: T, snd: S){ private val pair = (fst, snd) def s...
> 查看全部4篇
说明 · · · · · ·
表示其中内容是对原文的摘抄