Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy将每5个字符与一个字符分开_Groovy - Fatal编程技术网

Groovy将每5个字符与一个字符分开

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

我需要一个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 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