快学Scala 第四章习题答案

1.设置一个映射,其中包含你想要的一些装备,以及它们的价格。然后构建另一个映射,但在价格上打9折。

scala> val map = Map("book"->10,"pen"->20,"ipad"->30)
map: scala.collection.immutable.Map[String,Int] = Map(book -> 10, pen -> 20, ipa
d -> 30)

scala> for((k,v)<-map) yield (k,v*0.8)
res0: scala.collection.immutable.Map[String,Double] = Map(book -> 8.0, pen -> 16
.0, ipad -> 24.0)

2.编写一段程序,从文件中读取单词。用一个可变映射来清点每一个单词出现的频率,最后打印出所有单词和它们出现的次数。

scala> val source = Source.fromFile("F:\\1.txt")
source: scala.io.BufferedSource = non-empty iterator

scala> val tokens = source.mkString.split("\\s+")
tokens: Array[String] = Array(ddd, haha, ddd, eee, eee, eee, ddd, ddd, haha, kkk
)

scala> val time = new scala.collection.mutable.HashMap[String,Int]
time: scala.collection.mutable.HashMap[String,Int] = Map()

scala> for(key <- tokens){
     | time(key) = time.getOrElse(key,0) + 1
     | }

scala> time.mkString(",")
res7: String = haha -> 2,kkk -> 1,eee -> 3,ddd -> 4

3.重复前一个联系,这次用不可变的映射。

scala> import scala.io.Source
import scala.io.Source

scala> val source = Source.fromFile("F:\\1.txt")
source: scala.io.BufferedSource = non-empty iterator

scala> val tokens = source.mkString.split("\\s+")
tokens: Array[String] = Array(ddd, haha, ddd, eee, eee, eee, ddd, ddd, haha, kkk
)

scala> val times = new scala.collection.immutable.HashMap[String,Int]
times: scala.collection.immutable.HashMap[String,Int] = Map()

scala> var times = new scala.collection.immutable.HashMap[String,Int]
times: scala.collection.immutable.HashMap[String,Int] = Map()

scala> for(key <- tokens){
     | times+= (key -> (times.getOrElse(key,0) + 1))
     | }

scala> times.mkString(",")
res9: String = kkk -> 1,ddd -> 4,eee -> 3,haha -> 2

4.重复前一个联系,这次用已排序的映射,以便单词可以按顺序打印出来。

scala> import scala.io.Source
import scala.io.Source

scala> val source = Source.fromFile("F:\\1.txt")
source: scala.io.BufferedSource = non-empty iterator

scala> val tokens = source.mkString.split("\\s+")
tokens: Array[String] = Array(ddd, haha, ddd, eee, eee, eee, ddd, ddd, haha, kkk
)

scala> val times = new scala.collection.immutable.HashMap[String,Int]
times: scala.collection.immutable.HashMap[String,Int] = Map()

scala> var times = new scala.collection.immutable.HashMap[String,Int]
times: scala.collection.immutable.HashMap[String,Int] = Map()

scala> for(key <- tokens){
     | times+= (key -> (times.getOrElse(key,0) + 1))
     | }

scala> times.mkString(",")
res9: String = kkk -> 1,ddd -> 4,eee -> 3,haha -> 2

5.重复前一个联系,这次用java.util.TreeMap并使之适用于Scala API。

scala> val source = Source.fromFile("F:\\1.txt")
source: scala.io.BufferedSource = non-empty iterator

scala> val tokens = source.mkString.split("\\s+")
tokens: Array[String] = Array(ddd, haha, ddd, eee, eee, eee, ddd, ddd, haha, kkk
)

scala> import scala.collection.JavaConversions.mapAsScalaMap
import scala.collection.JavaConversions.mapAsScalaMap

scala> val time:scala.collection.mutable.Map[String,Int] = new java.util.TreeMap
[String,Int]
time: scala.collection.mutable.Map[String,Int] = Map()
scala> }for(key <- tokens){
:1: error: eof expected but '}' found.
       }for(key <- tokens){
       ^

scala> for(key <- tokens){
     | time(key) = time.getOrElse(key,0) + 1
     | }

scala> time.mkString(",")
res14: String = ddd -> 4,eee -> 3,haha -> 2,kkk -> 1

6.定义一个链式哈希映射,将“Monday”映射到java.util.Calendar.MONDAY.以此类推加入其他日期。展示元素是以插入的顺序被访问的。

scala> import scala.collection.mutable.LinkedHashMap
import scala.collection.mutable.LinkedHashMap

scala> val map = LinkedHashMap[String,Int]()
map: scala.collection.mutable.LinkedHashMap[String,Int] = Map()

scala> import java.util.Calendar
import java.util.Calendar

scala> map += ("Monday" -> Calendar.MONDAY)
res15: map.type = Map(Monday -> 2)

scala> map += ("Tuesday" -> Calendar.TUESDAY)
res16: map.type = Map(Monday -> 2, Tuesday -> 3)

scala> map.mkString(",")
res17: String = Monday -> 2,Tuesday -> 3

7.打印出所有Java系统实行的表格,你需要找到最长键的长度才能正确的打印出这张表格。

import scala.collection.JavaConversions.propertiesAsScalaMap

val props:scala.collection.Map[String,String] = System.getProperties()

val keys = props.keySet

val keyLengths = for( key <- keys ) yield key.length

val maxKeyLength = keyLengths.max

for(key <- keys) {
  print(key)
  print(" " * (maxKeyLength - key.length))
  print(" | ")
  println(props(key))
}

8.编写一个函数minmax(values:Array[Int]),返回数组中最小值和最大值的对偶。

def minmax(values:Array[Int])={
    (values.min,values.max)
}

9.编写一个函数lteqgt(values:Array[Int],v:Int),返回数组中小于v、等于v和大于v的数量,要求三个值一起返回。

def lteqgt(values:Array[Int],v:Int)={
    (values.count(_ < v), values.count(_ == v),values.count(_ > v))
}

10.当你将两个字符串拉链在一起,回事什么结果?相处一个讲的通的用例。

scala> "Hello".zip("World")
res19: scala.collection.immutable.IndexedSeq[(Char, Char)] = Vector((H,W), (e,o)
, (l,r), (l,l), (o,d))

你可能感兴趣的:(scala)