institute for personal robots in education (ipre) csc 170 computing: science and creativity

12
BIG IDEAS: ALGORITHMS AND PROGRAMMING LISTS: MANAGING A COLLECTION OF ITEMS Institute for Personal Robots in Education CSC 170 Computing: Science and Creativity

Upload: rosamund-parrish

Post on 26-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

BIG IDEAS: ALGORITHMS AND PROGRAMMINGLISTS: MANAGING A COLLECTION OF ITEMS

Institute for Personal Robots in Education

(IPRE)

CSC 170Computing: Science and

Creativity

Page 2: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: 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

Page 3: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• 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

Page 4: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

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

Page 5: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

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

Page 6: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

ONE SOLUTION

6

Page 7: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• 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

Page 8: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

A SNAP! LIST OF LISTS

8

Page 9: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• A lot of computer time is spent• Searching for an item in a list

• Sorting a list

SEARCHING AND SORTING LISTS

9

Page 10: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• 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

Page 11: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• 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

Page 12: Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity

• 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