49
LISTS

LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

Embed Size (px)

Citation preview

Page 1: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

LISTS

Page 2: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

RECAPMajor Topics

• Variables

• Functions

• Sensing

• Messaging

• OOP

Page 3: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

STORING DATA• We learned one way of storing data.

• Can you recall?

Page 4: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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.

• Depending on the type of data there are different ways of storing data efficiently.

Page 5: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 6: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

LISTS… Lists!!

Our new unit is all about Lists!!

Page 7: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

LISTS

Examples

• List of Students in Introduction to Computer Science class.

• List of Songs

• Shake It Off, All About That Bass, Habits, Animals, Bang Bang, Black Widow

• List of Books

• Fahrenheit 451, Catcher In the Rye, Harry Potter and the Sorcerer’s Stone

Page 8: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 9: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 10: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 11: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 12: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

GOAL(S) FOR TODAY

• Understanding List data structure and operations that can be performed on Lists.

• Write code successfully to perform simple operations on Lists.

Page 13: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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

number of inputs

Page 14: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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. RECAP Major Topics Variables Functions Sensing Messaging OOP

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. RECAP Major Topics Variables Functions Sensing Messaging OOP

SETTING A VARIABLE TO BE A LIST

Page 17: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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

block

Page 18: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

BLOCKS THAT RETURN A VALUE

Page 19: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

HOW TO RETURN THE VALUE

Page 20: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

ASK THE USER FOR WORD

Page 21: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 22: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 23: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

AGENDA

• Continue to Explore/learn other functionality pertaining to lists: item<index>list , length of

• Exercise 3 – To Create a Word Bank.

• Exercise 4, 5 – Operations on Word Bank.

• Look at the Specifications for the Hangman project.

Page 24: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 25: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 26: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 27: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

CREATE WORD BANK

Page 28: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 29: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 30: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

CREATE A BLOCK WITH A PARAMETER

Page 31: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

USING SOME OF THE BUILT-IN LIST FUNCTIONALITY

• item<index>list and length of

Page 32: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

HIGHER ORDER FUNCTIONS

• Modify each list item using an expression.

• Select items from a list given an expression.

• Combine all list items into one.

Page 33: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

EXERCISES• Create a list of the Beatles

• Use map and the list to get a list of just the first names of the Beattles

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

• Use the Combine with block to create a block that computes the average of a list of numbers

Page 34: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 35: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 36: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 37: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 38: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 39: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

PROJECT HANGMAN USING LISTSDETAILED INSTRUCTIONS – CONTD.

• The program repeats until

• all the letters in the word are guessed where the user Wins!

• All the lives are exhausted where user looses!

Page 40: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 41: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 42: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

ASSIGNMENT - HANGMAN PROJECT - GRADINGGrading Item Points

Function - CreateWordBank 10

Function - PickSecretWord 10

Function - IsLetterPresentinWord 10

Combining previous and current guesses into guessedWord 15

Create Sprites for various stages and update hangman using Sprites as the game progresses.

15

Integrating the different functions from the main program and tying it all together.

15

Comments, readable code 10

Displaying the number of lives remaining in the game. 5

TeamWork 10

ExtraCredit - level of difficulty included in the game ex:Easy, Medium, Expert etc , game in a learning mode where new words are added, Story, special ( visual, sound ) effects,

20

Page 43: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

ASSIGNMENT - HANGMAN PROJECT

• Due date: Week after the Spring break

• completed projects to be emailed to [email protected]

Page 44: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP
Page 45: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

ASSIGNMENT - HANGMAN PROJECTVariable/operator/control

“Make a List”

“Add”, “delete”, “item”, “length of” from List Variables section

From operators section use “Letter(i) of word”, “Length of” when dealing with words

Page 46: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

ASSIGNMENT - HANGMAN PROJECT - GRADINGGrading Item Points

Function - CreateWordBank 10

Function - PickSecretWord 10

Function - IsLetterPresentinWord 10

Combining previous and current guesses into guessedWord 15

Create Sprites for various stages and update hangman using Sprites as the game progresses.

15

Integrating the different functions from the main program and tying it all together.

15

Comments, readable code 10

Displaying the number of lives remaining in the game. 5

TeamWork 10

ExtraCredit - level of difficulty included in the game ex:Easy, Medium, Expert etc , game in a learning mode where new words are added, Story, special ( visual, sound ) effects,

20

Page 47: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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 48: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

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.

Page 49: LISTS. RECAP Major Topics Variables Functions Sensing Messaging OOP

LISTS – WHAT WE DID TODAY 3/31/2014

• Looked at the Specifications, detailed instructions for the Hangman project.

• Continued to Explore/learn other functionality pertaining to lists:

• “replace item”

• “item<index>list”

• <list>contains<item>

• Exercise 3,4,5 – To Create a Word Bank, perform operations on Word Bank.

• Exercise 6 – start creating Hangman Sprites