BYU logo Computer Science

Big and Small Transcript

[00:00:00] Hey there. Let’s go to the big and small problem together. So here, we need to write a program that queries the user for a list of numbers, one number at a time. Then ask the user what number to use as the boundary between big and small numbers.

Start visual description. Text on screen reads:
Big and Small
Write a program that queries the user for a list of numbers (one number at a time).
Then ask the user what number to use as the boundary between “big” and “small” numbers.
Print “You have {how_many} numbers”
Then print the small numbers with the heading “These are small:”
Then print the bit numbers with the heading
“These are big:”
End visual description.

[00:00:16] And we’re going to print out how many numbers they have, print out the small numbers, these are small and then print out the big numbers with the heading these are big. Here’s some example output from this program, right. So we can see the the query phase where we’re going to get their inputs, get the boundary, print out how many numbers they gave us, and then these are the small ones, these are the big ones.

[00:00:42] And, you know, once again, we can see the stages. This is probably a function right here to get the input from the get the list from the user. Then here’s a little one where we’re just going to get the boundary.

[00:00:58] And then here we’ve got the report stage, right? Where we’re going to tell them about the numbers, which itself is broken up into like the high level summary, the print the smalls, sorry, and the print the bigs. Right. So we can sketch out the overall pieces of the program and now we can go over and write out some functions for each of those pieces, put them all together.

[00:01:24] Come back in here. Big and small again with an empty file, it’s hard to think about empty. I like to get the bare bones in there. Name equals main. Call main. In this case, let’s come back and look. We need to get the numbers and then we need to get the boundary and then we need to report. Right, so we can say: numbers equals get numbers, boundary equals get boundary, and then report numbers boundary.

[00:02:09] That’s the overall structure of our program. Get the numbers, get the boundary, report on it. So let’s start to fill in the details. What does it mean to get numbers? Well, we’re going to ask for each number one at a time, we’re going to make sure this is our prompt for input making sure there’s a space there.

[00:02:27] We’re going to stop when we get the q. So we’ve seen this pattern before. We’re just going to build up a list using an input loop. So let’s set up that same pattern, right. So we’re going to set up our empty list and eventually we’re going to return that list. But before we return it we’re going to fill it in right? While true number equals input number.

[00:03:01] Okay, if number is actually the letter q, break. Else, now if we actually got a number back, not q we need to turn it into a number, an integer. Right now it’s just a string. Input always returns strings. We want to turn that into an integer and then add that to our numbers right. So we can say numbers dot append int number. So that numbers only holds actual integers that we’ll be able to compare to others later on.

[00:03:36] And it’s a good idea, you know, like if you have a function that’s responsible for inputting numbers that it is the one that turns them into numbers as opposed to returning a list of strings and then letting some other piece of code figure out what to do with it.

Start visual description. Instructor types the following code:
def get_numbers():
numbers = []
while True:
number = input(‘Number: ‘)
if number == ‘q’:
else:
break
numbers.append(int (number))
return numbers
def main():
numbers = get_numbers()
boundary = get_boundary()
report (numbers, boundary)
End visual description.

[00:03:52] You know, here this whole function name suggests I’m going to give you numbers and by numbers we mean integers not strings. And so I’m going to deliver on that promise and make sure that all the strings have been turned into numbers before we return it.

[00:04:08] So we’ve got some numbers, great. Let’s get a boundary. Create a function get boundary. This one’s really simple. We just need to return input boundary. Once again, input is going to return a string, the boundary needs to be an integer so let’s turn that thing that we get back from input right into an integer right away and return that.

[00:04:34] Finally, we need to report. Define report, it’s going to take some numbers and a boundary. And let’s go back and look again. So we’re going to print out the total number and then the small and the big, right? So let’s say print total numbers.

[00:05:01] We don’t need the boundary in order for print total to work. Print small numbers boundary. We do need the boundary and print large numbers boundary. Okay, now let’s implement those. What does it mean to print the total? Well, print you have length of numbers, numbers.

[00:05:31] Is that what we wanted here? You have 8 numbers. I guess there’s no period at the end. What does it mean to print small? And remember the length function, if you pass a list to it it tells you how big that list is.

Start visual description. Instructor types the following code:
def get_boundary():
return int(input(‘Boundary: ‘))
def print_total(numbers):
print(f’You have {len (numbers)} numbers’)
def report(numbers, boundary):
print_total (numbers)
print small (numbers, boundary)
print large (numbers, boundary)
End visual description.

[00:05:46] So whenever you think: how many do I have in the list? Just think length function. Alright so that knows to find what print small means. Create a function print small and we’re going to have our little heading: these are small. And then we’re going to loop through and print out only the ones that are less.

[00:06:09] Okay so let’s do that. Print these are small. for number in numbers if number is less than boundary, print. Does it tell us how to do it? Just print the number right? Just print the number, okay. That’s it.

[00:06:32] Let’s do print large. Great, well we already have an example we can just look up here and more or less follow the same logic. These are large. for number in numbers, if number is bigger than the boundary print number.

[00:06:54] Now what if the number is equal to the boundary? Does it get included? We can look in here and see did we input 20 at all in this example? No so it’s not specified here what numbers are supposed to be big or small.

Start visual description. Instructor types the following code:
def print_small (numbers, boundary):
print(‘These are small:‘)
for number in numbers:
if number < boundary:
print(number)
def print_large (numbers, boundary):
print(‘These are large:‘)
for number in numbers:
if number > boundary:
print(number)
End visual description.

[00:07:09] So this is where you go back to the user or you go back to the product manager, whoever it is that you’re building this piece of software for and you’d ask that question: Hey, what about the boundary itself? What if one of the numbers is that number? Do you want it to be small do you want it to be big or do you want it in a separate category? And you clarify with them what’s supposed to happen. Now in this case, let’s just leave it as it is.

[00:07:37] Let’s say we don’t print the boundary. Small numbers are less than, big numbers are bigger and that’s all we’re going to do. Then this is all we need. So we’re going to get the numbers, get the boundary, report. Report means print the total, print the small, print to large. The rest of just details. Let’s see how we did.

[00:07:57] So numbers: 8, 2, 30 a hundred something big, 64, 7, 10 quit. What’s a good boundary number? How about 29? And then there we go right. These are the small numbers and these are the large numbers. And that is that. Again, I want to emphasize the importance of decomposition just like we did with Bit, always follow this same pattern.

Start visual description. Text on console reads:
Number: 8
Number: 2
Number: 30
Number: 12345
Number: 64
Number: 7
Number: q
Boundary: 299
These are small:
8
2
7
10
These are large:
30
123456
64
End visual description.

[00:08:31] Let the functions that you write be concise and simple with other functions define the big ideas. And if a given function has enough big ideas in it, break it down further so that any individual function is very simple and focused on a single goal.

[00:08:52] It will make your code so much easier to understand, so much easier to fix, so much easier to debug, so much easier to build on as new features come, so much easier for someone else to understand and build on. Software developers work in teams, right? And this is the way that you write code that you in the future and other people can all quickly get into and fix and work on together.

[00:09:20] If you’re struggling with that idea, come talk to me. Come to office hours and let’s practice it together so that you can get a good feel for this.

[00:09:31] You can go to the TAs in the TA lab and talk to them and I recommend that you do. Get the practice you need to be able to approach programming in this top- down decomposition style and it’ll make all the difference in the world. Thank you.