Snap! (BYOB): Lesson 5: Finding the Smallest or Largest Number in a List
For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1. I’m using BYOB version 3.1.1 for my examples, but you can follow along with either version linked below.
Sorting through lists is something you will encounter several times when beginning programming. So it’s important to understand the logic in comparing. In a future lesson, I’ll share the C++ equivalent of this Snap! program.
In this lesson, we’re building a program that sorts through a small list of scores and reports the largest number in the list. If you would like to find the smallest number in a list, there’s only one major block difference – the greater than becomes less than and variables named accordingly.
For this exercise, I’m using blocks from the following categories:
- Control (Brown)
- Operators (Green)
- Variables (Orange)
Finding the Largest Number in a List
Under Variables, click Make a block. In the pop-up box, select a category for the custom block, select the reporter option, and name the block Largest number in. Click OK.
Hover over Largest number in and click the plus sign on the right.
On the Create input name box, click the black arrow.
Under Create input name, write the word list. In the Input type, Shape of slot section, select List. Click OK.
By specifying the input type, the final block will only allow lists.
Under Variables, grab the script variables block.
We will use this block to create all variables the program needs.
Click on the little a. In the pop-up box, rename the variable largestNumber. Click OK.
The black arrows allow us to add more variables. Make one more variable and name it indexLocation.
Under Variables, grab two set _ to 0 blocks.
Click the first box in both blocks. Set the first block to largestNumber and the second block to indexLocation.
Now, the variables need a starting point.
Under Variables, grab the item 1 of _ block. Place it in the set largestNumber to _ block.
Drag the list variable into the last slot.
The largestNumber is set to the first number in the list because it’s the largest number we’ve seen when the block begins running.
In the set indexLocation to _ block, change the 0 to 1.
Under Control, grab the repeat block. Place it at the end.
Under Variables, grab the length of _ block and place it in the spot with the 10.
Drag the list variable and place it in the empty slot in the length of _ block.
The repeat block will run through the entire list.
Under Control, grab the if _ block. Place it inside the repeat block.
Under Operators, grab the _ > _ (greater than) block. Place it as the conditional.
Under Variables, grab the item 1 of _ block. Place it in the left slot in the greater than block.
Drag the indexLocation and list variables and place them in the item 1 of _ block as pictured below.
Drag the largestNumber variable and place it into the right slot of the greater than block.
Under Variables, grab the set _ to 0 block. Place it inside the if block.
Right click the item indexLocation of list block above and select duplicate.
Place the copy in the right slot of the set _ to 0 block and change the left box to largestNumber.
Here’s what the if does: If the number at the current index is larger than the current value at largestNumber, it becomes the new largestNumber. This step-by-step comparison repeats throughout the length of the list.
Under Variable, grab the change _ by 1 block. Place it after the if block, but stay inside the repeat block.
Click the down facing arrow and select indexLocation. The 1 stays unchanged.
Here’s what it does: The index location begins at 1, comparing the first number to largestNumber. The index changes to 2 and the comparison process continues.
Under Control, grab the report _ block. Place it on the very end, outside the repeat block.
Drag the largestNumber variable and place it inside the report block. Click OK.
Under Control (or whichever category you chose in the first step), grab our custom block. It should like the one below.
Now we need lists to sort. Under Variables, click the make a list option. Name the list and click OK.
I’m making two lists to demonstrate the capabilities of our custom block. You can make one or two to follow along.
On the stage, you’ll see an empty list of scores. Click any corner and drag outward to resize the box.
Click the plus sign on the bottom left to input scores. Write any score, press Enter, and repeat. When you have your desired number of scores, click anywhere off the list to exit.
If you create too many slots, simply hover on the line to bring up an x (delete option).
The blocks for Scores1 and Scores2 (or the name of your list) are found under Variables.
Grab the list name and place it in the Largest number in _ block.
Click it for the result.
Any list works in our custom block.
I’ve included the blocks for smallest number in a list below.