[Tutor] problem solving with lists
marcus.luetolf at bluewin.ch
marcus.luetolf at bluewin.ch
Sun Mar 6 02:15:31 EST 2022
Hello Experts,
I'am still biting my teeth out trying to put in to code the following
pseudocode:
1.Set up a list of 16 letters and assign it to a variable : all_letters =
list(abcdefghijklmnop)
2.Find an code to insert all 16 letters in 5 Lists in 4 sublists of 4
letters in each list:
list1 = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'k'].....['m', 'n', 'o',
'p']]
list2= [['a', 'e', 'f','g'] ...............,['d', 'i', 'l',' p']]
.
.
list5 =[[...],....................,[.........]]
with constraint that a pair of letters, p.e. ['a', 'b', ..] can occur
only once in all 20 sublists
or if list1 contains sublist ['a', 'b',..], such a sublist cannot appear
in list2 through list5.
To write this code,
as a first step I made a list (comb_list) containing all possible sublists
and reducing their number to 80
by sorting them : ['a', 'b'..] equals ['b', 'a', ..] :
with to code snippet below.
>comb_list = []
>for dummy_i in range(4):
>copy_all_letters = all_letters[:]
>lst = (16*16)*copy_all_letters
>f1 = []
>f2 = []
>f3 = []
>f4 = []
> f1.append(lst[0+n])
>f1.append(lst[2+n])
>f1.append(lst[4+n])
>f1.append(lst[6+n])
> f1.sort()
>comb_list.append(f1)
>f2.append(lst[8+n])
>f2.append(lst[10+n])
>f2.append(lst[12+n])
>f2.append(lst[14+n])
>f2.sort()
>comb_list.append(f2)
> f3.append(lst[1+n])
>f3.append(lst[3+n])
>f3.append(lst[5+n])
>f3.append(lst[7+n])
>f3.sort()
>comb_list.append(f3)
> f4.append(lst[9+n])
>f4.append(lst[11+n])
> f4.append(lst[13+n])
> f4.append(lst[15+n])
>f4.sort()
> comb_list.append(f4)
> print(f1, f2, f4, f4)
> print(comb_list)
> n += 3
The next step should be to extract out of the 80 sublists in comb_list 20
sublists fullfilling the above mentioned constraint.
a) I don't know how to do that, b) I am not shure wether this is a way do
it, nor c) if my problem can by solved by python at all.
Marcus.
More information about the Tutor
mailing list