CSC 131: Introduction to Computer Science

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
CSc 120 Introduction to Computer Programing II
Would you like to donate materials?
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Course Overview - Database Systems
Katherine Kampf / kkampf
Creating Databases Local storage. join & split
CSC 131: Introduction to Computer Science
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Topic: Conditional Statements – Part 1
Tuples and Lists.
CSC 131: Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to Computer Science II
CSc 120 Introduction to Computer Programing II
Department of Computer Science,
CSC 131: Introduction to Computer Science
CSC 131: Introduction to Computer Science
CSC 131: Introduction to Computer Science
Announcements Project 4 due Wed., Nov 7
CSC 131: Introduction to Computer Science
CSC 108H: Introduction to Computer Programming
CSC 131: Introduction to Computer Science
Ruth Anderson CSE 140 University of Washington
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
LING 388: Computers and Language
Course Overview - Database Systems
Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Dictionaries
Ruth Anderson UW CSE 160 Winter 2017
Physics 215–Elementary Modern Physics
CS2110: Software Development Methods
Accelerated Introduction to Computer Science
CS 1111 Introduction to Programming Fall 2018
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Michael Ernst CSE 140 University of Washington
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Open House Apex High School
CHAPTER 4: Lists, Tuples and Dictionaries
Python for Informatics: Exploring Information
CSC 380: Design and Analysis of Algorithms
Ruth Anderson CSE 160 University of Washington
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Unit 3: Variables in Java
Ruth Anderson UW CSE 160 Spring 2018
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Introduction to Dictionaries
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CSC 131: Introduction to Computer Science Dr. Curry Guinn

Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zachary Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab

CSC Tutoring Lab! A free CSC Tutoring Lab has been created this semester for CSC 131, CSC 133, and CSC 231. It will be located in CIS 2004 on the following schedule: The tutors are Steve Lawrence, Tori Pruim, and Daniel Orsa. At least 2 tutors will be present during those hours. Monday 5pm-9pm Tuesday 6pm-9pm Wednesday 5pm-8pm Thursday

Extra Credit Computer Science Circles -The Centre for Education in Mathematics and Computing (supplemental text) A free online interactive text  http://cscircles.cemc.uwaterloo.ca/using-this-website/ Each student should create an account using your first and last name and UNCW email, then go to "Edit My Profile" in the user menu at top right, and set the guru to be CurryGuinn so I can give you credit for your homework and feedback on your progress. For this extra credit, do all of the problems between 0: Hello! up to and including 9: Else, And, Or, Not Due date: Friday, November 3, 2017

For Next Class, Tuesday Quiz 10 due Monday, Oct 30 Homework 7is due, tonight Thursday, October 26. Don’t forget that Extra Credit is due Friday, November 3.

Today Dictionaries Sets

What is a “dictionary” in Python? A collection or bag of data. Not in any order Can be used to store and retrieve items VERY quickly based on a “key” Other languages have “dictionaries” Php and perl call them associative arrays Java calls them maps or HashMaps C# and .Net call them Property Bags

We “index” things by a lookup tag or key change = dict() # or change = {} change['quarter'] = 25 change['dime'] = 10 change['nickel'] = 5 change['penny'] = 1 print(change) value = change['quarter'] print('quarter:', value)

Lists vs. Dictionaries Lists index things by an integer which represents their order in a sequence Dictionaries index things by a key value

Dictionaries Some syntactic things to notice Dictionaries are surrounded by curly braces You can give a dictionary initial values this way: change = { 'quarter' : 25, 'dime' : 10, 'nickel' : 5, 'penny' : 1} An empty dictionary is myBook = {}

Possible keys So far, all of our keys have been strings like “quarter” or “nickel” Keys can be any immutable value: Integers Strings Tuples You can use floats, but be very careful. Errors in rounding can result in a different key value 3.999999999999 != 4.0

Retrieving a value associated with a key value = change[‘nickel’] But the following will throw an exception: Value = change[‘ruble’] Since ‘ruble’ hasn’t been entered as key What’s the fix? You can always ask if a key is in a dictionary if ‘ruble’ in change: Or use the get method: value = change.get(‘ruble’) “value” will be None if ‘ruble’ is not in the dictionary

Suppose we want to store a name with an ID number I have a file with <ID NUMBER> <NAME> Let’s parse it. Use the ID NUMBER as the key, and the name as the value. Download database.txt

Open, parse, add to dictionary datafile = open('database.txt', 'r') database = {} for line in datafile: tokens = line.split() idNumber = int(tokens[0]) name = tokens[1] + ' ' + tokens[2] database[idNumber] = name print("How many people? ", len(database))

Looking up someone in the database lookup = int(input("Enter an ID number: ")) if lookup in database: print(lookup, database[lookup]) else: print("There is no person with the id ", lookup)

Why is this better than using a list? We could have implemented this with a list something like this: datafile = open('database.txt', 'r') aList = [] for line in datafile: tokens = line.split() idNumber = int(tokens[0]) name = tokens[1] + ' ' + tokens[2] aList.append( (idNumber, name) )

Searching with the list would look like this: lookup = int(input("Enter an ID number: ")) found = False i = 0 while i < len(aList) and not found: idNum, name = aList[i] if idNum == lookup: print(idNum, name) found = True i += 1 if not found: print("there is no person with the id ", lookup)

Which is better? Functionally, they do the same thing. But the dictionary code will be much, much, much faster. Let’s time them and see.

How long does it take to do 1,000 lookups? start = time.time() print(start) for count in range(1000): found = False if lookup in database: name = database[lookup] found = True #print(count) endTime = time.time() print(endTime) print("Dictionary: It took %.15f seconds." % (endTime - start))

And with a list? start = time.time() for count in range(1000): found = False i = 0 while i < len(aList) and not found: idNum, name = aList[i] if idNum == lookup: found = True i += 1 endTime = time.time() print("List: It took %.15f seconds." % (endTime - start))

Sets A set is a collection that is not ordered and contains no duplicates Just like in math! hereIsASet = {5, 9, 4, 3, 2} You can convert a list to a set: myList = [4, 4, 8, 9, 10] mySet = set(myList) Or a set to a list list2 = list(mySet)

The Set Data Type A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 9.2 Set Data Type

Some code Suppose you want to count the number of words in a document Suppose you want to know the total number as well as the number of unique words.

The beginning filename = input('What is the name of the file? ') file = open(filename, 'r') allWords = [] for line in file: tokens = line.split() for w in tokens: allWords.append(w) print("The total number of words is ", len(allWords)) print("The number of unique words is ", len(set(allWords)))

What are some problems? Are “The” and “the” counted as different unique words? How can we fix this problem? What about words that have punctuation at the end like “at the market.” ? Is “market.” different than “market”? Either split() normally and then write a method to remove end punctuation Or use re.split which allows you to specify multiple characters to split on.

For Next Class, Tuesday Quiz 10 due Monday, Oct 30 Homework 7is due, tonight Thursday, October 26.