institute for personal robots in education (ipre) csc 170 computing: science and creativity
TRANSCRIPT
BIG IDEAS: ALGORITHMS AND PROGRAMMINGLISTS: MANAGING A COLLECTION OF ITEMS
Institute for Personal Robots in Education
(IPRE)
CSC 170Computing: Science and
Creativity
• We often need to store a collection of items
• Snap! has the list block• Found under Variables
• The following list has 3 elements• It is considered an anonymous list as it has no name
LISTS
2
• By default, a list has 1 item that is an empty word
• To create an empty list, click the left arrow to remove it
NEW LISTS CAN HAVE ONE NOTHING ITEM
3
A NAMED LIST WITH 3 ELEMENTS
4
• Code demo:• Give a name to an “empty” list, add 3 names
• Show all, first, last, and any element in the list known as names
names is a list with 0 elements
TIME MAGAZINE ARTICLE
• Many years ago, an historian asked me to come up with a list of random numbers, from 1 to 100• I used Turbo Pascal to do this, it took about 5 minutes because Turbo
Pascal has a pseudo random number generator, like Snap! does
• The list was then used to select from a list of the top 100 historians who would then supplied their top 5 list of the worst presidents in U.S. history
• Ronald Reagan made the list of the 5 worst presidents in a Time Magazine article
• My colleague received death threats and left Penn State
• Code demo: Create a custom block to return a list of random integers
ONE SOLUTION
6
• You list of phone contacts
• List of emails
• Your registered courses
• Your transcript
• A top ten list
• The list of movies in your NetFlix queue
• List of all addresses for the newspaper delivery person
• This list
• Snap list of lists• Each element in a list is another list
LISTS ARE USED EVERYWHERE
7
A SNAP! LIST OF LISTS
8
• A lot of computer time is spent• Searching for an item in a list
• Sorting a list
SEARCHING AND SORTING LISTS
9
• A data type is first class in a programming language if data of that type can be all of the following:• the value of a variable
• an input to a block
• the value returned by a block
• a member of a data aggregate: we can have a list of lists
• anonymous (not named)
• Snap lists are first class types!
LISTS ARE FIRST CLASS TYPES
10
• Average of a list of numbers input to a block• Set sum to 0
• For every element in the list• Add the item to sum
• report sum / length of the list
ALGORITHM: AVERAGE OF A LIST OF NUMBERS
11
• Minimum word of a list input to a block• Set min to the first item in the list
• For every element in the list• If that item is less than the min
• Change min
• report min
• Write a Snap block that does this
ALGORITHM: FIND THE ALPHABETICALLY FIRST WORD
12