我需要一个常规脚本在每10个唯一数字后插入一个分隔线
示例:
输入:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7
输出:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
/////////////
7
8
9
7
Blockquote
答案 0 :(得分:0)
def arr = [1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,9,0,7,8,9,7]
def map = [:]
arr.each{
println it
map[it] = (map.containsKey("$it") ? map[it] : 1)+1
if( map.size() == 10 ){
println "/////"
map = [:]
}
}
答案 1 :(得分:0)
一种方法:
def numbers = """\
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7""".readLines().collect { it as Integer }
def unique = [] as Set
def seen = [0] as Set // 0 - don't add delimiter at start
def result = numbers.inject([]) { acc, val ->
def s = unique.size()
// if we are on a 10 multiple of unique numbers seen
// and we have not already added a delimiter for this multiple
if (!(s % 10 || s in seen)) {
acc << '///////////////'
seen << s
}
unique << val
acc << val
}
// result will now contain the original list of numbers,
// interleaved with the delimiter every 10th unique number
result.each {
println it
}
其结果是:
~> groovy solution.groovy
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
7