Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
12/2/11
12/2/11
Announcements
Functions
Functions
Final - Mon Dec 5, 8-10AM, this room.
Similar to Midterm 2; two programs.
Bring a Scantron.
Open book, notes, programs. No computers,
phones.
Office hours Sunday, 1-3 1131 KEMPER
def twoChars(myStr, i):
if i < 0 or i > len(myStr)-2:
return “”
one = myStr[i]
two = myStr[i+1]
return(one+two)
def setUpList():
L = []
for i in
range(4):
L = L + [i]return L
def setUpList():
numbers = setUpList() print(numbers)
NOTE: This
example will be
relevant to final.
def main():
s = twoChars(“Edith”,2)
print(s)
main()
ECS 10
What does this
print?
12/2
What to review
Using a dictionary
Combined data structures
List
Putting
Dictionary
data in
Getting data out
Changing data
Creating
of lists
Functions
Calling
functions
data in
data out
Local variables
Global variables
Using a list
Passing
Putting
Passing
data in
Getting data out
Changing data
Materials
Functions
You’re responsible for what was covered in lecture.
Review slides. Readings in book.
Emphasis on things in programming assignments.
Go over programs we did in class, solutions to
programs. Do you understand every line?
Last year’s final is on SmartSite, and a program for
the problem we’ll do today. Also we’ll post
Michael’s Solution to Armen’s tic-tac-toe problem,
and I’ll post solution to last year’s final MC.
def initX():
x = 10
def main():
print(x)
main()
Functions
Functions
def setUpList():
L = []
for i in
range(4):
L = L + [i]return L
def setUpList():
numbers = setUpList() print(numbers)
def setUpList():
L = []
for i in
range(4):
L = L + [i]return L
def setUpList(L1):
numbers = setUpList() print(numbers)
L = []
for i in range(4):
L.append(i)
def main():
setUpList()
print(L)
main()
where values are lists
L = []
for i in range(4):
L.append(i)
return L
def main():
numbers = setUpList()
print(numbers)
main()
What does it print?
What if it was….
What does it print?
What if it was….
What does it print?
for i in range(4):
L1.append(i)
def main():
L = []
setUpList(L)
print(L)
main()
List indexing
What does this
do?
L = [5,7,2,6,3]
averages = []
____________:
avg = (L[i]+L[i+1])/2
averages.append(avg)
print(averages)
1
Prints [6, 4.5, 4, 4.5]
What could fill in the
blank?
2
12/2/11
Putting things into a dictionary
For on a dictionary
Band = {}
Band[“Anders”] = “tuba”
Band[“Ho”] = “flute”
Band[“Moon”] = “tuba”
L = []
___________________
__________________
L.append( [instrument, name] )
L.sort()
Programming problems
12/2/11
Program structure
What could fill in
the blanks?
Three functions:
Loop1
Loop2
Thanks!
Thank you all, and see you Monday!
(file read, build data structure),
(use data structure to produce output),
Main.
What
is the data structure? What should go into and
out of each function?
Yelp
Build a program out of parts:
Read
a file and build a dictionary, possibly combining
items.
Read
a file and build a list (or list of lists)
user input and look up item in a dictionary
dictionary items into a list
Sort a list
Write an output file, or write dictionary contents out to
a file.
Get
Put
The job – make a top ten list
Input is long file of user rankings
Each
line has restaurant, number of stars
order
Arbitrary
Get average number of stars per restaurant
Output restaurant with best average
An algorithm
Read file, store in dictionary using restaurant name
as key.
Values are [total stars, number of ratings]
When we get a new rating for restaurant,
Create
Add
Average number of stars
new dictionary entry if necessary
to total stars, number of ratings for the restaurant
For loop on dictionary
Sort the list to get top ten
Compute
(total
stars) / (number of ratings)
Example: ***, **, ****, ** = (3+2+4+2) / 4 = 2.75
Put
average for each restaurant
[avg, restaurant name] into a list
3
4