[Tutor] problem solving with lists
marcus.luetolf at bluewin.ch
marcus.luetolf at bluewin.ch
Wed Mar 9 14:39:56 EST 2022
Hello Experts,
many thanks for your offer. My problem to solve:
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.
Append all 9 letters to each list, 3 letters to each sublist 4 times.
Apply a constraint that in all 12 sublists a pairing of 2 letters can occur
only once.
P.e. if the first sublist of the first list = ['a', 'b', 'c'] the
combination of 'a','b' or 'a', 'c', or 'b','c'
cannot occur in a sublist in the subsequent lists any more.
A solution exists only for a specific lenght of sublists and number of
letters:
(number of letters -1)%(length of sublist -1) == 0
In the first part of my algorithm I created sublists of all possible letter
combInations:
>all_letters = list('abcdefghi')
>l1 = []
>l2 = []
>l3 = []
>l4 = []
>chars = []
>for index, letter in enumerate(all_letters):
> copy_all_letters = all_letters[:]
> del(copy_all_letters[index])
> lst = copy_all_letters
> l1.append(lst[0])
> l1.append(lst[1])
> l1.append(letter)
> l1.sort()
> chars.append(l1)
> l2.append(lst[2])
> l2.append(lst[3])
> l2.append(letter)
> l2.sort()
> chars.append(l2)
> l3.append(lst[4])
> l3.append(lst[5])
> l3.append(letter)
> l3.sort()
> chars.append(l3)
> l4.append(lst[6])
> l4.append(lst[7])
> l4.append(letter)
> l4.sort()
> chars.append(l4)
> l1 = []
> l2 = []
> l3 = []
> l4 = []
>print(chars, len(chars))
>all_chars = []
>for i in chars:
> if i not in all_chars:
> all_chars.append(i)
>print('all_chars: ', all_chars, len(all_chars))
In a second step I should create sub_sub_lists containing all possble 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'.
In a third step append to a final list only sublists not already containing
one ot the sub_sub_lists
It is this third step I am unable to code efficiently, avoiding to hardcode
it.
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.
Finally the letters will be sustituted by words.
I hope I made myself clear, Marcus.
-----Ursprüngliche Nachricht-----
Von: Tutor <tutor-bounces+marcus.luetolf=bluewin.ch at python.org> Im Auftrag
von Alan Gauld via Tutor
Gesendet: Montag, 7. März 2022 12:24
An: tutor at python.org
Betreff: Re: [Tutor] problem solving with lists
On 07/03/2022 07:44, marcus.luetolf at bluewin.ch wrote:
> I should have written ... c) if my problem can by solved with python
> or any other programming language at all.
Python, or any other "Turing complete" programming language can represent
any algorithm you come up with. So the answer is yes, once you have the
algorithm.
> It's the process of finding an algorithm to solve my task I am stuck
> with
And that is often the hardest bit.
> I'am very gratefull for your advice to reduce the task to 9
> items/letters in sublists of 3 (soem sort of recursion ?) but it
> doesn't get me any further.
Can you solve it by hand? - ie. using a pencil and paper.
If not then you don't really understand your problem yet.
So you need to write down all the different conditions that must be met,
including all the exceptional cases that might arise.
Once you understand what needs to be done you should be able to work out how
to do it using pencil and paper.
That will give you an algorithm. It may not be the most efficient, and it
will likely not be generalized to "N sublists from M" elements yet. But it
is a start.
But unless you understand the solution at that level you are unlikely to be
able to write a program to solve it. (Unless it's a swell known algorithm
and you can find a library. But it looks as if your requirements are
slightly different from the more common cases)
We can certainly help you formulate the required algorithm, and possibly
some shortcuts. But you need to give us a complete and precise description
of the problem.
--
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
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list