Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Dictionaries: Keeping track of pairs
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Classes 3 COMPSCI 105 S Principles of Computer Science.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Built-in Data Structures in Python An Introduction.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
CS261 Data Structures Maps (or Dictionaries). Goals Introduce the Map(or Dictionary) ADT Introduce an implementation of the map with a Dynamic Array.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
TUTORIAL 8 Generative Recursion. Reminders  Deadline of midterm remark  Assignment 7 due Wed, Mar 18 th.
Queen Elizabeth II 1926 Elizabeth is Born 1947 Princess Elizabeth marries prince philip 1947 Princess Elizabeth marries prince philip 1953 the Coronation.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
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.
Lists of Dictionaries Combining Data Storage Types.
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.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
Maps Nick Mouriski.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due FRIDAY, Dec 4 at 4:00 pm.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
Chapter 16: Linked Lists.
10 - Python Dictionary John R. Woodward.
COMPSCI 107 Computer Science Fundamentals
Working with Collections of Data
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
From Think Python How to Think Like a Computer Scientist
Announcements Project 4 due Wed., Nov 7
Introduction to Object-Oriented Programming (OOP) II
String and Lists Dr. José M. Reyes Álamo.
Introduction to Dictionaries
Heaps By JJ Shepherd.
11.6 Systems of Equations.
Lisp.
Introduction to Object-Oriented Programming (OOP) II
Dictionary.
Introduction to Computer Science
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Tutorial 10 Dictionaries and Classes

Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)

Review Dictionary Classes ◦ __init__ ◦ __repr__ ◦ __eq__

Dictionary {key1:value1, key2:value2, …} Each element has a key (a way to look up info) and a value associated with the key Unordered list (with each element being a key- value pair) Like a dictionary (word = key, definition = value)

Useful dictionary functions D[k] -> Get the value of k D[k] = v -> Add key-value pair or change value to be v if k already exists in D D.keys() -> Creates a list of all the keys in D D.values() -> Creates a list of all the values in D D.pop(k) -> Removes key-value pair of k from D and produces the value of k k in D -> produce True if k is a key in D

Classes Python’s version of structures in Scheme Allows related info to be grouped together We’ll use __init__, __repr__, and __eq__ with the class

__init__ class name: def __init__(self, f1, f2, …): self.field1 = f1 self.field2 = f2... … Creates the class Call the fields by: x=name(field1,field2,…) x.field1 Scheme’s version: (define-struct name (field1 field2 …)) (name-field1 x) Field of class you want to use

__repr__ def __repr__(self): return “name: {0},{1},…”\.format(self.field1, self.field2,…) If we try to print a class object, we’d get something like We can print a more informative message using the __repr__ command within the class definition You can put the class representation into any form you like, so long as you understand what each field is

__eq__ def __eq__(self, other): return isinstance(other, name) and \ self.field1 = other.field1 and\ self.field2 = other.field2 and If two classes have the same fields (and are not aliases), it is used to ensure that they produce True.

Question 1 – list_multiples Consumes a string s and produces a list containing every character in s that appears more than once in sorted order. Use dictionaries. Examples: list_multiples(“abcd”) => [] list_multiples(“bacaba”) => [a,b] list_multiples(“gtddyucaadsa”) => [“a”,”d”]

Question 2 - xor Write a function xor that consumes two dictionaries ( d1 and d2 ) and produces a dictionary. The produced dictionary will contain all the keys that appear in one of d1 and d2 but not both. The value associated with each key will be the same as the one found in the original dictionary.

Examples If d1 = {1:'a', 2:'b', 3:'c', 4:'d'} and d2 = {5:'e', 6:'f', 7:'g', 8:'h'} Then xor(d1,d2) => {1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h'} If d3 = {5:’q', 6:’l', 7:’c', 8:’e'} Then xor(d2,d3) => {}

Class Definition The remaining questions will use the following class: - A Person is a class with fields name, parents, and children -name is a non-empty string representing the person's name -parents is a list of at most two Person objects representing the person's parents (if the parents are unknown, the list will be empty) -children is a list of Person objects representing the person's children

Examples: george = Person("George", [], [elizabeth] ) elizabeth = Person("Elizabeth", [george], [charles, andrew, edward]) philip = Person("Philip", [], [charles, andrew, edward]) charles = Person("Charles", [elizabeth, philip], [] ) andrew = Person("Andrew", [philip, elizabeth],[]) edward = Person("Edward", [philip, elizabeth],[])

Question 4 – full_siblings Write a function full_siblings which consumes two Person objects and produces True if they have the same set of known parents (i.e. their only parent is the same, or their two parents are the same - though possibly in a different order), and False otherwise.

Question 5 – no_kids Write a function no_kids which consumes a list of Person objects and produces the number of people who have no children. Write the solution using a loop.

Question 6 – new_child Consumes a string new_name representing the name of a newly born child, and two Person objects ( parent1 and parent2 ) representing the child’s parents Does two things: ◦ Produces a new person structure to represent the child ◦ Updates the ‘children’ field of each parent to include the new child