+ 1

Help with Python combinations [Solved]

I'm trying to make a program that given 2 user inputs it will generate all the possible combinations based on that input. For example, 1st input is 6 and 2nd input is 2, the program will search all combinations, something like this 1,1,1,1,1,1 1,1,1,1,1,2 1,1,1,1,2,1 1,1,1,2,1,1 I've tried in some ways and with some examples of the internet, but all of them the output is like 1,2,2,2,2,2 doesn't do 1,2,1,1,1,1 2,3,3,3,3,3 doesn't do 2,1,1,1,1,1 and that is the main key for the problem So to short it, the game is about moving objects with a determine object that will make him move in certain direction, so the 1st input is how many movements it can do, and the 2nd input how many objects it has to allow movement. So the program will check all combinations it can do with the number of movements given by the game (0 to x) and with the objects also given by the game (0 to y) Any help is good, also if any suggestion how to speed it up and reduce the combinations is welcome Thanks in advance! 1024 char rechead.

26th Feb 2021, 10:42 PM
LethalArms
LethalArms - avatar
28 Answers
27th Feb 2021, 8:55 PM
LethalArms
LethalArms - avatar
+ 3
# no need to write your own: from itertools import product width = int(input('width? ')) count = int(input('count? ')) pro = [*product(range(1,1+count),repeat=width)] print() for p in pro: print(','.join(map(str,p)))
27th Feb 2021, 10:58 AM
visph
visph - avatar
+ 3
no need to write your own... check my previous answer ;)
27th Feb 2021, 11:07 AM
visph
visph - avatar
+ 3
array = lambda len: [0]*len # is enough as array creator... # use: array(3) return [0,0,0] # but the most efficient way would be to use built-in functions: product will return a generator like object, so both memory footprint and efficiency is maximized ^^
27th Feb 2021, 11:19 AM
visph
visph - avatar
+ 3
I'm gonna check the code from abhay, and all the array creating suggestions, thank you all for the help, when I have a final code I will link it here
27th Feb 2021, 1:18 PM
LethalArms
LethalArms - avatar
+ 3
LethalArms user can choose array size , look at meas1 function , meas2 was only for 6. also the code by sandeep prints in reverse but if it worked fine for 3,3 and 5,3 it should work fine for others as well.
1st Mar 2021, 11:40 AM
Abhay
Abhay - avatar
+ 2
LethalArms something like this ? for i in range(6): a=[1,1,1,1,1,1] if(i+1>=2): a[-i]=2 print(a) If not ,can you tell what will it output for inputs 6,3 or 8,2
26th Feb 2021, 11:38 PM
Abhay
Abhay - avatar
+ 2
LethalArms Your 3,3 explanation of possible combination is actual example of combinations than compared to 6,2 one which was confusing for me . There is combinations function from built in itertools package for that purpose, that you might want to check out . I will add an example later on. Edit: ok seems like combination won't really work here. though there is another way to do it , print(list((i,j,k) for i in range(1,3+1) for j in range(1,3+1) for k in range(1,3+1))) for 6,2 https://code.sololearn.com/c3o791tYcX3s/?ref=app I can't just arrange it to output according to user input but i think sandeep code will work fine.
27th Feb 2021, 11:17 AM
Abhay
Abhay - avatar
+ 2
Abhay, your code is really cool tho, if you find a way of getting the output according to user input, it will be awesome
27th Feb 2021, 8:56 PM
LethalArms
LethalArms - avatar
+ 2
Abhay I took a look into your code and it is amazing, can you explain what it does exactly? Since I need the program to send only one number of the combinations, so the program for each combination sends each number of the combination, I was able to do that with Sandeep code but I think it doesn't do all the combinations, since I tested it with multiple examples and only some of them worked (probably luck) and o think with your code will work perfectly. With Sandeep code I tried this combinations With 3/3 worked With 5/3 worked With 8/2 doesn't work idk if is the time between clicks that is too fast and its missing the right combination or just the program doesn't do all the combinations I'm gonna link here the repository with the full code of the program so you can understand better what program does, I can't explain it too well: https://www.github.com/lethalarms23/pythonSyncro
28th Feb 2021, 4:43 PM
LethalArms
LethalArms - avatar
+ 2
Mohd Ayan Ansari post your question in new thread , not under someone else question.
28th Feb 2021, 5:31 PM
Abhay
Abhay - avatar
+ 1
Didn't test the code but from what I see the array and the for will be always for 6, but I want if the 1st input is like 20 the array and the for will also be 20
27th Feb 2021, 7:49 AM
LethalArms
LethalArms - avatar
+ 1
LethalArms i need an example of output for input 8,2 or 6,3 , i am not really able to understand what you are looking for . And you can run the code in any python ide or code playground here to check it.
27th Feb 2021, 7:58 AM
Abhay
Abhay - avatar
+ 1
I'm gonna test it the code For a 8 / 2 input Needs to give outputs of 1,1,1,1,1,1,2,2
27th Feb 2021, 8:12 AM
LethalArms
LethalArms - avatar
+ 1
I've tested the code but it's not how I want, I want it to check all combinations with the range of the 2nd input, so if the 2nd input is 3 it will test all the combinations between 1,2 and 3 and the size of the array where it will do the combinations its determined by the 1st input
27th Feb 2021, 8:47 AM
LethalArms
LethalArms - avatar
+ 1
LethalArms so for 6,3 what exactly will be the output ? Something like this 1,1,1,1,1,1 1,1,1,1,1,2 1,1,1,1,1,3 1,1,1,1,3,1 1,1,1,3,1,1 ??
27th Feb 2021, 9:30 AM
Abhay
Abhay - avatar
+ 1
No, for 6,3 will need to be like 1,1,1,1,2,1 / 1,1,1,1,2,2 / 1,1,1,1,2,3 etc
27th Feb 2021, 9:46 AM
LethalArms
LethalArms - avatar
+ 1
I think thats it!! I just need to implement an array creator depending of an input and will work how I wanted, Thks!!
27th Feb 2021, 11:03 AM
LethalArms
LethalArms - avatar
+ 1
mdNaqueebraza MdNaqueebraza don't spam please.
28th Feb 2021, 10:28 AM
Abhay
Abhay - avatar
+ 1
LethalArms ok i modified it to be based on user input , here it uses eval to output the result . I added time to check if it is better than one you have posted . Well it turns out that the recursive solution performs really worst , even for small numbers like 7 , 3 . Use an interactive ide as sololearn won't output anything if it takes more than few seconds. https://code.sololearn.com/c67FELGm6ilD/?ref=app I am using timeit module to run the function 10 times and the final time is cumulative sum of that. And be sure to check out if eval is a right thing or not to use, as it has security concerns but it can be really useful as well in situations like here.
28th Feb 2021, 10:33 AM
Abhay
Abhay - avatar