Scala:数据结构

1、编写一段代码,将a设置为一个n个随机整数的数组,要求随机数介于0和n之间。

scala> def makeArr(n : Int) : Array[Int] = {
     |     val a = new Array[Int](n);
     |     val rand = new scala.util.Random();
     |     for (i <- a) yield rand.nextInt(n);
     |   }
makeArr: (n: Int)Array[Int]

scala> makeArr(10).foreach(println)
9
8
3
5
9
6
8
8
0
1
  1. 编写一个循环,将整数数组中相邻的元素置换。比如Array(1, 2, 3, 4, 5)置换后为Array(2, 1, 4, 3, 5)
scala> def revert(arr : Array[Int]) = {
     |     for (i <- 0 until (arr.length - 1, 2)) {
     |       val t = arr(i);
     |       arr(i) = arr(i + 1);
     |       arr(i + 1) = t;
     |     }
     |   }
revert: (arr: Array[Int])Unit

scala> val a = Array(1, 2, 3, 4, 5);
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala>     revert(a);

scala>     a.foreach(println);
2
1
4
3
5
  1. 给定一个整数数组,产出一个新的数组,包含原数组中的所有正值,以原有顺序排列,之后的元素是所有零或负值,以原有顺序排列。
scala> def sigNumArr(arr : Array[Int]) = {
     |     val buf = new ArrayBuffer[Int]();
     |     buf ++= (for (i <- arr if i > 0) yield i)
     |     buf ++= (for (i <- arr if i == 0) yield i)
     |     buf ++= (for (i <- arr if i < 0) yield i)
     |
     |     buf.toArray
     |   }
sigNumArr: (arr: Array[Int])Array[Int]

scala> val a = Array(1, -2, 0, -3, 0, 4, 5);
a: Array[Int] = Array(1, -2, 0, -3, 0, 4, 5)

scala> val b = sigNumArr(a);
b: Array[Int] = Array(1, 4, 5, 0, 0, -2, -3)

scala> b.foreach(println);
1
4
5
0
0
-2
-3
  1. 创建一个由java.util.TimeZone.getAvailableIDs返回的时区集合,并只显示以America/前缀开头的时区,并且有序。
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.asScalaBuffer

scala>

scala> def timeZoneName() = {
     |     val arr = java.util.TimeZone.getAvailableIDs();
     |     val tmp = (for (i <- arr if i.startsWith("America/")) yield {
     |       i.drop("America/".length)
     |     })
     |     scala.util.Sorting.quickSort(tmp)
     |     tmp
     |   }
timeZoneName: ()Array[String]

scala> var c = timeZoneName()
scala> c.foreach(println)

5、设置一个映射,其中包含你想要的一些装备,以及它们的价格。然后根据这个映射构建另一个新映射,采用同一组键,但是价格上打9折。

scala> val map = Map("book"->10, "gun"->18, "ipad"->1000)
map: scala.collection.immutable.Map[String,Int] = Map(book -> 10, gun -> 18, ipad -> 1000)

scala> for((k,v) <- map) yield (k, v * 0.9)
res0: scala.collection.immutable.Map[String,Double] = Map(book -> 9.0, gun -> 16.2, ipad -> 900.0)

6、编写一段WordCount函数,统计传入的字符串中单词的个数

scala> def wordCount(str:String)={
     |     val count = new scala.collection.mutable.HashMap[String, Int]
     |     for(word <- str.split("\\s+"))
     |         count(word) = count.getOrElse(word,0) + 1
     |     count
     | }
wordCount: (str: String)scala.collection.mutable.HashMap[String,Int]

scala> wordCount("wo shi zhong guo ren, wo hen hao")
res1: scala.collection.mutable.HashMap[String,Int] = Map(hao -> 1, guo -> 1, wo -> 2, shi -> 1, ren, -> 1, hen -> 1, zhong -> 1)

7、重复上一个练习,使统计后的单词有序

scala> def wordCount(str:String)={
     |     var count = scala.collection.immutable.SortedMap[String, Int]()
     |     for(word <- str.split("\\s+"))
     |         count += (word -> (count.getOrElse(word,0) + 1))
     |     count
     | }
wordCount: (str: String)scala.collection.immutable.SortedMap[String,Int]

scala> wordCount("wo shi zhong guo ren, wo hen hao")
res0: scala.collection.immutable.SortedMap[String,Int] = Map(guo -> 1, hao -> 1, hen -> 1, ren, -> 1, shi -> 1, wo -> 2, zhong -> 1)

8、 重复前一个练习,使用java.util.TreeMap进行实现,并使之适用于Scala API

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

scala> def wordCount(str:String)={
     |     var count: scala.collection.mutable.Map[String, Int] = new java.util.TreeMap[String, Int]
     |     for(word <- str.split("\\s+"))
     |         count += (word -> (count.getOrElse(word,0) + 1))
     |     count
     | }
wordCount: (str: String)scala.collection.mutable.Map[String,Int]

scala> wordCount("wo shi zhong guo ren, wo hen hao")
res1: scala.collection.mutable.Map[String,Int] = Map(guo -> 1, hao -> 1, hen -> 1, ren, -> 1, shi -> 1, wo -> 2, zhong -> 1)

9、编写一个函数,从一个整型链表中去除所有的零值。

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> def removeZero(nums : List[Int]):List[Int]={
     |     nums.filter(_ != 0)
     | }
removeZero: (nums: List[Int])List[Int]

scala> removeZero(List(3,4,5,6,0,7,0,0))
res6: List[Int] = List(3, 4, 5, 6, 7)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值