-2

I am trying to solve this coding problem with no luck. The problem is like this: I need to create a function that receives two arguments. The first is a list that consists n number of string values. The second argument is the amount of values I want in the returned list. My challenge is to create a new list of lists with the maximum amount of combinations possible with k strings in each list. I think using an example will best explain this:

symbols = ['stop', 'bomb', 'moon']
def create_deck(symbols, k):

create_deck(symbols,2)

I expect the function to return:

[['stop', 'bomb'], ['stop', 'moon'], ['moon', 'bomb']]

which is the maximum amount of combinations while k=2. Meaning two words per list.

I dont have any code to present because the furthest I got is a mathematical equation that represents the solution:

nCr = n! / r! * (n - r)!

but no code.

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

0

itertools.combinations should do the trick:

import itertools
def create_deck(symbols, k):
    return [list(x) for x in itertools.combinations(symbols, k)]
Mureinik
  • 297,002
  • 52
  • 306
  • 350