Help with Python combinations [Solved] | SoloLearn: Learn to code for FREE!

+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.

2/26/2021 10:42:12 PM

LethalArms

+2

Done it thank you all. https://code.sololearn.com/cHv64BoCgpQt/?ref=app

+7

check it ðŸ‘‡ðŸ‘‡ðŸ‘‡ https://code.sololearn.com/cVjsIpc3hVmt/?ref=app

+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)))

+3

+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 ^^

+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

+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.

+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

+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.

+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

+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

+2

Mohd Ayan Ansari post your question in new thread , not under someone else question.

+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

+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.

+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

+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

+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 ??

+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

+1

I think thats it!! I just need to implement an array creator depending of an input and will work how I wanted, Thks!!

+1