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?

11 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
7 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?

24 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.

56 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?

8 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.

54 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.

6 Likes

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

5 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
17 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.

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.

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.

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)
8 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.

2 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.

7 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.

1 Like