CompSci 101 Introduction to Computer Science Feb 24, 2015 Prof. Rodger.

Slides:



Advertisements
Similar presentations
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Advertisements

CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
CompSci 101 Introduction to Computer Science Feb 26, 2015 Prof. Rodger.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
CompSci 6 Introduction to Computer Science October 20, 2011 Prof. Rodger.
CS 103 Discrete Structures Lecture 10 Basic Structures: Sets (1)
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
More Strings CS303E: Elements of Computers and Programming.
CompSci 6 Introduction to Computer Science November 8, 2011 Prof. Rodger.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 3, 2015 Prof. Rodger compsci101 spring151.
Compsci 101.2, Fall Plan for TDAFB (after fall break) l Hear via about plans to catch-up and stay-ahead  Profs. Rodger and Astrachan to.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
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 22 l Images, tuples, RGB color model  Image processing by understanding API  Image processing with tuples.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
CompSci 101 Introduction to Computer Science February 11, 2016 Prof. Rodger.
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 101 Introduction to Computer Science February 25, 2016 Prof. Rodger compsci101 spring20161.
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 April 7, 2015 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
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.
Numbers and Sets. A set is a collection of objects. So, any collection of things, such as numbers, can be called a set. To show that we have a set, we.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CS2005 Week 7 Lectures Set Abstract Data Type.
Sets Set is an unordered list of items
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Ruth Anderson CSE 140 University of Washington
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2017
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CompSci 101 Introduction to Computer Science
Michael Ernst CSE 140 University of Washington
Spin the wheel and click the right word
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Sets and Dictionaries
Ruth Anderson CSE 160 University of Washington
Bananas apples peaches pears.
Ruth Anderson UW CSE 160 Spring 2018

Sets CS3240, L. grewe.
Introduction to PYTHON
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CompSci 101 Introduction to Computer Science Feb 24, 2015 Prof. Rodger

Announcements Reading and RQ11 due next time Assignment 5 out today - Hangman APT 4 due, APT 5 out Exam 1 back today –Exam regrades go to Prof. Rodger –Write on the front the problem and what the issue is Today –sets –Finish lecture notes from last time

More on List Comprehensions What is the list for the following: 1) [j+1 for j in range(20) if (j%3) == 0] 2) [i*2 for i in [j+1 for j in range(20) if (j%3) == 0] if i*i > 19] compsci101 spring2015

More on List Comprehensions bit.ly/101S Problem: Given a list of strings, return the longest string. If there are more than one of that length, return the first such one. [‘kiwi’, ‘plum’, ‘orange’, ‘lemon’, ‘banana’] Write a list comprehension for this problem compsci101 spring2015

Python Sets Set – unordered collection of distinct items –Unordered – can look at them one at a time, but cannot count on any order –Distinct - one copy of each Operations on sets: –Modify: add, clear, remove –Create a new set: difference(-), intersection(&), union (|), symmetric_difference(^) –Boolean: issubset = Can convert list to set, set to list –Great to get rid of duplicates in a list

Summary (from wikibooks) set1 = set() # A new empty set set1.add("cat") # Add a single member set1.update(["dog", "mouse"]) # Add several members set1.remove("cat“) # Remove a member - error if not there print set1 for item in set1: # Iteration or “for each element” print item print "Item count:", len(set1) # Length, size, item count isempty = len(set1) == 0 # Test for emptiness set1 = set(["cat", "dog"]) # Initialize set from a list set3 = set1 & set2 # Intersection set4 = set1 | set2 # Union set5 = set1 - set3 # Set difference set6 = set1 ^ set2 # Symmetric difference (elements in either set but not both) issubset = set1 <= set2 # Subset test issuperset = set1 >= set2 # Superset test set7 = set1.copy() # A shallow copy (copies the set, not the elements) set8.clear() # Clear, empty, erase

Creating and changing a set See setsEasy.py

Set Operations See setsEasy.py

Set Examples bit.ly/101S poloClub = set(['Mary', 'Laura', 'Dell']) rugbyClub = set(['Fred', 'Sue', 'Mary']) Question 1: print [w for w in poloClub.intersection(rugbyClub)] Question 2: print [w for w in poloClub.union(rugbyClub)]

More Set Examples bit.ly/101S lista = ['apple', 'pear', 'fig', 'orange', 'strawberry'] listb = ['pear', 'lemon', 'grapefruit', 'orange'] listc = [x for x in lista if x in listb] listd = list(set(lista)|set(listb)) Question 1: print listc Question 2: print listd