[Tutor] problem solving with lists
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Mar 6 13:05:26 EST 2022
On Sun, 6 Mar 2022 08:15:31 +0100, <marcus.luetolf at bluewin.ch> declaimed
the following:
>
>1.Set up a list of 16 letters and assign it to a variable : all_letters =
>list(abcdefghijklmnop)
>
FYI (presuming you left out quote markers in the string) this isn't
really needed. You can directly index a string, rather than making it a
list of 1-character strings.
>>> sixteen = "abcdefghijklmnop"
>>> lsixteen = list(sixteen)
>>> sixteen, lsixteen
('abcdefghijklmnop', ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p'])
>>> sixteen[3], lsixteen[3]
('d', 'd')
Note how both return the same item.
>>>
>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
Overkill and wasteful of memory -- note that your #2 above reads
"insert", not "reduce". The algorithm shouldn't, in my mind, even generate
an invalid set.
Nested loops with differing increments (skips) may suffice.
Consider the first set of four substrings:
abcd efgh ijkl mnop
Now, how to avoid any matching pairs? Well, you could take the "a", and
one character from the other three substrings
aeim afjn agko ahlp
But you can't do that with "b" -- as beim has "eim" in common. Instead
of matching through "ijkl" you could wrap it -- "jkli". (hint, use a
modulo operator and just increment the position)
bej. bfk. bgl. ghi.
Now you need to handle "mnop" to fill the "."; perhaps the same wrap?
"nopm".
bejn -- NO, conflicts with afjn.
So... an algorithm that needs to determine how much of a circular wrap
each four-character substring needs after the first simple splitting, based
upon which starting character is in play.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list