Listing combinations WITH repetitions in Scala

后端 未结 7 898
盖世英雄少女心
盖世英雄少女心 2021-01-12 01:18

Trying to learn a bit of Scala and ran into this problem. I found a solution for all combinations without repetions here and I somewhat understand the idea behind i

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 01:26

    This solution was posted on Rosetta Code: http://rosettacode.org/wiki/Combinations_with_repetitions#Scala

    def comb[A](as: List[A], k: Int): List[List[A]] = 
        (List.fill(k)(as)).flatten.combinations(k).toList
    

提交回复
热议问题