BYU logo Computer Science

Assign Teams Transcript

Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.

[00:00:00] Instructor: Hi! Let’s go over the team assignments activity. So community members want to register for the rec center sports teams and you have a dictionary that maps age groups to team names. So you want to map a list of tuples that contain a name and an age to a list of tuples containing a name and a team.

[00:00:20] And we’ll give you a function that computes a participant’s age group, which is going to be the nearest multiple of three that is less than, or equal to the participant age. And then if the participant does not have an age group or assignment, then they’ll go on a team adult league. In fact, that’s kind of a boring name. Let’s call them the Old Gogies. All right. There you go.

[00:00:46] So what do we do? Let’s look at the data here. So I have a dictionary that maps age groups, team toddlers, tiny troopers, starting stars, mighty middle, too cool, boundless. And then all the old people come after that. And I have a little function here that given a specific age will return the age group.

[00:01:07] The math here, right? This is int divide.

[00:01:10] So let’s say I pass in an eight as age. Eight int divide by three, gives me a two multiplied back by three gives me a six. All right. If I pass in seven in divide by three, it gives me two multiplied by three, gives me a six.

[00:01:25] If I pass in six in divide by three, it gives me a two multiplied by three, gives me a six, right? So you can see how I can take six, seven, eight and put those all together as six or 9, 10, and 11 and those all return nine, right? So this is the little mathematical pattern in programming for rounding down to the nearest multiple if you will.

[00:01:48] And so given an age, I can get the age group. Now, I can find the team that the age group belongs to. And so here’s my participants and I need to be able to figure out which team, each one of these individuals belongs to and print that out.

[00:02:05] So let’s set this up. I’m going to have a mapping pattern again, transform, right? I’ve got a list of things. I want a new list. So we’ll call a new list teams and we’ll just print teams out when we’re done. And so now I want to map participants to team. So, you know, for each participant, which is a name and an age. Now I’m going to do something with those before I append it onto teams.

[00:02:35] So, first step, I need to turn age into an age group. That’s easy enough here. We got a function for that. Group equals age group, age. Now I’ve got this age group, a zero, a three, a nine, a 15, or some bigger number.

[00:02:52] So, if I have a team for it, I want to use that. Otherwise, I want to use something else. So if group in age groups, we’ll look it up. How do we look it up? We’re going to say team is equal to age groups for group, this index. And then if I don’t have that age group, then we’re going to use the default here. Old Fogies.

[00:03:22] And now I have a name and a team and I can append that on the teams, name and team and print it out. So we can try that. There’s no system arguments here. So I’ll just run it from inside PyCharm. And here we see Joe who was 14.

[00:03:46] So what age group would have that then? That would have been 12. He got mapped to too cool. Jane got mapped to starting stars. Johnny, four, will map to the three age group, tiny troopers. Tiny troopers, et cetera.

[00:04:04] So once again, this shows us how this pattern you see over and over checking to see if my key is in the dictionary, if it is, we’re looking up the associated value for that key, and then if it’s not, using something else instead.