Download presentation
Presentation is loading. Please wait.
Published byWarren Mills Modified over 9 years ago
1
LISTS
2
LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate input from the user
3
RECAP Major Topics Variables Functions Sensing Messaging Object Oriented Programming (OOP)
4
STORING DATA We learned one way of storing data. Can you recall?
5
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.
6
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……
7
LISTS … Lists!! Our new unit is all about Lists!!
8
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
9
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.
10
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?
11
GOOD PROGRAMMING PRACTICES Continued… Modular/functions: According to the functionality of the program, write each major task as a function. Gives you the flexibility to change a certain task/function without affecting the rest of the program. This helps while working as a team. Team members can work on individual functions without having to worry about its inner workings. These functions can be called during integration. Improves readability. Comments: There is no such thing as too many comments. Helps understand the program better!
12
GOOD PROGRAMMING PRACTICES Continued… Help improve program maintenance. Write an Algorithm i.e. Steps in English to solve the problem/writing the steps, then code – don’t start coding right away!!
13
LISTS IN SNAP! Variables palette – the red blocks Lists use a variadic input – You can give it any number of inputs
14
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.
15
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?
16
SETTING A VARIABLE TO BE A LIST
17
ITEMS OF A LIST To select an item of a list use the item block
18
BLOCKS THAT RETURN A VALUE
19
HOW TO RETURN THE VALUE
20
ASK THE USER FOR WORD
21
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
22
AGENDA Using Join and Split Exercise 2 – Using Join and Split Exercise 3 – Create a Word Bank. Exercise 4, 5 – Operations on Word Bank. Higher Ordered Functions: Map, Keep Items, Combine Lists - HOF
23
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.
24
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
25
EXERCISE 2 Create a block that asks the user for a word and returns it. Create 3 lists and a block that returns a random sentence based upon the 3 lists Nouns Verbs Adjectives
26
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.
27
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
28
LISTS - EXERCISE 3 CREATE A WORD BANK/DICTIONARY Write a function called CreateWordBank Function CreateWordBank should do the following: Create a List of random ( but common ) words. The List should have a reasonable number of words. Say for Example about 20 words. Function CreateWordBank should return the total number of words in the bank. You’ll use this function CreateWordBank in the Hangman Project.
29
CREATE WORD BANK
30
LISTS – EXERCISE 4 SIMPLE OPERATIONS ON LISTS Using the WordBank List in previous exercise: Add new words to the list. - The idea is the word bank/ dictionary can learn/ grow dynamically. - You could take the new words from the user as an input. Remove some items from the list. find out the length/size of the WordBank list and store this value in a variable.
31
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.
32
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.
33
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.
34
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?
35
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.
36
CREATE A BLOCK WITH A PARAMETER
37
USING SOME OF THE BUILT-IN LIST FUNCTIONALITY item list and length of
38
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.
39
ASSIGNMENT - HANGMAN PROJECT 1.Using lists create a WordBank/WordStore of secret words. You could create a single list of all words you want to store or separate lists for different word lengths. You choose the words, total number of words, length of words. Ex: function CreateSecretWordBank 2.Write a function that given a length, picks a word of this length from the WordBank. Ex: function PickSecretWord 3.Write a function that takes a letter and a word and gives a word with all occurrences of the letter in the word and a “_” in places where the letter is not present. It also returns a Boolean(T or F) to signal the presence of the letter. function IsLetterPresentInWord
40
ASSIGNMENT - HANGMAN PROJECT 4.Write a function that works with the words with the guessed letter ( from step 3) that combines the new guessed word with the previous guessed word, so that a letter in either word is copied to the combined word at that index/location. CombineGuesses 5.Create sprites for various stages of Hangman and draw the Hangman sprites according to the progress in the game. You could also use costumes from the letters section. The program decides on the number of lives for the user.
41
LISTS/HANGMAN – EXERCISE 6 CREATE HANGMAN SPRITES FOR VARIOUS STAGES OF THE GAME. As part of this exercise start creating the sprites for the Hangman game.
42
LISTS - EXERCISES The idea is to write simple exercises in BYOB as functions that are useful for the Hangman project. You can use the functions you write here for the actual Hangman project.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.