Search code examples
pythonlistintminimax

Generate a list of lists that add to a given int (game of Nim)


I am new to Python/Computing science - (and also to this site). I tried to search this up but couldn't find how to do it. This is part of a assignment that I have to do. (Game of Nim).

Basically, I have to split a given integer number (e.g: 6) into it's all possible combinations that can add to it, in a list of lists - but NOT duplicates.

So, for example, for 6, the function would generate: [[1,5],[2,4]] for 10, it would generate: [[9,1],[8,2],[7,3],[6,4]]

(I would then use these to "dissect" them more, using a binary tree, but I think I can do that myself - just need help on this part)

Thanks for your time and help!


Solution

  • This cab be one of the possible helper function.

    # python 3.x
    def solution(number):
        return [[i, number - i] for i in range(number // 2 + 1)]
    
    >>>solution(6)
    [[0, 6], [1, 5], [2, 4], [3, 3]]
    
    >>>solution(10)
    [[0, 10], [1, 9], [2, 8], [3, 7], [4, 6], [5, 5]]