python, How we can do it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

python, How we can do it?

How we can make a program to output this: [[1,1,1,1], [1,1,1,2],...., [2, 2, 2, 1], [2, 2, 2, 2]] using function that call itself, and list with python. and, what is the correct way for this code? https://code.sololearn.com/cZdM9X1R6ZYD/?ref=app

31st Jul 2021, 6:16 AM
Abd Alwahab Alansi
Abd Alwahab Alansi - avatar
1 Answer
+ 2
Your task definition is not fully clear. Do you want to get all possible combinations of 1 and 2 in a list that contains 4 elements? If you want to implement it with recursion, you need to think about the base case. Your function could start with a list (the outside container) that has an empty list inside. Each step of the recursion would add one element, either 1 or 2, to the inner list. So with each step, the number of lists in your container would double up, and their length would grow by one. You reach the base case, when the size of the inner lists is 4. There can be other correct ways to do this, it is just my approach :) https://code.sololearn.com/cdpx5q7uL4mO/?ref=app
31st Jul 2021, 7:05 AM
Tibor Santa
Tibor Santa - avatar