Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 131: Introduction to Computer Science

Similar presentations


Presentation on theme: "CSC 131: Introduction to Computer Science"— Presentation transcript:

1 CSC 131: Introduction to Computer Science
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu
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

3 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

4 Extra Credit Computer Science Circles -The Centre for Education in Mathematics and Computing (supplemental text) A free online interactive text  Each student should create an account using your first and last name and UNCW , 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

5 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.

6 Today Dictionaries Sets

7 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

8 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)

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

10 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 = {}

11 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 != 4.0

12 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

13 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

14 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))

15 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)

16 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) )

17 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)

18 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.

19 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))

20 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))

21 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)

22 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

23 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.

24 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)))

25 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.

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


Download ppt "CSC 131: Introduction to Computer Science"

Similar presentations


Ads by Google