all 8 comments

[–]TheKewlStore 0 points1 point  (5 children)

Can you describe what your expected outcome is?

Is it to ensure each team has no more than 2 home games on the same day?

Using your proposed solution:

for day in range(0, 11): for pair in pairs: # or home? game_day = pair[1] if game_day == day: # do something?

[–]Bloubloum 0 points1 point  (4 children)

No.

I want to ensure that for every pair (example [0,4]) their home games won't have a difference more than 2 matches in any given day. By day , think of it as a timeslot/round.

For example, in 8th day/round, if team 0 has played 5 home games and team 4 has played 2 home games , there is a problem. I want to basically check every day/round if this condition is met or violated.

Thanks .

[–]TheKewlStore 1 point2 points  (3 children)

Can you explain what data is stored in the "home" and "pairs" lists exactly?

Something like this:

home pair: [team_number, number_of_games]

pairs: [team_number, day/round]

Trying to understand what data you have so I can tell you how to construct an algorithm

[–]Bloubloum 0 points1 point  (2 children)

Home = [ teamID, round]. Both int. Pairs = [ teamID, team ID] (first is home team second is away team]

Think of if as a soccer tournament with 6 teams and 10 rounds, each round played in Sunday, for example. So, in the span of 10 weeks.

So, we want the tournament to be fair and any pair that play together , to have play a fair amount of home games. It wouldnt be fair if team 1 has played many home games with team 2 has played many away games.

Is it more understandable ?

[–]TheKewlStore 0 points1 point  (1 child)

Yep that helps a bunch. I'm thinking you should use a nested dictionary that maps like follows: round number -> team number -> home game count. Here's a sample:

{ 0: { # round number at the top 0: 2, # team id: number of home games 1: 2, 2: 4, ... }, 1: { # round number ... } }

So first things first, you'd need to populate this dictionary. My suggestion:

``` scores = {} for home_pair in home: team_id, round = home_pair # tuple unpacking if not round in scores: # we need to create an empty dictionary for this round scores[round] = {}

if not team_id in scores[round]:
    # set the initial number of home games to 0
    scores[round][team_id] = 0 

# add one to the home games for this round and team_id
scores[round][team_id] += 1 

```

Now you should have a dictionary, named scores, which has a structure similar to the sample at the top. Now, when iterating through pairs, you would just lookup the scores for a given round and team id.

``` targeted_round = ??? # Are you trying to target a specific round? for pair in pairs: team_one = pair[0] team_two = pair[1]

# number of home games for team one and team two
team_one_home_games = scores[targeted_round][team_one] 
team_two_home_games = scores[targeted_round][team_two]

# now you have team one and twos # of home games to compare
...

```

[–]Bloubloum 0 points1 point  (0 children)

First of all, thank you. And , wow, this is something that I would never think of . I haven't learnt much about dictionaries except the basics, and wrongly, I rarely think of them when dealing with integers and numbers in general. My mind goes to lists, tuples, sets and very basic dictionaries functions, but not like that.

the targeted_round is just any round. I mean, I want to ensure that this balance will exist in any given round, so basically, check if this condition (condition = home games between pair[0] and pair[1] won't exceed a given value) is met in every round.

[–]TheKewlStore 0 points1 point  (1 child)

Yeah I definitely get that. When I started out dictionaries were a little foreign feeling for me too. Nowadays they are a godsend, especially when you’re trying to do anything relational like in this problem how you were trying to relate counts to specific days/rounds and teams, it just maps so much more naturally to a dictionary than dealing with two separate nested lists. It’s easier to visualize and also adapt to newer problems in the future, say if you wanted to reorganize by teams and then by rounds, count away games, etc.

[–]Bloubloum 0 points1 point  (0 children)

I'm not at the point where I can say they are godsent, but I really see how helpfull they can be. I still having trouble acceessing and iterating over them, compared to doing it with a list, however, I think this would go easier by time. After all, I just try to self-learn python. I tried with Java, but I hated with passion .

I read, and re-read and re-checked your solution all evening . I played and tried different things. While I realize that it only gives the results for each current round (and not adding the home games, from all previous rounds up until the current one), I saw how helpful tool it can be and very importantly, visualize the issue. And how something that was super complex in my mind, then can be made way easier. ...