How do I get a 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!
There is a method in Groovy called subsequences(), but that is not doing this job exactly. Can anyone say me how can I do this in Groovier way? Or is there any built-in method?
Although late to the game, here’s a solution that is less sophisticated than @tim’s, but also will do the trick:
Edit:
The code works like two nested loops that iterate over the String representation of the number and extracting the various substrings from it.
The outer loop represents the start index of the substring and the inner loop the end index of the substring. The outer loop will go from the beginning to the end of the String, whereas the inner loop starts at the current start index and goes from there to the end.
The
as SortedSetensures that there are no duplicate numbers in the result and that the numbers are sorted in ascending order.