[Tutor] problem solving with lists
Dennis Lee Bieber
wlfraed at ix.netcom.com
Mon Apr 4 16:47:40 EDT 2022
On Mon, 4 Apr 2022 20:53:51 +0200, <marcus.luetolf at bluewin.ch> declaimed
the following:
>Hello Experts,
>Francesco Loffredo's solution looks very clean.
>It returns 15 sublists/teams in final, but 20 are required, for each item in text has to combine with
>every other item in 4 sublists/teams 5 times.
Which is the correct result IF one does not restart the combinations
for each week. Restarting the combinations allows subsequent weeks to
select "teams" that were excluded in previous weeks.
I've never modified the ooREXX code to restart, and to treat "current
week" differently from "previous weeks"...
C:\Users\Wulfraed\Documents\_Hg-Repositories\REXX>letters_set.rex 16 4
Accepted items: 15
abcd aefg ahij aklm
anop behk bfil bgjm
ceim cfhn cgko cjlp
dejn dfkp dghl
C:\Users\Wulfraed\Documents\_Hg-Repositories\REXX>
That (my ooREXX) code is only looking for common pairs over the entire
period, and does not account for "player can not repeat within current
week". I suspect the same is true for the referenced code.
The Python version with both
Constraint: all players are assigned to groups each week;
ie, no player may appear in multiple groups in the week
Constraint: no pairs of players may repeat from week to week
(a solution may mean some players are never paired)
C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs\SGP>main.py 4 4 5
Week # 0: abcd efgh ijkl mnop
Week # 1: aeim bfjn cgko dhlp
Week # 2: afkp belo chin dgjm
Week # 3: agln bhkm cejp dfio
Week # 4: ahjo bgip cflm dekn
C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs\SGP>
The main computation (no input parsing/output formatting)
-=-=-
from itertools import combinations
LETTERS = string.ascii_letters + string.digits
def main(num_groups, group_size, num_weeks):
schedule =[ [ set() for _ in range(num_groups)] for _ in
range(num_weeks)]
for wn, week in enumerate(schedule):
#initialize generator of groupings at start of each week, so items
#skipped in previous week are again candidates
group_gen = combinations(LETTERS[:group_size * num_groups],
group_size)
for gn, group in enumerate(week):
while True:
accept = True
candidate = set(next(group_gen))
#first filter for no duplicate players in this week
for agroup in week[:gn]:
if not candidate.isdisjoint(agroup): #if candidate &
agroup:
accept = False
break
#filter for no duplicate pairs in previous weeks
if accept:
for aweek in schedule[:wn]:
for agroup in aweek:
if len(candidate & agroup) > 1:
#repeated pair
accept = False
break
if not accept: break
if accept:
#candidate has not been rejected, add it to current
week
week[gn] = candidate
break
return schedule
-=-=-
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list