Can this solution be made more efficient?
game_scoring.py
"""
Game Scoring
Imagine a game where the player can score 1, 2, or 3 points depending on the move they make. Write a function or functions,
that for a given final score computes every combination of points that a player could score to achieve the specified score in the game.
Signature
int[][] gameScoring(int score)
Input
Integer score representing the desired score
Output
Array of sorted integer arrays demonstrating the combinations of points that can sum to the target score
Example 1:
Input:
score = 4
Output:
[ [ 1, 1, 1, 1 ], [ 1, 1, 2 ], [ 1, 2, 1 ], [ 1, 3 ], [ 2, 1, 1 ], [ 2, 2 ], [ 3, 1 ] ]
Example 2:
Input:
score = 5
Output:
[ [ 1, 1, 1, 1, 1 ], [1, 1, 1, 2 ], [ 1, 1, 2, 1 ], [ 1, 1, 3 ], [ 1, 2, 1, 1 ], [ 1, 2, 2 ], [ 1, 3, 1 ], [ 2, 1, 1, 1 ], [ 2, 1, 2 ], [ 2, 2, 1 ], [ 2, 3 ], [ 3, 1, 1 ], [ 3, 2 ] ]
"""
from itertools import combinations_with_replacement, product
import sys
sys.dont_write_bytecode = True
def gameScoring(score):
points = [1,2,3]
combos = list()
for L in range(1,score+1):
combos += list(combinations_with_replacement(points, L))
for M in range(len(combos)):
combos += list(product(combos[M], repeat=len(combos[M])-1))
for i,combo in enumerate(combos):
if sum(combo) != score:
combos[i] = None
output = sorted(list(set(combo for combo in combos if combo is not None)))
return [list(out) for out in output]
# These are the tests we use to determine if the solution is correct.
# You can add your own at the bottom.
test_case_number = 1
def check(expected, output):
global test_case_number
result = True
if len(expected) == len(output):
for score in expected:
result = result & (score in output)
for score in output:
result = result & (score in expected)
else:
result = False
rightTick = '\u2713'
wrongTick = '\u2717'
if result:
print(rightTick, ' Test #', test_case_number, sep='')
else:
print(wrongTick, ' Test #', test_case_number, ': Expected ', sep='', end='')
print(expected)
print(' Your output: ', end='')
print(output)
print()
test_case_number += 1
if __name__ == "__main__":
test_1 = 4
expected_1 = [
[1, 1, 1, 1],
[1, 1, 2],
[1, 2, 1],
[1, 3],
[2, 1, 1],
[2, 2],
[3, 1]
]
output_1 = gameScoring(test_1)
check(expected_1, output_1)
test_2 = 5
expected_2 = [
[1, 1, 1, 1, 1],
[1, 1, 1, 2],
[1, 1, 2, 1],
[1, 1, 3],
[1, 2, 1, 1],
[1, 2, 2],
[1, 3, 1],
[2, 1, 1, 1],
[2, 1, 2],
[2, 2, 1],
[2, 3],
[3, 1, 1],
[3, 2],
]
output_2 = gameScoring(test_2)
check(expected_2, output_2)
# Add your own test cases here