[Tutor] problem solving with lists: final (amateur) solution

marcus.luetolf at bluewin.ch marcus.luetolf at bluewin.ch
Sat Jun 25 14:45:37 EDT 2022


Hello Experts, hello dn,
it's a while since I - in terms of Mark Lawrence - bothered you with my
problem.
Thanks to your comments, especially to dn's structured guidance I've come up
with the code below, based on repeatability.
I am shure there is room for improvement concerning pythonish style but for
the moment the code serves my purposes.
A commented version can be found on
https://github.com/luemar/player_startlist.

def startlist(all_players, num_in_flight):
    c_all_players = all_players[:]
    history = {'a':[], 'b':[],'c':[],'d':[],'e':[],'f':[],'g':[],'h':[],\
               'i':[],'j':[],'k':[],'l':[],'m':[],'n':[],'o':[],'p':[]}
    print('...............day_1................')
    def day_1_flights():
        key_hist = list(history.keys())
        c_key_hist = key_hist[:]
        for dummy_i in c_key_hist:
            print('flights_day_1: ', c_key_hist[:num_in_flight])
            for key in c_key_hist[:num_in_flight]:
                [history[key].append(player)for player in
c_all_players[0:num_in_flight]]
            del c_key_hist[:num_in_flight]
            del c_all_players[0:num_in_flight]
    day_1_flights()
    
    def day_2_to_5_flights():
        flights = {}
        for i in range(2,6):
            print('...............day_' + str(i)+'................')
            flights['a_flight_day_'+str(i)]= []
            flights['b_flight_day_'+str(i)]= []
            flights['c_flight_day_'+str(i)]= []
            flights['d_flight_day_'+str(i)]= []            
            lead = list('abcd')                   
            flight_list = [flights['a_flight_day_'+str(i)],
flights['b_flight_day_'+str(i)],\
                           flights['c_flight_day_'+str(i)],
flights['d_flight_day_'+str(i)]]

            for j in range(len(flight_list)):            
                def flight(cond, day):
                    for player in all_players:
                        if player not in cond:
                            day.extend(player)
                            cond.extend(history[player])
                            history[lead[j]].extend(player)
                    day.extend(lead[j])
                    day.sort()
                    [history[pl].extend(day) for pl in day[1:]]
                    return lead[j]+'_flight_day_'+str(i)+ ': ' +
str(flight_list[j])
                   
                conditions = [history[lead[j]], history[lead[j]] +
flights['a_flight_day_'+str(i)],\
                              history[lead[j]] +
flights['a_flight_day_'+str(i)] + \
                              flights['b_flight_day_'+str(i)], \
                              history[lead[j]] +
flights['a_flight_day_'+str(i)] + \
                              flights['b_flight_day_'+str(i)]+
flights['c_flight_day_'+str(i)]] 
                print(flight(list(set(conditions[j])), flight_list[j]))
    day_2_to_5_flights()
startlist(list('abcdefghijklmnop'), 4)

 Many thanks, Marcus.

-----Ursprüngliche Nachricht-----
Von: Tutor <tutor-bounces+marcus.luetolf=bluewin.ch at python.org> Im Auftrag
von dn
Gesendet: Mittwoch, 25. Mai 2022 22:02
An: tutor at python.org
Betreff: Re: [Tutor] problem solving with lists: preliminary solution

Hi Marcus,

This is a bit of 'blast from the past' - and I haven't gone 'back' to look
at our previous correspondence/this message hasn't "threaded".

May I offer a few criticisms and suggestions:-


On 26/05/2022 05.19, marcus.luetolf at bluewin.ch wrote:
> ....sorry, forgott correct e_mail address.....
> 
> Hello Experts, hello dn,

and not forgetting @wlfraed who mentioned university research papers dealing
with the intricacies (and insolubles) of the "SGP". Did you try following-up
on any of those?


> In resuming my task to write code for a special constellation of the 
> "SocialGolferProblem" (SGP) :
> number_of_days  (number_of_weeks) = 5
> number_of_flights (number_of_groupings)  = 4 seize of flight 
> (players_per_grouping) = 4

Criticism: although each of these terms is perfectly understandable, and
easily converted into a 'readable' pythonic identifier, later the code uses
"n" (for example) - which of the two values commencing "*n*umber_"
is it?


> and given the solution below (draws for week 1 through 5) I've come up 
> with a code of which I only post part of it (for it gets to big):
> for day 1( hardcoded)  and for day 2 (as a function to be used through 
> day 5).

The word "hardcoded" immediately stopped me in my tracks!

The whole point of using the computer is to find 'repetition' and have the
machine/software save us from such boredom (or nit-picking detail in which
we might make an error/become bored).

That said, may I suggest that you grab a whiteboard/blackboard, or a
convenient wall and a bunch of Post-It notes, and try to solve the problem
manually - first for week-one (which is largely trivial), but then recording
the 'history' and moving-on to the extra decision-layer for week-two. If
your brain doesn't go 'click'*, go on to deal with week-three and see how
the algorithm develops...

* keeping my 'pocket handkerchief' lawn tidy doesn't require 'heavy
machinery', but after many years of service my little electric mower 'died'.
I bought a new Bosch model which needed to be partly assembled from the box.
The accompanying manual featured explanations in dozens of languages.
However, the (single/all-language) diagram showing where the back-wheels
were to be joined to the axles featured (only) the English
explanation: "Click". Perhaps other languages do use the word (?spelling),
but the term "going click in your mind" has long been used for the idea
explained in today's idiom as an "ahah! moment".


> The crucial part of my functions for day 2 to 5 are the conditional 
> statements. These conditional statements do not follow a consistent
pattern.
> This inconsistency casts some doubt on the effectiveness of my code 
> below and I'd appreciate critical comments.
> 
...
>     a_flight_day_1 = []
>     b_flight_day_1 = []
>     c_flight_day_1 = []
...
>     history = {'a':[],
>
'b':[],'c':[],'d':[],'e':[],'f':[],'g':[],'h':[],'i':[],'j':[],'k':[],'l':[]
,'m':[],'n':[],'o':[],'p':[]}
...

Following-on from talking about looping (repetition), yes we need to use
multiple conditional expressions to ensure that history is taken into
account (consistently).

The other 'side' of both of these code-constructs is the data-construct.
Code-loops require data-collections! The hard-coded "a" and "day_1" made me
shudder.
(not a pretty sight - the code, nor me shuddering!)

Would you benefit from spending a little time, putting aside the SGP for a
moment, and looking at a tutorial on "nesting" lists (and
dictionaries) in Python?


Again, the above-mentioned 'whiteboard' exercise may help 'reveal' the
patterns in the data, as well as identifying an algorithm.


Sadly, the 'hard-coded' parts may 'help' sort-out week-one, but (IMHO) have
made things impossibly-difficult to proceed into week-two (etc).

(and I'm back to mentioning that I haven't searched for our previous
discussions - specifically whether we talked-about a data-construct for
'history', and also to referring-back to the 'whiteboard' suggestion)


We were warned!

The SGP can be made to work for this particular combination of
weeks/flights/players. Which gives credence to the first-thought: "it looks
so easy". However, the wider problem is more complex than one at-first
imagines!

This is the reason why 'combinatorial problems' are so interesting. Did I
say "interesting"? Perhaps "frustrating" and/or "frustratingly difficult to
solve" - or even: 'cannot (always) be solved'!

This is no 'little kitten' or some old piece of rope, you have "a tiger by
the tail"...
--
Regards,
=dn
_______________________________________________
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