chapter 9.1: lists
DESCRIPTION
Chapter 9.1: Lists. Lists are often called by another name in other programming languages – arrays. Suppose there is a list of Customers (people waiting in line). You want to serve each customer one at a time, so each one should walk into the store one at a time. . Use a DoTogether tile - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/1.jpg)
Chapter 9.1: Lists
• Lists are often called by another name in other programming languages – arrays.
![Page 2: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/2.jpg)
Suppose there is a list of Customers (people waiting in line). You want to serve each
customer one at a time, so each one should walk into the store one at a time.
A. Use a DoTogether tileB. Use a DoInOrder tileC. Use a ForAllTogether tileD. Use a ForAllInOrder tile
![Page 3: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/3.jpg)
Which of the following types can be used in a list?
A. RocketteB. Rockette’s ThighsC. Rockette’s HipsD. All of the Above
![Page 4: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/4.jpg)
If I declare a list thusly, and use a ForAllInOrder tile, what order do the rockettes kick?
Order
A rocketterockette2rockette3
B rocketterockette3rockette2
C You can’t be sure
D None of the above
![Page 5: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/5.jpg)
Old SuperBowl Commercialhas three frogs talk
• But what they are advertising we isn’t “ahem” a school topic – So let’s modify it.
![Page 6: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/6.jpg)
Three Frogs
• Three frogs, together they say – Frog 1 (on left) “Hear No Evil”– Frog 2 (in middle) “See No Evil”– Frog 3 (on right) “Speak No Evil”
• That’s hard – let’s start with– Three frogs talk in order left to right– All of them say the same thing: Hello– Like this:
![Page 7: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/7.jpg)
What does this code do?
![Page 8: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/8.jpg)
What does this code do?A. The frogs talk in order
left to rightB. The frogs talk in
backwards order (right to left)
C. Each frog talks, but the order depends
![Page 9: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/9.jpg)
It ALWAYS goes in the order you make your list in
![Page 10: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/10.jpg)
Let’s assume I remake my list like
this (L to R):
![Page 11: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/11.jpg)
What if I want the frogs to say Hello (left to right) 5 times?
A. Make a counted loop run five times INSIDE (as the inner loop) the ForAllInOrder loop
B. Make a counted loop run five times with the ForAllInOrder loop INSIDE (as the inner loop)
![Page 12: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/12.jpg)
Predict: What would this code do?
• ???
![Page 13: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/13.jpg)
By the way… you can make a list out of different kinds of objects
• Do the wave with different “People” objects• Make different types of flowers grow
And out of other things…
![Page 14: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/14.jpg)
Problem: Each frog should say something different…Hear no evil
See no evil
Speak no evil
![Page 15: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/15.jpg)
What programming construct would you use?
A. An if statement inside the For all in orderB. A counted loop inside the For all in orderC. An if statement before the For all in orderD. A counted loop outside the For all in orderE. I don’ t know…
![Page 16: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/16.jpg)
There are (at least) two possible solutions… What is the BEST explanation of why is one
better than the other? A. Option 1 is better because it is shorterB. Option 1 is better because it does the least
number of “checks” (or boolean condition evaluations)
C. Option 2 is better because it makes clear exactly what the “checks” (or boolean condition evaluations) are
D. Option 2 is better because it has a regular structure with empty “else” portions
![Page 17: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/17.jpg)
A) ShorterB) Least
Boolean Checks
C) Clear BooleanChecksD) RegularStructure withEmpty “else”es
![Page 18: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/18.jpg)
Remember this…
• “Good” programs aren’t (always) the ones that– Make it easiest on the programmer– Are easiest for humans to read– Require least amount of effort from programmer
• Instead they are (usually) the ones that– Require least computing work (by computer)– Are easiest to adapt to new situations• Methods and parameters come in here
![Page 19: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/19.jpg)
There’s an EVEN BETTER way to write this program!
• Think about this…– What if we had…• A list of froggies
– Some number of frogs that want to talk • A list of words
– Exactly the same “number” of Strings to be “said” by each of those frogs…
– How can we make a program that will make each frog say a specific word• In ONLY 2 LINES OF CODE?!?!?
![Page 20: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/20.jpg)
Let’s make the beetles do a more complicated dance…
• Like this:
![Page 21: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/21.jpg)
Which structure would you want?
A. One ForAllInOrder loopB. One ForAllInOrder loop with an if statement
insideC. One if statement with a ForAllTogether insideD. None of the above
![Page 22: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/22.jpg)
Does this code correctly implement our intended behavior?
A. YesB. NoC. I don’t know
And WHY!
![Page 23: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/23.jpg)
What is new about this code?
• Nested “for all” loops• A loop that “iterates” over all items in a list– Inside that is another loop that goes over all the
items in a list– In this case, happen to be the same list
• Wow, you can do complicated things now…
![Page 24: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/24.jpg)
![Page 25: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/25.jpg)
There are 4 beetles, how many times is this instruction executed?
A. 4B. 12C. 16D. 48E. I don’t know
![Page 26: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/26.jpg)
Parameters to methods:We can do better now!
![Page 27: Chapter 9.1: Lists](https://reader033.vdocuments.mx/reader033/viewer/2022061615/56816525550346895dd7ad5c/html5/thumbnails/27.jpg)
How would we do our “new” solo (with backup)