package com.learn
/**
*
*
* Created by zhuqing on 2017/7/6.
*
*/
object LearnScala01 {
def main(args: Array[String]): Unit = {
println(last(List(1, 1, 2, 3, 5, 8)))
println(penultimate(List(1, 1, 2, 3, 5, 8)))
println(nth(2, List(1, 1, 2, 3, 5, 8)))
println(length(List(1, 1, 2, 3, 5, 8)))
println(reverse(List(1, 1, 2, 3, 5, 8)))
println(palindrome(List(1, 1, 2, 3, 5, 8)))
println(palindrome(List(1, 2, 3, 2, 1)))
println(flatten(List(List(1, 1), 2, List(3, List(5, 8)))))
println(compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
println(pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
println(encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
println(encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
println( decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e))))
println( encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
println(duplicate(List('a, 'b, 'c, 'c, 'd)))
println(duplicateN(3,List('a, 'b, 'c, 'c, 'd)))
println( drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
println(split(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)))
}
/**
* P01 (*) Find the last element of a list.
* Example:
* scala> last(List(1, 1, 2, 3, 5, 8))
* res0: Int = 8
*
* @param list
* @return
*/
def last(list: List[Int]): Int = {
list.last
}
/**
*P02 (*) Find the last but one element of a list.
*
* @param list
* @return
*/
def penultimate(list: List[Int]): Int = {
list.takeRight(2).head
}
/**
* P03 (*) Find the Kth element of a list.
*
* @param index
* @param list
* @return
*/
def nth(index: Int, list: List[Int]): Int = {
list.take(index + 1).last
}
/**
* P04 (*) Find the number of elements of a list.
*
* @param list
* @return
*/
def length(list: List[Int]): Int = {
list.length
}
/**
* P05 (*) Reverse a list.
*
* @param list
* @return
*/
def reverse(list: List[Int]): List[Int] = {
list.reverse
}
/**
* P06 (*) Find out whether a list is a palindrome.
* @param list
* @return
*/
def palindrome(list: List[Int]): Boolean = {
if (list.isEmpty || list.length == 1) {
true
} else {
if (list.head != list.last) {
false
} else {
palindrome(list.drop(1).dropRight(1))
}
}
}
/**
* P07 (**) Flatten a nested list structure.
*
* @param list
* @return
*/
def flatten(list: List[Any]): List[Any] = {
var res = List[Any]();
for (item <- list) {
item match {
case list: List[Int] => res = res ++ flatten(list)
case x: Int => res = res :+ x
case _ => println("error")
}
}
res
}
/**
*P08 (**) Eliminate consecutive duplicates of list elements.
*
* @param list
* @return
*/
def compress(list: List[Symbol]): List[Symbol] = {
var res = List[Symbol]();
for (s <- list) {
if (!res.contains(s)) {
res = res :+ s
}
}
res
}
/**
* P09 (**) Pack consecutive duplicates of list elements into sublists.
* @param list
* @return
*/
def pack(list: List[Symbol]): List[List[Symbol]] = {
var res = List[List[Symbol]]()
var sub = List[Symbol]()
for (symbol <- list) {
if (sub.isEmpty || sub.contains(symbol)) {
sub = sub :+ symbol
} else {
res = res :+ sub
sub = List[Symbol](symbol);
}
}
res :+ sub
}
/**
* P10 (*) Run-length encoding of a list.
* @param list
* @return
*/
def encode(list:List[Symbol]):List[(Int,Symbol)]={
var res = List[(Int,Symbol)]()
val packList = pack(list)
for(packs <- packList){
res = res :+ (packs.length,packs.head)
}
res
}
/**
* P11 (*) Modified run-length encoding.
* @param list
* @return
*/
def encodeModified(list:List[Symbol]):List[Any]={
var res = List[Any]()
val packList = pack(list)
for(pack <- packList){
if(pack.length==1){
res = res:+pack.head
}else{
res = res :+ (pack.length,pack.head)
}
}
res
}
/**
* P12 (**) Decode a run-length encoded list.
* @param list
* @return
*/
def decode(list:List[(Int,Symbol)]):List[Symbol]={
var res = List[Symbol]()
for((len,symbol)<-list){
for( i <- 0 until len){
res = res :+symbol
}
}
res
}
/**
*P13 (**) Run-length encoding of a list (direct solution).
* @param list
* @return
*/
def encodeDirect(list : List[Symbol]):List[(Int,Symbol)]={
var res = List[(Int,Symbol)]()
var lenSym = (0,list.head)
for(symbol <- list){
if(lenSym._2 == symbol){
lenSym = (lenSym._1+1,symbol)
}else{
res = res :+ lenSym
lenSym = (1,symbol)
}
}
res :+ lenSym
}
/**
* P14 (*) Duplicate the elements of a list.
* @param list
* @return
*/
def duplicate(list:List[Symbol]):List[Symbol] = {
var res = List[Symbol]()
list.foreach(sys=> res = res :+sys :+sys)
res
}
/**
* P15 (**) Duplicate the elements of a list a given number of times.
* @param n
* @param list
* @return
*/
def duplicateN(n:Int,list:List[Symbol]):List[Symbol]={
var res = List[Symbol]()
list.foreach(sys=> {
for(i <- 0 until n)
res = res :+ sys
})
res
}
/**
* P16 (**) Drop every Nth element from a list.
* @param nth
* @param list
* @return
*/
def drop(nth:Int,list:List[Symbol]):List[Symbol]={
list.take(nth-1) ::: list.drop(nth)
}
/**
* P17 (*) Split a list into two parts.
* @param n
* @param list
* @return
*/
def split(n:Int,list:List[Symbol]):(List[Symbol],List[Symbol])={
(list.take(n),list.drop(n))
}
}
ScalaN99(1-17)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 小学语文修改病句的方法 修改病句是小学语文考试中常见的题型,在修改病句之前,我们应该清晰的了解有哪些病句现象,下面...