# How is modulo used and how does it determine which team?

Hello to all, I didnt really get how this modulo calculation goes. Could someone shed some light on it for me please?

How will the remainder of a division tell me which team I am on if it doesnt even mention how many people in total are being divided by the 4 groups?

63 Likes

Recall how we learned long division in around grade 4.

``````   ______
15 ) 230
``````

Which we would read, fifteen into two-hundred-thirty. The first step would be to divide 15 into 23.

``````      1
------
15 ) 230
- 15
------
8
``````

We see it goes one time, so we multiply 1 times 15, and subtract that from 23. This leaves 8. We next bring down the zero in 230, and divide again.

``````      15
------
15 ) 230
- 15
------
80
- 75
-----
5
``````

In the language of the day we would have read this as, âfifteen goes into two-hundred-thirty, fifteen times with five remainder.â

The remainder is the modulo of a division operation. We often see `%` described as the `remainder operator`.

``````    230 % 15  =>  5
``````
46 Likes

I get what Modulo is and how it is calculated, but I donât get why I would be on team number 3 if I was number 27. If I was number 27 and each team had 4, then I should be on team number 7 shouldnât I? The instructions seem to imply that my_team is equal to my team number. Instructions below:

Youâre trying to divide a group into four teams. All of you count off, and you get number 27.

1. Find out your team by computing 27 modulo 4. Save the value to `my_team` .

2. Print out `my_team` . What number team are you on?

3. Food for thought: what number team are the two people next to you (26 and 28) on? What are the numbers for all 4 teams?

80 Likes

Yeah, itâs not teams of 4, itâs 4 teams, but you still need to know the total number to tell you anything. This is a poorly written exercise. I understand how modulo and division works. Just not why the question is phrased the way it is, as itâs impossible to answer. Iâve submitted a bug report.

280 Likes

Its not just the exercise that is poorly written. The whole lesson is.

âIf the number is divisible, then the result of the modulo operator will be 0.â
What does that even mean? Divisible by what? 5 /2 in python will still give you 2.5 so 5 is still divisible by 2 it wont just be an int.

âThe modulo operator is useful in programming when we want to perform an action every nth-time the code is run.â
How? wonât be that treated via a loop and an if statement?

38 Likes

Thank you guys, someone just explained me it last night and now I get the logics behind it. So I will leave in here an easy way of understanding the logics behind it as well as where I was mixing things up.

Basically I was thinking âHow can I know which team will I be if I dont know how many people we are in total?â. The division I was doing was, if for example we have 100 people, we divide them 100 by 4 and the first 25 people will go into group 1, the next 25 (from 26 until 50) into group 2, the next 25 (from 26 til 75) into group 3 and the remaining people into group 4.

The explanation I was given is that this calcultion should be based on logics, and always the most effective way of doing things is also the most simple of it. The way I was doing it needed an extra answer (total number of people) but the optimal way of doing it would be for example when in physical education class the teacher divides the students into teams. So letâs say 10 kids divided into 3 teams. The teacher wouldnt divide 10 by 3, many times he wouldnât even need to count the total number of kids. He would simply say: âYou go into team number one, now you, the next in line, go into team number 2, next one into team number 3, now you again into team number 1, next into team number 2, next one into team number 3, now you again into team number 1, next one into team number 2 and next one into team number 3âŚ wait, thatâs 9 kids but there is one child remaining, so you go into team number 1â.

So again, the most effective way of doing something is usually the simple way, and by having to ask extra questions to be able to solve the problem you are already adding a degree of complication to it.

264 Likes

These were both written a bit confusing, but they make sense if you understand the math/syntax.

On your first example, when it says divisible, it means only divisible into integers/whole numbers. 5 % 2 = 2 + 2, modulo/remainder of 1. So even though technically you can do 5/2 and receive a float of 2.5, if you wanted to use the remainder for something, you could perform 5 % 2 to figure that out.

On your second example, itâs saying that if you wanted to run your program for example, 100 times, but you wanted it to take action every 5 times, you could check to see if your number is an even multiple of 5. Like so:

for x in 100:
#what to do every time.
if x % 5 == 0:
#what to do every 5th time

So since 20 % 5 == 0, when you get to the 20th iteration, it would run a command. Same at 25, 30, etc.

60 Likes

Thanks, this makes total sense now! Every 4th kid is on team 4, every 3rd kid is on team 3, etc.

31 Likes

âdivide a group into four teamsâ is not equal â4 people in each teamâ. This question would made sense if it changed to â4 people in each groupâ. If so, you were in team 7: 27//4=6;27%4=3.

It DOES needs total people for the original question as you mention.

6 Likes

Youâre trying to divide a group into four teams. All of you count off, and you get number 27.

Find out your team by computing 27 modulo 4. Save the value to `my_team` .

Each group of four kids, taken in order of count off, will each be on a different team.

Not quite. Starting anywhere in the sequence, every fourth kid will be on the same team. There is no âevery 3rdâ or every 2nd. The whole thing revolves around division by 4. The remainder determines which team a player is on.

``````MOD    TEAM
1       A
2       B
3       C
0       D
``````

What are all the MOD 1?

``````1 5 9 13 17 21 25 29
``````

What are all the MOD 2?

``````2 6 10 14 18 22 26 30
``````

What are all the MOD 3?

``````3 7 11 15 19 23 27 31
``````

What are all the MOD 0?

``4 8 12 16 20 24 28 32``
73 Likes

exactly - if one were to say every 3rd kid will be in team 3, that would suggest that kid-6 would be in team 3 (3;6;9;12;15;âŚ), however this is not true as we are working with 4 groups/teams (4;8;12;16;20;24;28;âŚ), therefore the correct statement would be, âevery 3rd kid within each grouping of 4 will be in team 3â i.e. kid 7 is in the second grouping of 4 and is representing the third âquartileâ/group, and is therefore in group 3.

4 Likes

Is there a way for Python to easily calculate the team assignments?

No. Python is like any computerâŚ dumb. We tell the computer what to do via the Python languge.

6 Likes

Well, thatâs what I mean. Codecademy asks this as âfood for thoughtâ. Just wondered what the program would look like to calculate this.

2 Likes

Can we do it with Python? Probably yes. Does Python do this? Probably, no. Python is a tool not a solution.

3 Likes

Youâre trying to divide a group into four teams. All of you count off, and you get number 27.

âCount offâ means the first of the total amount of kids declares, 1, the second declares 2, so on and so forth. You are the 27th kid in a linear lineup of an unknown total of kids in a class, and the teacher has to make 4 groups. This exercise uses the modulo to spit out some remainder, and use that as your team number, Modulo will only ever spit out a finite number of results, but for this exercise, weâre merely calculating (your_number_in_the_line_of_children % four_teams). The total is irrelevant, the teacher is just doing a hilarious math hack that will ensure he only gets 4 potential modulo results for his set of kids, and can use the result of the above equation to assign each kid in the line with a team number.

Food for thought: what number team are the two people next to you (26 and 28) on? What are the numbers for all 4 teams?

To determine this, I just copied my own formula that I just used for my own team:

``````#assign me a team number using the following equation with the modulo #operator and print the result (which is 3, because 27/4 is 6, remainder 3)
my_team = (27 % 4)
print(my_team)

#since we're just going to copy our formula for kids in the 26th slot and the #28th slot, I'll just define some variables like we did above and print the results #to determine their team number
kid_26 = (26 % 4)
kid_28 = (28 % 4)

print(kid_26, kid_28)
``````
59 Likes

Thank you for this, Now I understand what the question means.

Iâm not a native English Speaker and I was stuck because of this.

Hopefully CodeCademy can take into consideration that lots of students wonât be natives and explain things like this with sentences instead of two words.

I believe this will make the learning experience better for everyone.

15 Likes

I am a native speaker and this exercise confused the heck out of me. The explanation lacked depth, and there is only one example to build off of. Tis a great waste of 30 minutes of my life XD.

29 Likes

Thanks man, this make a whole lot of sense now

LOL it finally clicked, First off 27 is your number out of a unknown number of people. An easy way to understand this is if it was worded as such: You are divided into 4 groups with an unknown amount of people. You are given number 27 out of X number of people which group were you assigned to? Which is group three. If at some point your number was 64 then you would be in group: 0, 0 is technically a number and part of the 4 groups 0,1,2 and 3. I hope this helps.

21 Likes