Download ECS 10 Announcements Announcements Algorithms Tools

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Announcements
! 
Curves for Midterm 2
! Multiple
choice:
12-10 A, 9-7 B, 6-4 C
! Program:
33-40 A, 25-32 B, 14-24 C
ECS 10
12/1
Announcements
Algorithms
! 
All programs due on Thursday.
!  Lab hours will be crazy tomorrow. The TA and I
will focus on the people doing the makeup
assignment, so they don’t suffer from the game
people having an extension.
!  Final is Wds 12/8 (week from today), 8-10
AM, in this room. Bring a Scantron.
! 
Tools
Macroeconomics project
Our data wrangling algorithms are made up of
parts, most of them built around a loop.
!  Parts we have seen:
! 
!  Read
a file and build a dictionary
a file and build a list (or list of lists)
!  Get user input and look up item in a dictionary
!  Read a file and look up each item in a dictionary
!  Sort a list
!  Write a list out to a file
!  Read
The algorithm is the overall design of the program;
the idea of how it goes about solving the problem.
!  Often there is more than one algorithm to solve a
problem.
!  Today we’ll look at some data wrangling problems
and think of algorithms for them.
Read file and build dictionary for gdp
!  Read cab file and lookup each county in gdp
dictionary; compute R and store result in list
!  Sort list
!  Output sorted list
! 
Alternative algorithm
Read file and build dictionary for gdp
!  Read cab file and build dictionary for cab
!  for county in cab dictionary, lookup gdp, compute R
and store result in list
!  Sort list
!  Output sorted list
for loop on a dictionary
! 
Program structure
One function per part (or per loop)
A main function that calls all of the other functions
!  The main function serves as a kind of outline of the
algorithm
!  I have seen a lot of programs where commands that
are not inside a function are distributed among the
function definitions (no main() function). These work
but are very hard to read.
Variable country takes the value of each dictionary
key in turn.
!  Similar to for loop on a list or file.
!  Another way to get information out of a dictionary.
!  Which algorithm is better?
! 
Find phrase
! 
! 
Possible Algorithm
Read the file a quatrain at a time
Break up into words; check for word “nature”
!  If found, check for whole phrase
!  If found, print quatrain
! 
! 
! 
How many loops does this require?
Which quatrain
contains the
phrase “nature
red in tooth and
claw”?
Doner list
Job – organize doner list
! 
! 
Input is file with list of doners, size of donation
Output should be organized into 6 ranges,
alphabetical within range.
An algorithm
Divide into list of 6 lists, one for each range of
donation sizes.
!  Sort each list alphabetically
! 
! 
Yelp
How many loops does this take?
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 top ten
! 
! 
Average number of stars
!  (total
stars) / (number of ratings)
!  Example: ***, **, ****, ** = (3+2+4+2) / 4 = 2.75
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, add to
total stars, number of ratings
!  Read completed dictionary into list, computing
average
!  Sort by average
!  Print out top ten
! 
Rate movies
The job
Long file containing popular movies
Ask user to rank random movies until she quits
!  Output a file containing movies she’s rated
An algorithm
! 
! 
! 
! 
Read file of movies and save in movie list
While user still wants to rank:
!  Pick
a random integer between 0 and len(movie list)
it to get a random movie
!  Ask user for ranking
!  If she’s seen it, save ranking in output list
!  Use
! 
Write output list to file