2019独角兽企业重金招聘Python工程师标准>>>

package com.learn/**
* Created by zhuqing on 2017/7/7.
*/
object LearnScala02 {
def main(args: Array[String]): Unit = {
println(slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
println(rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
println(rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
println(removeAt(1, List('a, 'b, 'c, 'd)))
println(insertAt('new, 2, List('a, 'b, 'c, 'd)))
println(range(4, 9))
println("P23 (**) Extract a given number of randomly selected elements from a list.")
println(randomSelect(3, List('a, 'b, 'c, 'd, 'f, 'g, 'h)))
println(randomSelect(3, List('a, 'b, 'c, 'd, 'f, 'g, 'h)))
println(randomSelect(3, List('a, 'b, 'c, 'd, 'f, 'g, 'h)))
println("=========================================================================")println(lotto(6, 49))println(randomPermute(List('a, 'b, 'c, 'd, 'e, 'f)))println(combinations(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g)))println(group3(List("Aldo", "Beat", "Carla", "David", "Evi", "Flip", "Gary", "Hugo", "Ida")))println(group(List(2, 3, 4), List("Aldo", "Beat", "Carla", "David", "Evi", "Flip", "Gary", "Hugo", "Ida")))
println(group(List(2, 2, 5), List("Aldo", "Beat", "Carla", "David", "Evi", "Flip", "Gary", "Hugo", "Ida")))println(lsort(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o))))println(lsortFreq(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o))))}/**
* P18 (**) Extract a slice from a list.
*/
def slice(start: Int, end: Int, list: List[Symbol]): List[Symbol] = {
list.take(end).drop(start)
}/**
* P19 (**) Rotate a list N places to the left.
*
* @param nth
* @param list
*/
def rotate(nth: Int, list: List[Symbol]) = {
if (nth > 0) {
list.drop(nth) ::: list.take(nth)
} else if (nth < 0) {
list.takeRight(-nth) ::: list.dropRight(-nth)
} else {
list
}
}/**
* P20 (*) Remove the Kth element from a list.
*
* @param nth
* @param list
* @return
*/
def removeAt(nth: Int, list: List[Symbol]): (List[Symbol], Symbol) = {
(list.take(nth) ::: list.drop(nth + 1), list(nth))
}/**
* P21 (*) Insert an element at a given position into a list.
*
* @param nth
* @param list
* @return
*/
def insertAt(sym: Symbol, nth: Int, list: List[Symbol]): List[Symbol] = {
(list.take(nth) :+ sym) ::: list.drop(nth)
}/**
* P22 (*) Create a list containing all integers within a given range.
*
* @param start
* @param end
* @return
*/
def range(start: Int, end: Int): List[Int] = {
(start to end).toList
}/**
* P23 (**) Extract a given number of randomly selected elements from a list.
*
* @param size
* @param list
* @return
*/
def randomSelect(size: Int, list: List[Symbol]): List[Symbol] = {
var res = List[Symbol]()
var temp = list
for (i <- (0 until size)) {
val rindex = Math.random() * temp.length
res = res :+ temp(rindex.toInt)
temp = remove(rindex.toInt, temp)
}res
}def remove(nth: Int, list: List[Symbol]): List[Symbol] = {
list.take(nth) ::: list.drop(nth + 1)
}/**
* P24 (*) Lotto: Draw N different random numbers from the set 1..M.
*
* @param size
* @param max
* @return
*/
def lotto(size: Int, max: Int): List[Int] = {
var res = List[Int]()
var i = 0;
while (i < size) {
var random = Math.random() * max
if (!res.contains(random.toInt)) {
res = res :+ random.toInt
i = i + 1
}
}res
}/**
* P25 (*) Generate a random permutation of the elements of a list.
*
* @param list
* @return
*/
def randomPermute(list: List[Symbol]): List[Symbol] = {
var res = List[Symbol]()
var temp = listfor (i <- (0 until list.size)) {
val index = Math.random() * temp.size
res = res :+ temp(index.toInt)
temp = remove(index.toInt, temp)}
res}/**
* P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list.
* In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient). For pure mathematicians, this result may be great. But we want to really generate all the possibilities.
*
* @param size
* @param list
* @return
*/
def combinations[T](size: Int, list: List[T]): List[List[T]] = {
var temp = List[List[T]]()
var res = List[List[T]]()
for (i <- (0 until list.size - size + 1)) {
temp = temp :+ List[T](list(i))
}for (i <- (0 until size - 1)) {
temp = combinations(temp, list)
}temp.toStream.filter((_.size == size)).toList}def combinations[T](seed: List[List[T]], list: List[T]): List[List[T]] = {
var res = List[List[T]]()
for (i <- (0 until seed.size)) {
val itemOfTemp = seed(i)
val start = list.indexOf(itemOfTemp(itemOfTemp.size - 1)) + 1
for (j <- (start until list.size)) {
res = res :+ (itemOfTemp :+ list(j))
}
}
res}/**
* P27 (**) Group the elements of a set into disjoint subsets.
* a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities.
*
* @param list
* @return
*/
def group3(list: List[String]): List[List[List[String]]] = {
var res = List[List[List[String]]]()var temp = this.initGroup(2, list)
for (item <- temp) {
val t = remove(list, item)
val tem = this.combinations(3, t)
for (i <- tem) {
res = res :+ (item :+ i)
}
}temp = res;
res = List[List[List[String]]]()
for (item <- temp) {
val t = remove(list, item)
val tem = this.combinations(4, t)
for (i <- tem) {
res = res :+ (item :+ i)
}
}
res}def remove[T](resource: List[T], remove: List[List[T]]): List[T] = {
resource.filter(item => {
var res = true
for (re <- remove if re.contains(item)) {
res = false
}
res
}).toList
}/**
* P27 (**) Group the elements of a set into disjoint subsets.
*
* @param sizes
* @param list
* @return
*/
def group(sizes: List[Int], list: List[String]): List[List[List[String]]] = {
var temp = initGroup(sizes.head, list)
var res = List[List[List[String]]]()for (size <- sizes.drop(1)) {
res = List[List[List[String]]]()
for (item <- temp) {
val leftList = remove(list, item)
val tem = this.combinations(size, leftList)
for (sub <- tem) {
res = res :+ (item :+ sub)
}
temp = res
}}res
}def initGroup[T](size: Int, list: List[T]): List[List[List[T]]] = {
var res = List[List[List[T]]]()
val grouped = this.combinations(size, list)
for (item <- grouped) {
res = res :+ List[List[T]](item)
}
res
}/**
* P28 (**) Sorting a list of lists according to length of sublists.
* a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of the list according to their length. E.g. short lists first, longer lists later, or vice versa.
*
* @param list
* @return
*/
def lsort[T](list: List[List[T]]): List[List[T]] = {
list.sortWith(_.length < _.length)
}/**
* P28 (**) Sorting a list of lists according to length of sublists.
* b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements according to their length frequency; i.e. in the default, sorting is done ascendingly, lists with rare lengths are placed, others with a more frequent length come later.
*
* @param list
* @tparam T
* @return
*/
def lsortFreq[T](list: List[List[T]]): List[List[T]] = {
var temp = list.map(item => {
(item.length, item)
})var countMap = scala.collection.mutable.Map[Int, Int]()
for (item <- temp) {
countMap(item._1) = countMap.getOrElse(item._1, 0) + 1
}val sortLength = countMap.toList.sortWith(_._2 < _._2)
var res = List[List[T]]()
for (item <- sortLength) {
res = res ::: list.filter(_.length == item._1)
}
res
}}

转载于:https://my.oschina.net/u/587323/blog/1239937

scala N99(18-28)相关推荐

  1. scala学习手记28 - Execute Around模式

    我们访问资源需要关注对资源的锁定.对资源的申请和释放,还有考虑可能遇到的各种异常.这些事项本身与代码的逻辑操作无关,但我们不能遗漏.也就是说进入方法时获取资源,退出方法时释放资源.这种处理就进入了Ex ...

  2. 2013年7月16日 18:28:32 GPS应用

    今天看完一部漫画,,,怎么说呢,总体的结果就是又呗感动了呢~ 一眼看是黄漫,但撸不起来那种,能给人有青春触动和爱情反思,以及一点点人性引思;还有很多很多吧.反正我这也许表达不完全的人可能没说完全,反正 ...

  3. 18.28 getchar()函数与缓冲区问题

    一个关于使用链表增加删除人名的小程序,在使用getchar()函数,得到输入的菜单选项时,出现了问题,现记录如下: [菜单部分代码如下:] #include <stdio.h>int ma ...

  4. Scala开发入门教程

    出处:http://blog.csdn.net/mapdigit/article/details/21878083 Scala语言和其它语言比如Java相比,算是一个比较复杂的语言,它是一个面向对象和 ...

  5. Scala 学习笔记

    Scala 学习笔记 1 object func_exp { 2 println("Welcome to the Scala worksheet") //> Welcome ...

  6. Chisel教程——08.Chisel参数化生成器(从Scala讲起)

    Chisel参数化生成器(从Scala讲起) 动机 要想使得Chisel模块成为代码生成器,就必须要有一些东西来告诉生成器如何执行这个工作.这一节会介绍模块的参数化,涉及多种方法和Scala语言特性. ...

  7. 使用Scala编写Spark程序求基站下移动用户停留时长TopN

    使用Scala编写Spark程序求基站下移动用户停留时长TopN 1. 需求:根据手机基站日志计算停留时长的TopN 我们的手机之所以能够实现移动通信,是因为在全国各地有许许多多的基站,只要手机一开机 ...

  8. MySQL【环境搭建 01】Linux root 用户部署 mysql-5.7.28 及 not allowed to connect to this MySQL server 和中文乱码问题处理

    云盘资源:mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 链接:https://pan.baidu.com/s/1DmavSo3kCKOPZtmd-FE8mw 提 ...

  9. Spark中Data skew(数据倾斜)Java+Python+Scala三种接口完整代码

    起因 代码中shuffle的算子存在的地方,groupByKey.countByKey.reduceByKey.join等 判断一个算子是shuffle算子可以通过[20] 出现的问题有两种 ①大部分 ...

最新文章

  1. 面试题:2018最全Redis面试题整理
  2. 深度学习~生成式对抗神经网络GAN
  3. 005 NsPack 1.4 之附加数据初探
  4. 渲染性能测试 , 结果比想象中好很多.
  5. 我的电脑不联网,很安全!黑客:你还有风扇呢
  6. 重构-改善既有代码的设计 (该书写于1999)培训之一
  7. UITableView使用总结和性能优化
  8. Python随机梯度下降法(三)
  9. 高科技加持,升哲科技助力打造首都智慧社区
  10. 阿里云祝顺民:因云而生的云原生网络
  11. springboot日志写入mysql_springboot运用logback将日志写入数据库
  12. VMware12虚拟机安装教程
  13. 2022达摩院青橙奖公布:15位青年学者多半从事基础研究,4位女科学家获奖破纪录...
  14. 蚂蚁金服出品,这个企业级前端应用框架你值得拥有
  15. Win10+Ubuntu双系统修复Ubuntu系统引导
  16. 《环太平洋》(pacific rim)观后感
  17. Tomcat部署及安装
  18. 使用pgpool-ii 搭建postgresql 高可用、负载均衡架构
  19. 全国互联电网调度管理规程(第一章__第九章)
  20. 计算机在机电一体化应用,关于计算机技术在机电一体化专业中的应用探讨

热门文章

  1. Eclipse创建java webproject配置Tomacat和JDK
  2. orale客户端与数据库连接
  3. Tech.Ed 2008
  4. 4/5 MySQL入门总结:数据表(TABLE)操作
  5. Python 查看服务器磁盘信息
  6. 让你的PHP也能执行JS并获得JS函数的返回值
  7. Web.xml配置Error Page不能够转发的问题分析及解决
  8. Java 集合框架 : Collection、Map
  9. poj 2388 排序的水题
  10. Coolite(二)服务器端Alert,Confirm,Prompt