[Tutor] problem solving with lists

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Mar 25 09:27:12 EDT 2022


On Fri, 25 Mar 2022 16:56:06 +1300, dn <PythonList at DancesWithMice.info>
declaimed the following:

>Further, if we consider that arranging players into a logical order,
>before selection, is also a likely and 'natural' approach; this would
>seem to imply that every time they enter a tournament, 'a' and 'b' will
>play each other in the first game. That may not be a good thing! (may
>not even be seen as "fair" - by those who don't think in statistics)
>
>Accordingly, I'm wondering how 'expensive' it would be to 'throw' all
>the player's names into a 'pool' and make repeated random drawings to
>form the groupings? Each such selection, will either be 'accepted' if
>the player may be added to the group currently being constructed, or
>'rejecting' if the player is not suitable/permitted to be added (and
>returning to the 'pool').
>

	Since "a", "b", etc. are just placeholders, and -- if all tournaments
are based upon the same criteria (4 groups of 4 players for 5 weeks) such
that the "table of placeholders" doesn't change (it becomes a constant in
the code that translates from placeholder to player name) -- all that is
required is to randomize the list of 16 players before mapping them to the
placeholder letters.

"""
random.shuffle(x[, random])

Shuffle the sequence x in place.

<SNIP>

To shuffle an immutable sequence and return a new shuffled list, use
sample(x, k=len(x)) instead.
"""
"""
random.sample(population, k)

Return a k length list of unique elements chosen from the population
sequence or set. Used for random sampling without replacement.

Returns a new list containing elements from the population while leaving
the original population unchanged. The resulting list is in selection order
so that all sub-slices will also be valid random samples. This allows
raffle winners (the sample) to be partitioned into grand prize and second
place winners (the subslices).
"""

players = [	"Lady Gaga", "Arnold Palmer", ..., "George Bush"	]

random.shuffle(players)

lookup = {}
for i, placeholder in enumerate("abcdefghijklmnop"):
	lookup[placeholder] = players[i]

Alternatively, untested:

players = [	"Lady Gaga", "Arnold Palmer", ..., "George Bush"	]

random.shuffle(players)

lookup = {}
for placeholder, player in zip("abcdefghijklmnop", players):
	lookup[placeholder] = player


	Final output would look something like (pseudo-code)

for w, week in enumerate(weeks)::
	print("Week %s" % w+1)
	for t, team in enumerate(week):
		print("\tTeam %s" % t+1)
		for player in team:
			print("\t\t%s" % players[player])


	


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list