Modulo

I dont understand how modulo actually assigns the group team that a person would be in.

in this exercise the task is to use modulo to determine which team I would be in. However, modulo is just a operation that gives the remainder. Meaning that the information given shows simply that the number of individuals to choose from is not evenly divisible by the number of teams.

my_team = 27 % 4
print(my_team)
my_team2 = 26 % 4
print(my_team2)
my_team3 = 28 % 4
print(my_team3)

i understand it to mean that with 27 people and 4 teams there will be three people left over without a team.

26 people and 4 teams = 2 people without a team.

but 28 people and 4 teams = 0 people without a team.

am I missing something or is this question worded poorly?

Let’s for all practical purposes stipulate that there will be a number of people divisible by 4 so each team is the same size, and there are four of them.

Give me a minute to re-examine your question…

Now we can write,

player_number % 4

and always get a number less than 4, as in 0, 1, 2, or 3.

If we add 1, we get the team number the person with that player number will be on.

Bottom line, the number of people is fixed, and can be divided equally into four teams. It is the player number that determines which of the four teams they play for.

where player_number is from a linear sequence, 1 thru 28.

Thank you @mtf.

so any number between 1 and 28 should give a remainder between 0 and 3?

1 Like

Yes, always.


Alternate.

When no addition takes place, the sequence we get from a range will be, (note, the range starts with 1)

[1, 2, 3, 0, 1, 2, 3, 0, ...]

When we add 1 the fourth value in the sequence becomes 1.

[2, 3, 4, 1, 2, 3, 4, 1,...]

The sequence now begins on 2 instead of 1. What if we wish to impose the requirement that the sequence always starts with 1, and we are not using any addition?

We would use logic to replace the 0 with a 4. There are no 4s in general sequence so this will not interfere with the list.

player_number % 4 or 4

P. S. Have you covered lists and functions, yet?


What happens if we give the first player the number 0, and the last player the number 27 (just for experimentation, mind)?

team_sequence = []
for x in range(28):
    team_sequence.append(x % 4)

Now we have a sequence that looks like this,

[0, 1, 2, 3, 0, 1, 2, 3, ...]

Adding 1 now makes perfect sense.

[1, 2, 3, 4, 1, 2, 3, 4, ...]

No logic involved. Only problem is we have a player number 0, which would be rather humiliating.

How would we work around that, without using the aforementioned logic?