+ 1

Generate Permutation

Please help me modify my code, I was not able to follow the output that our professor want us to do. Here is my code https://code.sololearn.com/c3ufjV67ofIN/#py This is the problem our prof gave us. Write a python program that generates permutations from a given set taken "r" at a time. Sample Output: Enter the element of the set (S): 2,4,6,3,5 Enter the number of elements taken at a time (r): 2 The number of permutations is 20 (2,4), (4,2), (2,6), (6,2), (2,3), (3,2), (2, 5), (5,2), (4,6), (6,4), (4,3), (3,4), (4,5), (5,4), (6,3), (3,6), (6,5), (5,6), (3,5), (5,3)

21st Apr 2022, 2:49 PM
Jenie
Jenie - avatar
3 odpowiedzi
+ 2
# this can be solved in this way: from itertools import permutations p2 = permutations([2,4,6,3,5], 2) for i in list(p2): print(i)
21st Apr 2022, 3:35 PM
JaScript
JaScript - avatar
+ 1
You need to loop from 0 to X for the first, then from 1 to X, then from 2 to X (where X is the end) and so goes on... Notice the nested loop required for that. You just get pairs of arr[i] and arr[j] and make a tuple. Also notice how 2,2 is not in the sample output, you can start iterations from 1 and get arr[i-1] to omit self repetition.
21st Apr 2022, 3:19 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
Your code works? Maybe simplify the input for testing purposes? https://code.sololearn.com/cCHMgOikiZ1S/?ref=app
21st Apr 2022, 3:28 PM
Lisa
Lisa - avatar