P18 (**) Extract a slice from a list
来源:互联网 发布:乎肉的做法大全 编辑:程序博客网 时间:2024/04/29 17:55
Given two indices, I and K, the slice is the list containing the elements from and including the Ith element up to but not including the Kth element of the original list. Start counting the elements with 0.
Example:
scala> slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))res0: List[Symbol] = List('d, 'e, 'f, 'g)
//18def slice[A](n1:Int,n2:Int,ls:List[A])={ val len=ls.length ls.drop(n1).dropRight(len-n2)}
也可以用drop和take结合的方法,
ls drop s take (e - (s max 0))
也可以用递归的方法
def slice2[A](n1:Int,n2:Int,ls:List[A]):List[A]=(n1,n2,ls) match{ case(_,_,Nil) =>Nil case(_,n2,_) if(n2<=0) =>Nil case(n1,n2,h::tail) if(n1<=0)=>h::slice2(0,n2-1,tail) case(n1,n2,h::tail) =>slice(n1-1,n2-1,tail)}
参考答案
0 0
- P18 (**) Extract a slice from a list
- P18 (**) Extract a slice from a list.
- P23 (**) Extract a given number of randomly selected elements from a list.
- Extract channel information from a video
- How to extract .obj from a .lib
- jar - user of jar command to list/extract file from a jar file
- extract the x509 ssl certificate from a pcap file
- A value is trying to be set on a copy of a slice from a DataFrame
- remove a loop from a linked list
- codeforces A.Extract Numbers
- decruft(A library to extract meaningful data from a webpage) 源码分析
- 问题解决:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
- Extracting Unique Elements From a List
- C++: delete a node from linked list
- Extract raw SOAP Message returned from Foreign Web Service after Deserialization Failure in a Web Service
- My excersize program used to extract English words from a library
- Extract the shortest summary contains all the keywords from a article
- Extract lowercase masked FASTA from a BLAST database with masking information
- Java____内部类学习整理
- 多个授权文件共存的正确方法
- intltool 的安装!!
- 20M 虚机介绍
- Python 列表的清空
- P18 (**) Extract a slice from a list
- esper之相关概念
- CRC校验之模2除法(学习笔记、备忘)
- Java中Object转化为int类型
- How to extend the root partition in LVM
- MongoDB使用中的一些问题
- oracle数据库中,实例、表空间、用户、方案、表、数据的关系例子
- 数组内容为随机1~7,不重复
- put()函数把数据顺序写入文件