Download presentation
Presentation is loading. Please wait.
Published byPrudence Miller Modified over 8 years ago
1
LISTS
2
LEARNING OBJECTIVES Explain the difference between the three types of blocks. Explain how a list differs from a normal variable
3
IN THIS LESSON Make a Reporter Block Make a Predicate Block Recognize Command List Blocks Recognize Reporter List Blocks Recognize Predicate List Blocks
4
GRAPHIC ORGANIZER Known TopicNew TopicExamples
5
BLOCK TYPES
6
Known ConceptNew ConceptExamples Block Types
7
RECALL : MAKING A COMMAND BLOCK Note the Shape:
8
BLOCK TYPES Known ConceptNew ConceptExamples Block TypesCommand
9
BLOCK TYPE - REPORTER Creating Reporter Blocks
10
BLOCKS THAT RETURN A VALUE - REPORTERS Note the Shape:
11
HOW TO REPORT THE VALUE Report value Note: The shape of Can only report one value
12
BLOCK TYPES Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate
13
BLOCK TYPE - PREDICATE Creating Predicate Blocks
14
BLOCKS THAT RETURN A BOOLEAN Booleans are TRUE or FALSE values
15
HOW TO REPORT A BOOLEAN Report Boolean Note: The shape of and Can only report one value and it must be a Boolean
16
HOW TO REPORT A BOOLEAN - BETTER Report Boolean Note: The shape of Can only report one value and it must be a Boolean
17
NOTE ABOUT AND, OR, AND NOT,, and are all predicates that take in other predicates. These three predicates behave in ways you might expect from their meanings in English. For example, When will the phrase "I am hungry and with my friends" evaluate to true? Only when both "I am hungry" and "I am with my friends" are true. If I wasn't with my friends, the entire phrase would certainly be false. We often summarize the behavior of predicates like and, or, and not using what are called truth tables. Here is the truth table for and: ABA and B False TrueFalse TrueFalse True
18
BLOCK TYPES Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate
19
NOTE ABOUT AND, OR, AND NOT,, and are all predicates that take in other predicates. These three predicates behave in ways you might expect from their meanings in English. For example, When will the phrase "I am hungry and with my friends" evaluate to true? Only when both "I am hungry" and "I am with my friends" are true. If I wasn't with my friends, the entire phrase would certainly be false. We often summarize the behavior of predicates like and, or, and not using what are called truth tables. Truth table for ABA and B False TrueFalse TrueFalse True
20
NOTE ABOUT AND, OR, AND NOT Truth tables for and Do these definitions fit with what you'd expect from English? Something you should notice is that "A or B" is still true even if both A and B are true. You might be tempted to argue that "or" should be false in this case (consider the sentence "you may have hashbrowns or toast with your breakfast;" having both is generally assumed not to be an option). However, when "or" is used in a conditional, we assume that it is "inclusive;" that is, it includes the fourth case as true (think about the third example from earlier; if we were out of both eggs and milk, we'd almost certainly still want to go to the store). The exclusive version of "or," often called "xor," is available in many other programming languages, but it isn't included in Snap !"xor," ABA or B False True FalseTrue ANot A TrueFalse True
21
ACTIVITY Make a block that when given a traffic light color, it reports what the driver should do. For example, should report “Go!” Make a block that takes in a percentage reports the letter grade. For example,, should report “A”. Make a block that takes in a temperature and reports whether water will be a liquid, solid, or gas. Make a block that reports true if the number is evenly divisible by the other number. Make a block that reports true if the year is a leap year. A calendar year is a leap year if it can be divided evenly by 4, unless it can also be divided evenly by 100, except if it is also divisible evenly by 400, in which case it is a leap year.
22
REVIEW Block Types Predicate Reporter Command Boolean Operations And, OR, NOT Building Reporters and Predicates
23
WHAT DO YOU KNOW AND WANT TO KNOW ABOUT LISTS
24
STORING DATA We learned one way of storing data.
25
STORING DATA Variables are a simple way of storing data. Data has a Type associated with it. Ex: Integer, Real, Boolean, String/Char etc. Data in real world is more complex. Think about the days of the week Actors in a play Temperatures at which elements switch between states Pricing for seats in a stadium Depending on the type of data there are different ways of storing data efficiently.
26
DATA STRUCTURES A data structure is a way of storing and organizing data in a computer so that it can be used efficiently. So depending on the type of data that we are working with, a suitable data structure is used. Examples of data structures - Tables, Sets, Graphs, Lists, Stacks etc. The Data structure we will be learning in this unit is……
27
LISTS … Lists!! Our new unit is all about Lists!!
28
LISTS Examples List of Students in Introduction to Computer Science class. List of Songs Downtown, Hello, Shake It Off, Uptown Funk, Habits, Animals, Bang Bang, Black Widow List of Books Fahrenheit 451, Catcher In the Rye, Harry Potter and the Sorcerer’s Stone
29
LISTS So what is a List? A list stores multiple values of the same data Type. Each element of the list is called an Item. Every item in the list can be accessed using its position in the list, also called its index. The size of the list is the total number of items in the list.
30
LISTS IN SNAP! Variables palette – the red blocks Lists use a variadic input – You can give it any number of inputs
31
SETTING A VARIABLE TO BE A LIST
32
ACTIVITY Fill in your graphic organizer with examples of Known blocks and List blocks Command List blocks Reporter List Blocks Predicate List Blocks HOF List Blocks You will need to Import Tools to get the Higher Ordered Function (HOF) list blocks of Map, Keep Items, and Combine With.
33
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists
34
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists Command List Blocks
35
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists Command List Blocks Reporter List Blocks
36
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists Command List Blocks Reporter List Blocks Predicate List Blocks
37
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists Command List Blocks Reporter List Blocks Predicate List Blocks HOF List Blocks
38
OVERVIEW Known ConceptNew ConceptExamples Block TypesCommand Reporter Predicate VariablesLists Command List Blocks Reporter List Blocks Predicate List Blocks HOF List Blocks
39
LISTS Operations on Lists Create a list. Add an item to the list. Insert an item into the list. Delete an item from the list. Count the number of items in the list. Replace an item in the list. Delete a List! Create a List of Lists!! Examples?
40
LISTS AS INPUTS INTO OTHER BLOCKS The grey rounded rectangle with red rounded rectangles inside it is the visual representation of a list. Each red rectangle is one list item.
41
LISTS – EXERCISE 1 Create a simple list. Display the items of the List. Delete the items of the list created in Exercise 1. If you get done early: What value do you get if you call the list block with no inputs? (Use the left arrow to delete the original input slot.) Can you drag other reporters into list input slots to compute the list items? What happens if you drag a list block into an input slot that expects a number as input, such as the inputs to the arithmetic operators?
42
ITEMS OF A LIST To select an item of a list use the item block
43
REVIEW Operations on Lists Create a list. Add an item to the list. Insert an item into the list. Delete an item from the list. Count the number of items in the list. Replace an item in the list. Delete a List! Create a List of Lists!! Examples? Blocks that Return Values
44
AGENDA Using Join and Split Higher Ordered Functions: Map, Keep Items, Combine Lists – HOF Reminder Unit 4 Quiz on Tuesday
45
LISTS – QUICK REVIEW In Snap! we can create a List by using – found under the Variables palette. We need to create a variable ( usually named an Index ) to access the different items of the list at different positions. We can further use other functionality under the Variables palette. add to add an item to the end of the list. delete to delete an item at a specified position/index insert an item into the list at a specified position/index length of takes a list as an input and gives the length or size of the list.
46
CHALLENGE OF THE DAY Create 3 lists and a by choosing a random noun, a random adjective, and a random verb. Requires the creation of 3 lists. Nouns Verbs Adjectives
47
OBJECTIVES Navigate a list Use Map to create a new list by altering an existing list. Use Keep Items to create a new list that is a filtered of an existing list. Use Combine with to create a new list that combines elements of an existing list.
48
NAVIGATING A LIST 1.Create a loop counter variable Keeps track of how many times the loop has executed 2.Create a conditional loop – a loop that conditionally stops 3.Get the item in the list at the position stored in the loop counter variable 4.Increment the loop counter within the conditional loop
49
JOIN AND SPLIT Use Join to combine two or more words together to make a longer sentence Use split to split up a sentence into a list
50
HIGHER ORDER FUNCTIONS Import Tools Return a list of modified list items using an expression. Select items from a list given an expression. Combine all list items into one.
51
EXERCISES 1.Create a list of the Beatles Use map and the list to get a list of just the first names of the Beatles 2.Write an expression that will select all the words of at least five letters from a list. For example, if the words in the list are being, for, the, benefit, of, mister, and kite, then your block should choose the words being, benefit, and mister. 3.Use the Combine with block to create a block that computes the average of a list of numbers 4.Create a block that creates an acronym from the sentence pasted in. Hint: You will need all three HOF blocks +
52
PROJECT HANGMAN - SPECIFICATIONS Write a program to play the classic game - Hangman. The game is played between the computer program and a single user. The program should come up with a secret word and the user tries to guess this word within the lives (or chances) provided. Create sprites for various stages of Hangman. Draw the Hangman sprites according to the progress in the game.
53
PROJECT HANGMAN – SPECIFICATIONS CONTD. The program must use lists demonstrating some common operations on lists that we learn in this unit. The program should organize important tasks into separate functions. Last but not least - comments should be added to help understand the program better!! For this project you work in pairs.
54
PROJECT HANGMAN USING LISTS DETAILED INSTRUCTIONS Hangman Program should do the following: Using Lists create a word bank to store the secret words with different lengths. The Program decides on: the number of words. max length for the words in the word bank. Prompt the user for the length of the secret word. Pick a word of this length from the Word bank as a secret word to begin the game. Decide on the number of lives for Hangman – Number of lives left to be displayed for the user. Work in pairs.
55
SUMMARY How do you create a block that accepts a parameter? How do you create a block that returns a value? How do you incorporate input from the user?
56
LISTS – EXERCISE 5 SIMPLE OPERATIONS ON LISTS. Using the WordBank List in previous exercise: Write a function that takes an index as an input and returns the Word at that index from the list. Ask the user to input an Index and display the word List item at the number.
57
CREATE A BLOCK WITH A PARAMETER
58
PROJECT HANGMAN USING LISTS DETAILED INSTRUCTIONS – CONTD. Create a variable called GuessedWord of same length as secret word and sets it to all blanks ‘_’ Prompt the user to guess the next letter. Check if the letter is in the secret word. If letter present in secret word, then replace the ‘_’ in GuessedWord to the letter and repeat for all occurrences of letter in the word. If not, reduce the number of lives by 1 and draw the next part of Hangman.
59
EXTRA
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.