CompSci 101 Introduction to Computer Science

Slides:



Advertisements
Similar presentations
CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
Advertisements

CompSci 101 Introduction to Computer Science January 13, 2015 Prof. Rodger compsci 101 spring
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
Introduction. 2COMPSCI Computer Science Fundamentals.
CompSci Maps  Maps are another way of organizing data  Keys and Values  Each key maps to a value  Some keys can map to the same value  Can.
CompSci 6 Introduction to Computer Science October 20, 2011 Prof. Rodger.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CompSci 6 Introduction to Computer Science Sept 29, 2011 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not.
CompSci 101 Introduction to Computer Science March 3, 2015 Prof. Rodger compsci101 spring151.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
CompSci 101 Introduction to Computer Science February 10, 2015 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
Compsci 101.2, Fall Plan for October l Structuring Data and Code for Efficiency  Computer time, how expensive is it?  Data storage, how.
CompSci 101 Introduction to Computer Science Feb 24, 2015 Prof. Rodger.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science November 11, 2014 Prof. Rodger CompSci 101 Fall Review for exam.
CompSci 101 Introduction to Computer Science March 17, 2015 Prof. Rodger compsci 101, spring
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
CompSci 101 Introduction to Computer Science February 16, 2016 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science April 14, 2016 Prof. Rodger Lecture today by Sriram Vepuri.
CompSci 101 Introduction to Computer Science March 1, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CompSci 101 Introduction to Computer Science Sept 13, 2016 Prof. Rodger compsci101 fall161.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Now with a speaking professor! (hopefully...)
CompSci 6 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
CompSci 101 Introduction to Computer Science
Department of Computer Science,
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CISC101 Reminders Slides have changed from those posted last night…
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CS190/295 Programming in Python for Life Sciences: Lecture 6
Recitation Outline C++ STL associative containers Examples
Introduction to Dictionaries
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Presentation transcript:

CompSci 101 Introduction to Computer Science Oct 24, 2017 Prof. Rodger compsci 101, fall 2017

Announcements RQ 14 due Thursday Assignment 5 due Thursday APT 5 out, due Tues, Oct 31 Extend Exam 1 regrade requests by Oct 26! Contact gradescope if you cannot see your exam Lab this week! Songs and movies Today: Finish example from last time Dictionaries – a way to organize data for fast lookup compsci 101, fall 2017

Lab this week - .csv files Answering questions about songs and movies compsci 101 spring 2017

Lab - .csv file compsci 101 spring 2017

Registration time… What CS courses can you take next? CompSci 201 CompSci 216 - Everything Data CompSci 230 - Discrete Mathematics CompSci 230 is prereq for CompSci 330 CompSci 201 is prereq for many electives compsci 101, spring 2017

LAST TIME: Problem: Popular Name Given a list of names, determine the most popular first name and print that name with all of its last names. Input: Names are always two words, names are in a file. If multiple names are on the same line they are separated by a “:” Output: Most popular first name, followed by a “:”, followed by corresponding last names separated by a blank compsci 101, fall 2017

Example Input File with 5 lines Susan Smith:Jackie Long:Mary White Susan Brandt Jackie Johnson:Susan Rodger:Mary Rodger Eric Long:Susan Crackers:Mary Velios Jack Frost:Eric Lund Corresponding Output Susan: Smith Brandt Rodger Crackers compsci 101, fall 2017

Example – two lists firstNames lastNames ’Susan’’ [ ‘Smith’,‘Brandt’,‘Rodger’,‘Crackers’]’ ‘Jackie’ [ ‘Long’, ‘Johnson’]’ 1 1 ‘Mary’ [ ‘White’,’Rodger’,’Velios’]’ 2 2 ‘Eric’ [ ‘Long’, ‘Lund’]’ 3 3 ‘Jack’ [ ‘Frost’]’ 4 4 compsci 101, fall 2017

Now can we solve the problem? Compute those two lists that are associated with each other List of unique first names List of corresponding last names Compute the max list of last names Now easy to print the answer. See popular.py compsci 101, fall 2017

This function generates the list of lists of corresponding last names def correspondingLastNames(data, firstNames): lastNames = [ ] for name in firstNames: lastNames.append(allLastNames(data,name)) return lastNames compsci 101, fall 2017

Finish maxnum = max([len(item) for item in lastNames]) print maxnum lastIndex = [index for (index, v) in enumerate(lastNames) if len(v) == maxnum] print "first name with most last names is:" What is the last line of code? compsci 101, fall 2017

Expanding the Problem Suppose we want to read from multiple data files names1.txt, names2.txt, names3.txt See processFiles in popular.py compsci 101, fall 2017

Another way – list of lists First word in each list is a first name The rest are last names. [ ‘Susan’, ‘Smith’,‘Brandt’,‘Rodger’,‘Crackers’]’ [‘Jackie’, ‘Long’, ‘Johnson’]’ 1 [‘Mary’, ‘White’,’Rodger’,’Velios’]’ 2 [ ‘Eric’, ‘Long’, ‘Lund’]’ 3 Another way to do it. What would change? How you process the data? Can you find the max size list still? yes [ ‘Jack’, ‘Frost’]’ 4 compsci 101, fall 2017

Now, a new way to organize data…. compsci 101, fall 2017

Dictionaries/Maps Dictionaries/maps are another way of organizing data Keys and Values Each key maps to a value Some keys can map to the same value Can change the value a key maps to compsci 101, fall 2017

Example Each student could be mapped to their favorite ice cream flavor How is this different then compsci 101, fall 2017

How is dictionary different than a list? List – have to search for name first Dictionary – each key maps to a value getting name (or key) is automatic! Fast! Keys Values In a list if we wanted to know the ice cream flavor for Rodger, we would have to search for Rodger and then could get it. If we use a dictionary, we don’t have to search for Rodger, the dictionary knows where the name is and thus can return what it is matched to quickly. In 101 we are just focused on using a dictionary. In 201, you learn how dictionaries are implemented. compsci 101, fall 2017

Implementing a Dictionary/Map Keys map to values Create Empty dictionary somemap = {} Put in a key and its value somemap[“Forbes”] = “Strawberry” Get a value for a dictionary value = somemap[“Forbes”] OR value = somemap.get(“Forbes”, “default”) Change a value for a dictionary somemap[“Forbes’] = “Chocolate” Default – returns default value if there is no value compsci 101, fall 2017

More on using a Dictionary/Map Get all the keys (as a list) listKeys = somemap.keys() Get all the values (as a list) listValues = somemap.values() Other methods clear – empty dictionary items – return (key,value) pairs iteritems – return (key,value) pairs more efficiently, iterator – must use with for update – update with another dictionary compsci 101, fall 2017

Change Astrachan’s value somemap[“Astrachan”] = Coffee Mocha compsci 101, fall 2017

Change Astrachan’s value somemap[“Astrachan”] = Coffee Mocha compsci 101, fall 2017

Value could be a set or list compsci 101, fall 2017

Simple dictionary bit.ly/101f17-1024-1 compsci 101, fall 2017

More simple dictionaries bit.ly/101f17-1024-2 compsci 101, fall 2017

Back to Popular Name Problem: Given a list of names, determine the most popular first name and print that name with all of its last names. Input: Names are always two words, names are in a file. If multiple names are on the same line they are separated by a “:” Output: Most popular first name, followed by a “:”, followed by corresponding last names separated by a blank compsci 101, fall 2017

Example Input File with 5 lines Susan Smith:Jackie Long:Mary White Susan Brandt Jackie Johnson:Susan Rodger:Mary Rodger Eric Long:Susan Crackers:Mary Velios Jack Frost:Eric Lund Corresponding Output Susan: Smith Brandt Rodger Crackers compsci 101, fall 2017

Use a dictionary/map www.bit.ly/101f17-1024-3 Map first names to count of corresponding last names def mapNameToNumberLastNames(data): Use a dictionary/map popularMap.py compsci 101, fall 2017

Trace example with Python Tutor see popularMapSolnSmall.py compsci 101, fall 2017

Use a dictionary/map www.bit.ly/101f17-1024-4 Map first name to list of corresponding last names def mapNameToLastNames(data): compsci 101, fall 2017

Trace through example with Python Tutor See the small example popularMapSolnSmall.py compsci 101, fall 2017

Use dictionary of first names mapped to corresponding last names How do you find the most popular first name? compsci 101, fall 2017

Use a dictionary/map www.bit.ly/101f17-1024-5 Map first name to set of corresponding last names def mapNameToSetLastNames(data): compsci 101, fall 2017

Compare Using two parallel lists? Using one dictionary/map Which dictionary is most useful to solve the most popular name problem? compsci 101, fall 2017