3 of 6
Write a function all_sequences(n, k) that returns all sequences of length k where each element is chosen from 1 to n. Elements can repeat.
Return the sequences in any order.
print(all_sequences(2, 1)) # Output: [[1], [2]] print(all_sequences(2, 2)) # Output: [[1, 1], [1, 2], [2, 1], [2, 2]] print(all_sequences(3, 2)) # Output: [[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]]
At each position you loop through all n choices and pick one:
[]
/ | \
[1] [2] [3] β pick first element
/|\ /|\ /|\
[1,1][1,2][1,3] ... β pick second element
When the sequence reaches length k, add it to the result.
def all_sequences(n, k): # Your code here pass # Test your function: print(all_sequences(2, 1)) # Expected: [[1], [2]] print(all_sequences(2, 2)) # Expected: [[1, 1], [1, 2], [2, 1], [2, 2]] print(all_sequences(3, 2)) # Expected: [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]] print(all_sequences(2, 3)) # Expected: [[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,1],[2,1,2],[2,2,1],[2,2,2]]