Ch04 映射和元组 - 练习
2015-04-18 08:24 jetwill 阅读(206) 评论(0) 编辑 收藏 举报
1. 设置一个映射,其中包含你想要的一些装备,以及它们的价格。然后构建另一个映射,采用同一组键,但在价格上打9折。
import
scala.collection.JavaConversions.asScalaBuffer
import
scala.collection.mutable.Buffer
import
java.awt.datatransfer.
_
import
java.util.TimeZone
object
App{
def
main(args
:
Array[String]){
val
a
=
Map(
"Mac Mini"
->
4988
,
"MacBook Air"
->
6288
,
"MacBook Pro"
->
8488
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(
"10% OFF"
)
val
b
=
for
( (k,v) <- a )
yield
(k, v *
0.9
)
println(b.mkString(
"["
,
", "
,
"]"
))
}
}
/*result
[Mac Mini -> 4988, MacBook Air -> 6288, MacBook Pro -> 8488]
10% OFF
[Mac Mini -> 4489.2, MacBook Air -> 5659.2, MacBook Pro -> 7639.2]
*/
2. 编写一段程序,从文件中读取单词。用一个可变映射来清点每一个单词出现的频率。读取这些单词的操作可以使用java.util.Scanner:
val in = new java.util.Scanner(new java.io.File("myfile.txt"))
while(in.hasNext()) 处理 in.next()
或者翻到第9章看看更Scala的做法。
最后,打印出所有单词和它们出现的次数。
3. 重复前一个练习,这次用不可变的映射。
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
Map[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
4. 重复前一个练习,这次用已排序的映射,以便单词可以按顺序打印出来。
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
scala.collection.immutable.SortedMap[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
5. 重复前一个练习,这次用java.util.TreeMap并使之适用于Scala API
import scala.collection.JavaConversions.mapAsScalaMap object App{ def main(args : Array[String]){ val map = countWord( "myfile.txt" ) println(map.mkString( "[" , ", " , "]" )) } def countWord(filename : String) = { var words = new java.util.TreeMap[String, Int]() val in = new java.util.Scanner( new java.io.File( "myfile.txt" )) while (in.hasNext()){ var key = in.next() words + = (key -> (words.getOrElse(key, 0 ) + 1 )) } words } } /*result */
|
6. 定义一个链式哈希映射,将"Monday"映射到java.util.Calendar.MONDAY,以此类推加入其他日期。
展示元素是以插入的顺序被访问的。
import
scala.collection.JavaConversions.mapAsScalaMap
object
App{
def
main(args
:
Array[String]){
val
map
=
weekday(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
weekday(filename
:
String)
=
{
var
weekdays
=
new
java.util.LinkedHashMap[String,Int]()
weekdays(
"Monday"
)
=
java.util.Calendar.MONDAY
weekdays(
"Tuesday"
)
=
java.util.Calendar.TUESDAY
weekdays(
"Wednesday"
)
=
java.util.Calendar.WEDNESDAY
weekdays(
"Thursday"
)
=
java.util.Calendar.THURSDAY
weekdays(
"Friday"
)
=
java.util.Calendar.FRIDAY
weekdays(
"Saturday"
)
=
java.util.Calendar.SATURDAY
weekdays(
"Sunday"
)
=
java.util.Calendar.SUNDAY
weekdays
}
}
/*result
[Monday -> 2, Tuesday -> 3, Wednesday -> 4, Thursday -> 5, Friday -> 6, Saturday -> 7, Sunday -> 1]
*/
7. 打印出所有Java系统属性的表格,类似这样:
java.runtime.name | Java(TM) SE Runtime Environment
...
你需要找到最长键的长度才能正确地打印出这张表格。
import
scala.collection.JavaConversions.propertiesAsScalaMap
object
App{
def
main(args
:
Array[String]){
printSystemProperties()
}
def
printSystemProperties()
=
{
val
props
:
scala.collection.Map[String, String]
=
System.getProperties()
var
maxlen
=
0
for
((k,
_
) <- props)
if
(maxlen < k.length) maxlen
=
k.length
maxlen +
=
1
for
((k,v) <- props){
printf(
"%-"
+ maxlen.toString +
"s | %s\n"
, k, v)
}
}
}
/*result
java.vm.specification.version | 1.8
sun.java.command | scala.tools.nsc.MainGenericRunner e4-7.scala
java.home | H:\ProgramFiles\Java\jdk1.8.0_25\jre
...
*/
8. 编写一个函数minmax(values: Array[Int]),返回数组中最小值和最大值的对偶。
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
minmax(a)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
minmax(values
:
Array[Int])
=
{
(values.min,values.max)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(1,7)
*/
9. 编写一个函数lteqgt(values: Array[Int], v: Int),返回数组中小于v、等于v和大于v的数量,要求三个值一起返回。
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
lteqgt(a,
4
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
lteqgt(values
:
Array[Int], v
:
Int)
=
{
val
lt
=
values.count(
_
< v)
val
eq
=
values.count(
_
==
v)
val
gt
=
values.count(
_
> v)
(lt, eq, gt)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(3,1,3)
*/
10. 当你将两个字符串拉链在一起,比如"Hello".zip("World"),会是什么结果?想出一个讲得通的用例。
object App{ def main(args : Array[String]){ val a = zip( "Hello" , "World" ) println(a) //println(a.mkString("[",", ","]")) } def zip(s 1 : String, s 2 : String) = { val a = s 1 .zip(s 2 ) a } } /*result Vector((H,W), (e,o), (l,r), (l,l), (o,d)) */
|