开发者

I need Subsequence of a particular length, in Groovy

开发者 https://www.devze.com 2023-04-02 07:21 出处:网络
I have a Groovy code like this to find the subsequence : def number = \"248\" as List def number1= num开发者_StackOverflow社区ber.subsequences()

I have a Groovy code like this to find the subsequence :

def number = "248" as List
def number1= num开发者_StackOverflow社区ber.subsequences() 

Which outputs :

[[8], [2, 4, 8], [2, 8], [2], [4, 8], [4], [2, 4]]

Now what I want is a subsequence of a particular length. Lets say for example I want only the subsequence of length 3, then for our example we need to get only [2, 4, 8] as output.

How to do this in groovy?

Thanks in advance.


You should be able to use findAll:

    number.subsequences().findAll { it.size() == 3 }
0

精彩评论

暂无评论...
验证码 换一张
取 消