[Tutor] problem solving with lists

Alan Gauld alan.gauld at yahoo.co.uk
Wed Mar 9 16:19:04 EST 2022


On 09/03/2022 19:39, marcus.luetolf at bluewin.ch wrote:

> for a sublist of length 3 and a list of 9 letters (all_letters =
> list('abcdefghi'))
> create 4 lists ( = (len(all_letters)- 1)/2),  each containing 3 sublists
> (9/3) for a total of 12 sublists.

> In the first part of my algorithm I created sublists of all possible letter
> combInations: 
> 
>> all_letters = list('abcdefghi')

all_letter = 'abcdefghi'  # no need to create a list

>> chars = []
>> for index, letter in enumerate(all_letters):  
>>    l1 = []   # initialize once at top of loop
>>    l2 = []
>>    l3 = []
>>    l4 = []
>>    copy_all_letters = all_letters[:]
>>    del(copy_all_letters[index])
>>    lst = copy_all_letters

You could just have done:
      lst = all_letters[:]
      del(lst[index])

>>    l1.append(lst[0])
>>    l1.append(lst[1])
>>    l1.append(letter)
>>    l1.sort()
>>    chars.append(l1)

chars = [[a,b,c]]

>>    l2.append(lst[2])
>>    l2.append(lst[3])
>>    l2.append(letter)
>>    l2.sort()
>>    chars.append(l2)

chars = [[a,b,c],[a,d,e]]

>>    l3.append(lst[4])
>>    l3.append(lst[5])
>>    l3.append(letter)
>>    l3.sort()
>>    chars.append(l3)

chars = [[a,b,c],[a,d,e],[a,f,g]]

>>    l4.append(lst[6])
>>    l4.append(lst[7])
>>    l4.append(letter)
>>    l4.sort()
>>    chars.append(l4)

chars = [[a,b,c],[a,d,e],[a,f,g],[a,h,i]]

After the first iteration.
Is that correct? Is it what you want it to contain?

After the first 4 iterations:

chars = [
    [a,b,c],[a,d,e],[a,f,g],[a,h,i],
    [a,b,c],[b,d,e],[b,f,g],[b,h,i],
    [a,b,c],[c,d,e],[c,f,g],[c,h,i],
    [a,b,d],[c,d,e],[d,f,g],[d,h,i]
]

You already have 12 sublists but you still have another 5 loop
iterations to go, so you will wind up with 9 groups of 4
sublists, 36 in total. I don't think that's what you wanted?


>> print(chars, len(chars))

This should have highlighted the issue, if indeed it is an issue.

>> all_chars = []
>> for i in chars:

so i represents a sublist above. sub_list might have been clearer...

>>    if i not in all_chars:
>>        all_chars.append(i)

So that removed duplicates?
Maybe create a set of sublists instead?

>> print('all_chars: ', all_chars, len(all_chars))
> 
> In a second step I should create sub_sub_lists containing> all possble pairs

They aren't really sub_sub_lists though. They are
completely separate lists of pairs.


> of letters (['a', 'b'], ['a', 'c'] ..... ['i', 'h'] ,
> a total of len(all_letters) -1 * len(all_letters) = 72 and eliminating
> sub_sub_lists with equal pairs when 'a', 'b' = 'b', 'a'.

One of the itertools modules methods should be able to
do this for you.

So let's assume:

pairs = itertools.some_magic_here(all_chars...)

> In a third step append to a final list only sublists not already containing
> one of the sub_sub_lists		

Let's reword that to sublists of chars containing sublists of pairs.

> 
> It is this third step I am unable to code efficiently, avoiding to hardcode
> it.

Efficiently is always a dangerous word. What constitutes efficient?
How do you measure it? How do you know whether an algorithm is
"efficient" or not?

Start with the simple approach(pseudo code - you may
want some helper functions for the tests):

result = []
used_pairs = []
for sub_list in chars:
    for pair in pairs:
        if pair in sub_list and pair not in used_pairs:
           result.append(sub_list)
           used_pairs.append(pair)
           break  # next sublist??? may need more here???

I think, I'm not 100% confident I understood that step.
That's why it helps to see the pencil & paper outcome.
ie. what does correct look like?

Can you post your manual solution for each of the three steps?
Even 9 letters might be two much, try reducing it to 6 letters
if that's the case.

> In a last step the code (if it works properly including border cases) should
> be changed in a function for use with a
> length of sublists of 4 and number of letters of 16 or other combinations if
> a solution is possible.

Seems to me each step could (should?) be a function...

> Finally the letters will be sustituted by words.

I have no clue what that means! Which words? Where do they come from?
Is it a master dictionary perhaps with letters corresponding to words?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list