LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a...

41
LISTS

description

RECAP Major Topics Variables Functions Sensing Messaging Object Oriented Programming (OOP)

Transcript of LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a...

Page 1: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTS

Page 2: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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

Page 3: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

RECAPMajor Topics

• Variables

• Functions

• Sensing

• Messaging

• Object Oriented Programming (OOP)

Page 4: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

STORING DATA• We learned one way of storing data.

• Can you recall?

Page 5: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 6: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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……

Page 7: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTS… Lists!!

Our new unit is all about Lists!!

Page 8: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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

Page 9: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTSSo 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.

Page 10: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTSOperations 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?

Page 11: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

GOOD PROGRAMMING PRACTICESContinued…

• 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!

Page 12: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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!!

Page 13: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTS IN SNAP!• Variables palette – the red blocks Lists use a variadic input – You can give it any

number of inputs

Page 14: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 15: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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?

Page 16: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

SETTING A VARIABLE TO BE A LIST

Page 17: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

ITEMS OF A LIST• To select an item of a list use the item

block

Page 18: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

BLOCKS THAT RETURN A VALUE

Page 19: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

HOW TO RETURN THE VALUE

Page 20: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

ASK THE USER FOR WORD

Page 21: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

REVIEWOperations 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

Page 22: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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

Page 23: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 24: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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

Page 25: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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

Page 26: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 27: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

EXERCISES1. 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

Page 28: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTS - EXERCISE 3CREATE 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.

Page 29: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 30: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 31: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 32: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

PROJECT HANGMAN USING LISTSDETAILED INSTRUCTIONSHangman 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.

Page 33: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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?

Page 34: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 35: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

CREATE A BLOCK WITH A PARAMETER

Page 36: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

USING SOME OF THE BUILT-IN LIST FUNCTIONALITY

• item<index>list and length of

Page 37: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

PROJECT HANGMAN USING LISTSDETAILED 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.

Page 38: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

ASSIGNMENT - HANGMAN PROJECT1. 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

Page 39: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.

Page 40: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

LISTS/HANGMAN – EXERCISE 6CREATE HANGMAN SPRITES FOR VARIOUS STAGES OF THE GAME.As part of this exercise start creating the sprites for the Hangman game.

Page 41: LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.

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.