Groovy将每5个字符与一个字符分开
我需要一个groovy脚本在每10个唯一数字后插入一行分隔符 例如: 输入:Groovy将每5个字符与一个字符分开,groovy,Groovy,我需要一个groovy脚本在每10个唯一数字后插入一行分隔符 例如: 输入: 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 0 7 8 9 七, 输出: 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 0 /////////////// 7 8 9 七, 大宗报价 单向: 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 Inte
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
七, 输出: 1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
七, 大宗报价 单向:
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
}
到目前为止您尝试了什么?您的输入数据格式是什么?文件
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