I have a mathematical problem that i need to solve using python. (Read full qn pls,)
Problem :-
Find all ways to post 3 letters in 5 postboxes assuming all letters and postboxes are distinct.
I tried to solve it on paper for 2 hours but my brain got roasted, so i decided to do it via python
I tried using itertools product but no success.
Then i tried using nested for loops and got this :-
count = 0
a,b,c,d,e = [],[],[],[],[]
for A in range(1,6):
for B in range(1,6):
for C in range(1,6):
a.clear()
b.clear()
c.clear()
d.clear()
e.clear()
if A == 1:
a.append(A)
elif A == 2:
b.append(A)
elif A == 3:
c.append(A)
elif A == 4:
d.append(A)
elif A == 5:
e.append(A)
if B == 1:
a.append(B)
elif B == 2:
b.append(B)
elif B == 3:
c.append(B)
elif B == 4:
d.append(B)
elif B == 5:
e.append(B)
if C == 1:
a.append(C)
elif C == 2:
b.append(C)
elif C == 3:
c.append(C)
elif C == 4:
d.append(C)
elif C == 5:
e.append(C)
print(f'{str(a).ljust(12)} {str(b).ljust(12)} {str(c).ljust(12)} {str(d).ljust(12)} {str(e).ljust(12)}')
count += 1
print('\nCount :',count)
Output :-
[1, 1, 1] [] [] [] []
[1, 1] [2] [] [] []
[1, 1] [] [3] [] []
[1, 1] [] [] [4] []
[1, 1] [] [] [] [5]
[1, 1] [2] [] [] []
[1] [2, 2] [] [] []
[1] [2] [3] [] []
[1] [2] [] [4] []
[1] [2] [] [] [5]
[1, 1] [] [3] [] []
[1] [2] [3] [] []
[1] [] [3, 3] [] []
[1] [] [3] [4] []
[1] [] [3] [] [5]
[1, 1] [] [] [4] []
[1] [2] [] [4] []
[1] [] [3] [4] []
[1] [] [] [4, 4] []
[1] [] [] [4] [5]
[1, 1] [] [] [] [5]
[1] [2] [] [] [5]
[1] [] [3] [] [5]
[1] [] [] [4] [5]
[1] [] [] [] [5, 5]
[1, 1] [2] [] [] []
[1] [2, 2] [] [] []
[1] [2] [3] [] []
[1] [2] [] [4] []
[1] [2] [] [] [5]
[1] [2, 2] [] [] []
[] [2, 2, 2] [] [] []
[] [2, 2] [3] [] []
[] [2, 2] [] [4] []
[] [2, 2] [] [] [5]
[1] [2] [3] [] []
[] [2, 2] [3] [] []
[] [2] [3, 3] [] []
[] [2] [3] [4] []
[] [2] [3] [] [5]
[1] [2] [] [4] []
[] [2, 2] [] [4] []
[] [2] [3] [4] []
[] [2] [] [4, 4] []
[] [2] [] [4] [5]
[1] [2] [] [] [5]
[] [2, 2] [] [] [5]
[] [2] [3] [] [5]
[] [2] [] [4] [5]
[] [2] [] [] [5, 5]
[1, 1] [] [3] [] []
[1] [2] [3] [] []
[1] [] [3, 3] [] []
[1] [] [3] [4] []
[1] [] [3] [] [5]
[1] [2] [3] [] []
[] [2, 2] [3] [] []
[] [2] [3, 3] [] []
[] [2] [3] [4] []
[] [2] [3] [] [5]
[1] [] [3, 3] [] []
[] [2] [3, 3] [] []
[] [] [3, 3, 3] [] []
[] [] [3, 3] [4] []
[] [] [3, 3] [] [5]
[1] [] [3] [4] []
[] [2] [3] [4] []
[] [] [3, 3] [4] []
[] [] [3] [4, 4] []
[] [] [3] [4] [5]
[1] [] [3] [] [5]
[] [2] [3] [] [5]
[] [] [3, 3] [] [5]
[] [] [3] [4] [5]
[] [] [3] [] [5, 5]
[1, 1] [] [] [4] []
[1] [2] [] [4] []
[1] [] [3] [4] []
[1] [] [] [4, 4] []
[1] [] [] [4] [5]
[1] [2] [] [4] []
[] [2, 2] [] [4] []
[] [2] [3] [4] []
[] [2] [] [4, 4] []
[] [2] [] [4] [5]
[1] [] [3] [4] []
[] [2] [3] [4] []
[] [] [3, 3] [4] []
[] [] [3] [4, 4] []
[] [] [3] [4] [5]
[1] [] [] [4, 4] []
[] [2] [] [4, 4] []
[] [] [3] [4, 4] []
[] [] [] [4, 4, 4] []
[] [] [] [4, 4] [5]
[1] [] [] [4] [5]
[] [2] [] [4] [5]
[] [] [3] [4] [5]
[] [] [] [4, 4] [5]
[] [] [] [4] [5, 5]
[1, 1] [] [] [] [5]
[1] [2] [] [] [5]
[1] [] [3] [] [5]
[1] [] [] [4] [5]
[1] [] [] [] [5, 5]
[1] [2] [] [] [5]
[] [2, 2] [] [] [5]
[] [2] [3] [] [5]
[] [2] [] [4] [5]
[] [2] [] [] [5, 5]
[1] [] [3] [] [5]
[] [2] [3] [] [5]
[] [] [3, 3] [] [5]
[] [] [3] [4] [5]
[] [] [3] [] [5, 5]
[1] [] [] [4] [5]
[] [2] [] [4] [5]
[] [] [3] [4] [5]
[] [] [] [4, 4] [5]
[] [] [] [4] [5, 5]
[1] [] [] [] [5, 5]
[] [2] [] [] [5, 5]
[] [] [3] [] [5, 5]
[] [] [] [4] [5, 5]
[] [] [] [] [5, 5, 5]
Count : 125
It Works!
But the code seems to long. Is there any technique to simplify the code ? (Especially the if-elif part)
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire