+ 1

Is it possible to generate only one output by modifying this python permutation?

There are 11 digits that form a given number string. The first number is 0 followed by 8, then another 0. The 4th number is not an odd number but greater than 5. The 7th number is 1, the 5th and 6th numbers which are greater than 2 have a difference of 1 and the 5th number is greater than the 6th. 9th and 10th numbers are equal and the 8th number is greater than the 11th number. I don't know how to use iterator or any method in python to generate only a number that meets this condition. Can anyone help? https://code.sololearn.com/cU16HT8O3LJ1/?ref=app

21st Jan 2020, 8:13 AM
Idris Shola A.
5 Answers
+ 2
If you would use permutations from itertools, that would greatly simplify your code. from itertools import permutations num = "64001128454" But we'll get into that later. The main problem is about filtering the permutations. According to your description, the resulting number generated should be n[0] == 0 n[1] == 8 n[2] == 0 n[3] > 5 n[3] % 2 == 0 n[4] > 2 n[5] > 2 n[4] - n[5] == 1 n[6] == 1 n[7] > n[10] n[8] == n[9] In a sense, you should be able to do something like... ls perm = [n for n in permutations(num) if (n[0] == 0) && (n[1] == 8) && ... ] I'll test the code and get back to you.
21st Jan 2020, 9:18 AM
Fermi
Fermi - avatar
+ 2
Oh, but yeah. The 24 results I received are all the same. Try adding print([''.join(x) for x in ls_perm]) after generating ls_perm. The number is 08065412441.
21st Jan 2020, 9:48 AM
Fermi
Fermi - avatar
+ 1
Idris Shola A. It works for me! :D Out of the permutations, I received a list of 24 objects which agrees to the conditions. I also met syntax errors on the way though: Python uses 'and' instead of && Remember to convert string to int for numeric comparisons, e.g. int(n[4]) - int(n[5]). For comparing equality, remember to use strings, e.g. n[0] == "0". Code: from itertools import permutations num = "64001128454" ls_perm = [n for n in permutations(num) if n[0] == "0" and n[1] == "8" and n[2] == "0" and int(n[3]) > 5 and int(n[3]) % 2 == 0 and int(n[4]) > 2 and int(n[5]) > 2 and int(n[4])-int(n[5])==1 and n[6] == "1" and int(n[7])>int(n[10]) and n[8]==n[9]] On Termux, it takes a few seconds to process the result. I'm pretty sure Code Playground will timeout.
21st Jan 2020, 9:38 AM
Fermi
Fermi - avatar
0
The number is to be generated from '64001128454'
21st Jan 2020, 8:17 AM
Idris Shola A.
0
@Fermi Thanks for your contribution. I had tried something similar but returned syntax error. Will be waiting for your further response.
21st Jan 2020, 9:28 AM
Idris Shola A.