Groovy Number Sequence

How to get the sequence of a given number in Groovy, for example:

def number = 169 // need a method in groovy to find the consecutive numbers that is, 1,6,9,16,69,169 // not 19! 

Groovy has a subsequences() method, but it definitely doesn't. Can someone tell me how can I do this in Groovier? Or is there a built-in method?

+4
source share
6 answers

Despite the fact that at the end of the game this solution is less complicated than @tim, it will do the trick:

 def str = 169 as String def result = [] as SortedSet (0..<str.length()).each { i -> (i..<str.length()).each { j -> result << str[i..j].toInteger() } } 

Edit:

The code works like two nested loops that iterate over the string representation of a number and extract various substrings from it.

The outer loop represents the starting index of the substring, and the inner loop represents the ending index of the substring. The outer loop will go from beginning to end of the line, while the inner loop starts from the current start index and goes from there to the end.

as SortedSet ensures that as a result there are no duplicate numbers and that numbers are sorted in ascending order.

  1 6 9 ----- 0 1 2 <-- index ===== [1]6 9 (i=0; j=0) [1 6]9 (i=0; j=1) [1 6 9] (i=0; j=2) 1[6]9 (i=1; j=1) 1[6 9] (i=1; j=2) 1 6[9] (i=2; j=2) 
+3
source

Run it in the Groovy console

 def number = 169 number = number.toString() as List def results = [] 0.upto(number.size()) {numDigits -> for(int startPos = 0; startPos + numDigits < number.size(); startPos++) { def result = number[startPos..startPos + numDigits] results << result.join().toInteger() } } assert results == [1, 6, 9, 16, 69, 169] 
+4
source

Taking the Don answer above (which works great), I came up with a little Groovy -er version of the same:

 def number = 181 number = number.toString() as List def results = (0..<number.size()).inject([]) { res, numDigits -> res.addAll( (0..<number.size()-numDigits).collect { startPos -> number[startPos..startPos + numDigits].join() as int } ) res } println results 
+3
source

There is no need to switch from Integer to String to List, and then back to String with join (), since String already behaves like any sequence:

 // Sorry for the silly name. Couldn't think of anything better :) def subInts(num) { def str = num as String (1..str.length()).inject([]) { res, size -> res += (0..str.length() - size).collect { i -> str[i..< i + size] as int } } } assert subInts(169) == [1, 6, 9, 16, 69, 169] 

In addition to a small number of conversions, this is a copy of tim_yates solution . Hope this helps.

+1
source

Extremely algorithmic ... You can do something like this:

 num = 169 ar = [] while ( num >= 1 ) { str = num.toString() ar.add(str[str.indexOf('.')-1]) num = num/10.toInteger() } len = ar.size() for ( i in 1..len-1 ) { for (j in 0..len-i-1) { str = "" for ( k in 0..i) { str += ar.get(j+k) } ar.add(str) } } 

I think this should work.

0
source

Oneliner

 (0..<n.size()).inject([]) {a,b -> (0..<n.size()-b).each {a << (n[it] as Integer)};a} 
-1
source

All Articles