+ 1

Can you help me with a simple solution for my homework in python?

I have to generate X_NNNN_YY as “X” have to be the letter “P” “ _ “ have to be space “NNNN” have to be four numbers between (0,9) and the whole 4-digit number can be divided by 6 “_” must be space “YY” have to be two letters between this list [‘A’, ‘B’, ‘C’, ‘E’] JUST DID THIS BUT IT’S NOT EXACTLY WHAT I AM LOOKING FOR: alphabets=[‘A’, ‘B’, ‘C’, ‘E’] Numbers = [chr(i) for i in range(org(‘0’), ord(‘9’)+1)] Combinations=[one + two + three + four + five + six for one in Numbers for two in Numbers for three in Numbers for four in Numbers for five in alphabets for six in alphabets] print(Combinations) I’m new at python and I will be very grateful If you suggest some ideas 😇

4th Apr 2020, 2:11 PM
Nikkie
Nikkie - avatar
3 Antworten
+ 1
from itertools import combinations_with_replacement as cwr lst = ['P '+str(n).zfill(4) for n in range(0,10000,6)] cmb = [*cwr('ABCDE',2)] cmb = [s+' '+a+b for s in lst for a,b in cmb] print('\n'.join(cmb)) # reach timeout limit before print ends in code playground ^^
4th Apr 2020, 6:16 PM
visph
visph - avatar
0
give it a try first, then ask for help. https://www.sololearn.com/discuss/1316935/?ref=app
4th Apr 2020, 2:22 PM
John Robotane
John Robotane - avatar
0
You’re very close! You don’t need to manually build every digit or character. Since the required format is: P NNNN YY Here is the sample solution you can follow by the way : alphabets = ['A', 'B', 'C', 'E'] results = [] for num in range(0, 10000): # 0000 to 9999 if num % 6 == 0: # must be divisible by 6 four_digits = f"{num:04d}" # format as 4-digit string for first in alphabets: for second in alphabets: code = f"P {four_digits} {first}{second}" results.append(code) for r in results: print(r)
14th Nov 2025, 3:59 PM
Avinash Ranjan