problem16-20
声明
该系列文章来自:http://aperiodic.net/phil/scala/s-99/
大部分内容和原文相同,加入了部分自己的代码。
如有侵权,请及时联系本人。本人将立即删除相关内容。
P16 (**) Drop every Nth element from a list.
要求
Example:
|
|
方案
- (1) 递归
|
|
P17 (*) Split a list into two parts.
要求
The length of the first part is given. Use a Tuple for your result.
Example:
|
|
方案
- (1) take(n) + drop(n)
|
|
- (2) take(n) + drop(n) == splitAt(n)
|
|
- (3) 普通递归
|
|
P18 (**) Extract a slice from a list.
要求
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:
|
|
方案
- (1) List内置slice(废话)
|
|
- (2) take + drop (实际上,内置slice就是这么实现的)
|
|
P19 (**) Rotate a list N places to the left.
要求
Examples:
|
|
方案
- (1) drop + take
|
|
P20 (*) Remove the Kth element from a list.
要求
Return the list and the removed element in a Tuple. Elements are numbered from 0.
Example:
|
|
方案
- (1) aplitAt
|
|
另一写法:
|
|
- (2) 递归(这个写的很恶心)
|
|
- (3) 递归
|
|