61A Lecture 8 Wednesday, September 14 Sunday, September 11, 2011 Data Abstraction 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude • An abstract data type lets us manipulate compound objects as units 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude • An abstract data type lets us manipulate compound objects as units • Isolate two parts of any program that uses data: 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude • An abstract data type lets us manipulate compound objects as units • Isolate two parts of any program that uses data: How data are represented (as parts) 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude • An abstract data type lets us manipulate compound objects as units • Isolate two parts of any program that uses data: How data are represented (as parts) How data are manipulated (as units) 2 Sunday, September 11, 2011 Data Abstraction • Compound objects combine primitive objects together • A date: a year, a month, and a day • A geographic position: latitude and longitude • An abstract data type lets us manipulate compound objects as units • Isolate two parts of any program that uses data: How data are represented (as parts) How data are manipulated (as units) • Data abstraction: A methodology by which functions enforce an abstraction barrier between representation and use 2 Sunday, September 11, 2011 Data Abstraction • A date: a year, a month, and a day • A geographic position: latitude and longitude All Programmers • Compound objects combine primitive objects together • An abstract data type lets us manipulate compound objects as units • Isolate two parts of any program that uses data: How data are represented (as parts) How data are manipulated (as units) • Data abstraction: A methodology by which functions enforce an abstraction barrier between representation and use 2 Sunday, September 11, 2011 Data Abstraction • A date: a year, a month, and a day • A geographic position: latitude and longitude All Programmers • Compound objects combine primitive objects together • An abstract data type lets us manipulate compound objects as units How data are represented (as parts) How data are manipulated (as units) Great Programmers • Isolate two parts of any program that uses data: • Data abstraction: A methodology by which functions enforce an abstraction barrier between representation and use 2 Sunday, September 11, 2011 Rational Numbers 3 Sunday, September 11, 2011 Rational Numbers numerator denominator 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: • make_rat(n, d) returns a rational number x 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: Constructor • make_rat(n, d) returns a rational number x 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: Constructor • make_rat(n, d) returns a rational number x • numer(x) returns the numerator of x 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: Constructor • make_rat(n, d) returns a rational number x • numer(x) returns the numerator of x • denom(x) returns the denominator of x 3 Sunday, September 11, 2011 Rational Numbers numerator denominator Exact representation of fractions A pair of integers As soon as division occurs, the exact representation is lost! Assume we can compose and decompose rational numbers: Constructor Selectors • make_rat(n, d) returns a rational number x • numer(x) returns the numerator of x • denom(x) returns the denominator of x 3 Sunday, September 11, 2011 Rational Number Arithmetic Example: General Form: 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 2 5 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 2 9 = 5 10 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 2 9 nx = 5 ny * 10 dx dy 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 2 9 nx = 5 ny * 10 dx nx*ny = dy dx*dy 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 9 nx = 2 5 3 3 ny * 10 dx nx*ny = dy dx*dy + 2 5 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 9 = 5 10 3 3 21 + ny * 2 2 nx dx nx*ny = dy dx*dy = 5 10 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 9 nx = ny * = 2 5 10 dx dy 3 3 21 nx ny + 2 = 5 nx*ny dx*dy + 10 dx dy 4 Sunday, September 11, 2011 Rational Number Arithmetic Example: 3 General Form: 3 * 9 nx = ny * nx*ny = 2 5 10 dx dy dx*dy 3 3 21 nx ny nx*dy + ny*dx + 2 = 5 + 10 dx = dy dx*dy 4 Sunday, September 11, 2011 Rational Number Arithmetic Implementation 5 Sunday, September 11, 2011 Rational Number Arithmetic Implementation • • • make_rat(n, d) returns a rational number x numer(x) returns the numerator of x denom(x) returns the denominator of x 5 Sunday, September 11, 2011 Rational Number Arithmetic Implementation Wishful thinking • • • make_rat(n, d) returns a rational number x numer(x) returns the numerator of x denom(x) returns the denominator of x 5 Sunday, September 11, 2011 def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) Rational Number Arithmetic return make_rat(nx * dy Implementation + ny * dx, dx * dy) def mul_rat(x, y): """Multiply rational numbers x and y.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def equal_rat(x, y): """Return whether rational numbers x and y are equal.""" return numer(x) * denom(y) == numer(y) * denom(x) Wishful thinking • • • make_rat(n, d) returns a rational number x numer(x) returns the numerator of x denom(x) returns the denominator of x 5 Sunday, September 11, 2011 def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) Rational Number Arithmetic return make_rat(nx * dy Implementation + ny * dx, dx * dy) def mul_rat(x, y): """Multiply rational numbers x and y.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def equal_rat(x, y): Constructor """Return whether rational numbers x and y are equal.""" return numer(x) * denom(y) == numer(y) * denom(x) Wishful thinking • • • make_rat(n, d) returns a rational number x numer(x) returns the numerator of x denom(x) returns the denominator of x 5 Sunday, September 11, 2011 def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) Rational Number Arithmetic return make_rat(nx * dy Implementation + ny * dx, dx * dy) def mul_rat(x, y): """Multiply rational numbers x and y.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def equal_rat(x, y): Constructor Selectors """Return whether rational numbers x and y are equal.""" return numer(x) * denom(y) == numer(y) * denom(x) Wishful thinking • • • make_rat(n, d) returns a rational number x numer(x) returns the numerator of x denom(x) returns the denominator of x 5 Sunday, September 11, 2011 def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) Rational Number Arithmetic return make_rat(nx * dy Implementation + ny * dx, dx * dy) def mul_rat(x, y): """Multiply rational numbers x and y.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def equal_rat(x, y): Constructor Selectors """Return whether rational numbers x and y are equal.""" 8_rat.py return numer(x) * denom(y) == numer(y) * denom(x) def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return make_rat(nx * dy + ny * dx, dx * dy) def mul_rat(x, y): """Multiply rational numbers x and y.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def equal_rat(x, y): returnsx aand rational number x """Return whether rationald)numbers y are equal.""" • make_rat(n, return numer(x) * denom(y) == numer(y) * denom(x) Wishful numer(x) returns the numerator of x • thinking • denom(x) returns the denominator of x 5 Sunday, September 11, 2011 def add_rat(x, y): """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) Rational Number Arithmetic return make_rat(nx * dy Implementation + ny * dx, dx * dy) 8_rat.py def mul_rat(x, y): """Multiply rational numbers x and y.""" # Arithmetic return make_rat(numer(x) * numer(y), denom(x) * denom(y)) def add_rat(x, y): def """Add equal_rat(x, y): numbers x and Selectors rational y.""" Constructor """Return whether rational 8_rat.py nx, dx = numer(x), denom(x)numbers x and y are equal.""" return * denom(y) ny, dy numer(x) = numer(y), denom(y)== numer(y) * denom(x) def return add_rat(x, y): make_rat(nx * dy + ny * dx, dx * dy) """Add rational numbers x and y.""" nx, dx = numer(x), denom(x) def mul_rat(x, y): ny, dy = numer(y), denom(y) """Multiply rational numbers x and y.""" return make_rat(nx * dy + *nynumer(y), * dx, dx denom(x) * dy) make_rat(numer(x) * denom(y)) def eq_rat(x, mul_rat(x, y): y): def """Multiply rational numbersnumbers x and y.""" """Return whether rational x and y are equal.""" return make_rat(numer(x) * numer(y), denom(x) * denom(y)) return numer(x) * denom(y) == numer(y) * denom(x) def equal_rat(x, y): returnsx aand rational number x """Return whether rationald)numbers y are equal.""" • make_rat(n, # Constructor and selectors return numer(x) * denom(y) == numer(y) * denom(x) Wishful numer(x) returns the numerator of x • thinking from operator import getitem • denom(x) returns the denominator of x def make_rat(n, d): """Construct a rational number x that represents n/d.""" 5 return (n, d) Sunday, September 11, 2011 Tuples 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 1 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 Element selection 6 Sunday, September 11, 2011 Tuples >>> pair = (1, 2) >>> pair (1, 2) A tuple literal: Comma-separated expressions >>> x, y = pair >>> x 1 >>> y 2 "Unpacking" a tuple >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 Element selection More tuples next lecture 6 Sunday, September 11, 2011 Representing Rational Numbers 7 Sunday, September 11, 2011 return numer(x) * denom(y) == numer(y) * denom(x) # ConstructorRational and selectors Representing Numbers from operator import getitem def make_rat(n, d): """Construct a rational number x that represents n/d.""" return (n, d) def numer(x): """Return the numerator of rational number x.""" return getitem(x, 0) def denom(x): """Return the denominator of rational number x.""" return getitem(x, 1) # String conversion def str_rat(x): """Return a string 'n/d' for numerator n and denominator d.""" return '{0}/{1}'.format(numer(x), denom(x)) # Improved constructor from fractions import gcd def make_rat(n, d): 7 """Construct a rational number x that represents n/d in lowest Sunday, September 11,g 2011= gcd(n, d) return numer(x) * denom(y) == numer(y) * denom(x) # ConstructorRational and selectors Representing Numbers from operator import getitem def make_rat(n, d): """Construct a rational number x that represents n/d.""" return (n, d) def numer(x): Constructthe a tuple """Return numerator of rational number x.""" return getitem(x, 0) def denom(x): """Return the denominator of rational number x.""" return getitem(x, 1) # String conversion def str_rat(x): """Return a string 'n/d' for numerator n and denominator d.""" return '{0}/{1}'.format(numer(x), denom(x)) # Improved constructor from fractions import gcd def make_rat(n, d): 7 """Construct a rational number x that represents n/d in lowest Sunday, September 11,g 2011= gcd(n, d) return rational numer(x) numbers * denom(y) == y.""" numer(y) * denom(x) """Add x and nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) # Constructor and selectors return make_rat(nx * dy + ny * dx, dx * dy) Representing Rational Numbers frommul_rat(x, operator import getitem def y): """Multiply rational numbers x and y.""" def return make_rat(n, d): make_rat(numer(x) * numer(y), denom(x) * denom(y)) """Construct a rational number x that represents n/d.""" def equal_rat(x, return (n, d)y): """Return whether rational numbers x and y are equal.""" numer(x) * denom(y) == numer(y) * denom(x) def return numer(x): Construct a tuple """Return the numerator of rational number x.""" return getitem(x, 0) # Constructor and selectors def denom(x): from """Return operator import getitem of rational number x.""" the denominator return getitem(x, 1) def make_rat(n, d): """Construct a rational number x that represents n/d.""" return (n, d) # String conversion def numer(x): str_rat(x): """Return the numerator rational number x.""" a string 'n/d'of for numerator n and denominator d.""" return getitem(x, 0) '{0}/{1}'.format(numer(x), denom(x)) def denom(x): """Return the denominator of rational number x.""" # Improved constructor return getitem(x, 1) from fractions import gcd def make_rat(n, d): 7 # String conversion """Construct a rational number x that represents n/d in lowest Sunday, September 11,g 2011= gcd(n, d) return rational numer(x) numbers * denom(y) == y.""" numer(y) * denom(x) """Add x and nx, dx = numer(x), denom(x) def mul_rat(x, y): ny, dy = numer(y), denom(y) """Multiply rational numbers x and y.""" # Constructor and selectors return make_rat(numer(x) make_rat(nx * dy +* ny * dx, dxdenom(x) * dy) Representing Rational Numbers return numer(y), * denom(y)) fromequal_rat(x, operator import def mul_rat(x, y): def y): getitem """Multiply rational numbers x and x y.""" """Return whether rational numbers and y are equal.""" def return make_rat(n, d): * denom(y) return make_rat(numer(x) * == numer(y), denom(x) * denom(y)) numer(x) numer(y) * denom(x) """Construct a rational number x that represents n/d.""" def equal_rat(x, return (n, d)y): """Return and whether rational numbers x and y are equal.""" # Constructor selectors numer(x) * denom(y) == numer(y) * denom(x) def return numer(x): Construct a tuple """Return the numerator from operator import getitem of rational number x.""" return getitem(x, 0) # Constructor and selectors def make_rat(n, d): def """Construct denom(x): a rational number x that represents n/d.""" fromreturn operator getitem of rational number x.""" """Return the (n, import d) denominator return getitem(x, 1) def numer(x): make_rat(n, d): def """Construct rational of number x that represents """Return the anumerator rational number x.""" n/d.""" return (n, d) # String conversion return getitem(x, 0) def denom(x): numer(x): str_rat(x): def """Return the the numerator rational number x.""" a string 'n/d'of for numerator n and denominator d.""" """Return denominator of rational number x.""" return getitem(x, getitem(x, 0) '{0}/{1}'.format(numer(x), denom(x)) return 1) def denom(x): """Return the denominator of rational number x.""" Improved constructor ## String conversion return getitem(x, 1) fromstr_rat(x): fractions import gcd def def """Return make_rat(n, d): a string 'n/d' for numerator n and denominator d.""" 7 # String conversion """Construct a rational number x that represents n/d in lowest return '{0}/{1}'.format(numer(x), denom(x)) Sunday, September 11,g 2011= gcd(n, d) return rational numer(x) numbers * denom(y) == y.""" numer(y) * denom(x) """Add x and nx, dx = numer(x), denom(x) def mul_rat(x, y): ny, dy = numer(y), denom(y) """Multiply rational numbers x and y.""" # Constructor and selectors return make_rat(numer(x) make_rat(nx * dy +* ny * dx, dxdenom(x) * dy) Representing Rational Numbers return numer(y), * denom(y)) fromequal_rat(x, operator import def mul_rat(x, y): def y): getitem """Multiply rational numbers x and x y.""" """Return whether rational numbers and y are equal.""" def return make_rat(n, d): * denom(y) return make_rat(numer(x) * == numer(y), denom(x) * denom(y)) numer(x) numer(y) * denom(x) """Construct a rational number x that represents n/d.""" def equal_rat(x, return (n, d)y): """Return and whether rational numbers x and y are equal.""" # Constructor selectors numer(x) * denom(y) == numer(y) * denom(x) def return numer(x): Construct a tuple """Return the numerator from operator import getitem of rational number x.""" return getitem(x, 0) # Constructor and selectors def make_rat(n, d): def """Construct denom(x): a rational number x that represents n/d.""" fromreturn operator getitem of rational number x.""" """Return the (n, import d) denominator return getitem(x, 1) def numer(x): make_rat(n, d): def """Construct rational of number x that represents """Return the anumerator rational number x.""" n/d.""" return (n, d) # String conversion return getitem(x, 0) def denom(x): numer(x): str_rat(x): def """Return the the numerator rational number x.""" a string 'n/d'of for numerator n and denominator d.""" """Return denominator of rational number x.""" return getitem(x, getitem(x, 0) '{0}/{1}'.format(numer(x), denom(x)) return 1) def denom(x): """Return the denominator of rational number x.""" Select from a tuple Improved constructor ## String conversion return getitem(x, 1) fromstr_rat(x): fractions import gcd def def """Return make_rat(n, d): a string 'n/d' for numerator n and denominator d.""" 7 # String conversion """Construct a rational number x that represents n/d in lowest return '{0}/{1}'.format(numer(x), denom(x)) Sunday, September 11,g 2011= gcd(n, d) Reducing to Lowest Terms Example: 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 2 3 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 2 5 = 3 2 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 5 = 2 3 2 15 1/3 5 * 6 = 1/3 2 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 5 3 2 15 1/3 5 * 1 + = 2 6 2 5 10 = 1/3 2 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 5 3 2 15 1/3 5 * 1 + = 2 6 2 5 1 = 10 2 = 1/3 2 8 Sunday, September 11, 2011 Reducing to Lowest Terms Example: 3 5 * 5 2 1 + = 1 = 2 3 2 5 10 2 15 1/3 5 25 1/25 1 * 6 = 1/3 * 2 50 = 1/25 2 8 Sunday, September 11, 2011 # Constructor and selectors Reducing to Lowest Terms from operator import getitem def make_rat(n, d): """Construct a rational number x that represents n/d.""" Example: return (n, d) def numer(x): """Return the numerator of rational number x.""" 2 3 5 5 return getitem(x, 0) * 1 + = def denom(x): 5 10 2 3 denominator 2 of rational number """Return the x.""" return getitem(x, 1) # String conversion 15 1/3 5 25 1/25 1 = 2 1 def str_rat(x): * = * = """Return a string 'n/d' for numerator n and denominator d.""" 6 1/3 2 50 1/25 2 return '{0}/{1}'.format(numer(x), denom(x)) # Improved constructor from fractions import gcd def make_rat(n, d): """Construct a rational number x that represents n/d in lowest terms.""" g = gcd(n, d) return (n//g, d//g) 8 Sunday, September 11, 2011 # Constructor and selectors Reducing to Lowest Terms from operator import getitem def make_rat(n, d): """Construct a rational number x that represents n/d.""" Example: return (n, d) def numer(x): """Return the numerator of rational number x.""" 2 3 5 5 return getitem(x, 0) * 1 + = def denom(x): 5 10 2 3 denominator 2 of rational number """Return the x.""" return getitem(x, 1) # String conversion 15 1/3 5 25 1/25 1 = 2 1 def str_rat(x): * = * = """Return a string 'n/d' for numerator n and denominator d.""" 6 1/3 2 50 1/25 2 return '{0}/{1}'.format(numer(x), denom(x)) # Improved constructor from fractions import gcd def make_rat(n, d): """Construct a rational number x that represents n/d in lowest terms.""" g = gcd(n, d) return (n//g, d//g) Greatest common divisor 8 Sunday, September 11, 2011 Abstraction Barriers 9 Sunday, September 11, 2011 Abstraction Barriers Rational numbers in the problem domain add_rat mul_rat eq_rat 9 Sunday, September 11, 2011 Abstraction Barriers Rational numbers in the problem domain add_rat mul_rat eq_rat Rational numbers as numerators & denominators make_rat numer denom 9 Sunday, September 11, 2011 Abstraction Barriers Rational numbers in the problem domain add_rat mul_rat eq_rat Rational numbers as numerators & denominators make_rat numer denom Rational numbers as tuples tuple getitem 9 Sunday, September 11, 2011 Abstraction Barriers Rational numbers in the problem domain add_rat mul_rat eq_rat Rational numbers as numerators & denominators make_rat numer denom Rational numbers as tuples tuple getitem However tuples are implemented in Python 9 Sunday, September 11, 2011 Violating Abstraction Barriers add_rat( (1, 2), (1, 4) ) def divide_rat(x, y): return (x[0] * y[1], x[1] * y[0]) 10 Sunday, September 11, 2011 Violating Abstraction Barriers Does not use constructors add_rat( (1, 2), (1, 4) ) def divide_rat(x, y): return (x[0] * y[1], x[1] * y[0]) 10 Sunday, September 11, 2011 Violating Abstraction Barriers Does not use constructors Twice! add_rat( (1, 2), (1, 4) ) def divide_rat(x, y): return (x[0] * y[1], x[1] * y[0]) 10 Sunday, September 11, 2011 Violating Abstraction Barriers Does not use constructors Twice! add_rat( (1, 2), (1, 4) ) def divide_rat(x, y): return (x[0] * y[1], x[1] * y[0]) No selectors! 10 Sunday, September 11, 2011 Violating Abstraction Barriers Does not use constructors Twice! add_rat( (1, 2), (1, 4) ) def divide_rat(x, y): return (x[0] * y[1], x[1] * y[0]) No selectors! And no constructor! 10 Sunday, September 11, 2011 Violating Abstraction Barriers 10 Sunday, September 11, 2011 What is Data? 11 Sunday, September 11, 2011 What is Data? • We need to guarantee that constructor and selector functions together specify the right behavior. 11 Sunday, September 11, 2011 What is Data? • We need to guarantee that constructor and selector functions together specify the right behavior. • Rational numbers: If we construct x from n and d, then numer(x)/denom(x) must equal n/d. 11 Sunday, September 11, 2011 What is Data? • We need to guarantee that constructor and selector functions together specify the right behavior. • Rational numbers: If we construct x from n and d, then numer(x)/denom(x) must equal n/d. • An abstract data type is some collection of selectors and constructors, together with some behavior conditions. 11 Sunday, September 11, 2011 What is Data? • We need to guarantee that constructor and selector functions together specify the right behavior. • Rational numbers: If we construct x from n and d, then numer(x)/denom(x) must equal n/d. • An abstract data type is some collection of selectors and constructors, together with some behavior conditions. • If behavior conditions are met, the representation is valid. 11 Sunday, September 11, 2011 What is Data? • We need to guarantee that constructor and selector functions together specify the right behavior. • Rational numbers: If we construct x from n and d, then numer(x)/denom(x) must equal n/d. • An abstract data type is some collection of selectors and constructors, together with some behavior conditions. • If behavior conditions are met, the representation is valid. You can recognize data types by behavior, not by bits 11 Sunday, September 11, 2011 Behavior Conditions of a Pair 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? Constructors, selectors, and behavior conditions: 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? Constructors, selectors, and behavior conditions: If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? Constructors, selectors, and behavior conditions: If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. Together, selectors are the inverse of the constructor 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? Constructors, selectors, and behavior conditions: If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. Together, selectors are the inverse of the constructor Generally true of container types. 12 Sunday, September 11, 2011 Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple (also known as a pair). What is a pair? Constructors, selectors, and behavior conditions: If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. Together, selectors are the inverse of the constructor Generally true of container types. Sunday, September 11, 2011 Not true for rational numbers 12 Functional Pair Implementation 13 Sunday, September 11, 2011 return (n//g, d//g) Functional Pair Implementation # Functional pair def make_pair(x, y): """Return a functional pair.""" def dispatch(m): if m == 0: return x elif m == 1: return y return dispatch def getitem_pair(p, i): """Return the element at index i of pair p.""" return p(i) 13 Sunday, September 11, 2011 return (n//g, d//g) Functional Pair Implementation # Functional pair def make_pair(x, y): """Return a functional pair.""" def dispatch(m): if m == 0: This function return x represents a pair elif m == 1: return y return dispatch def getitem_pair(p, i): """Return the element at index i of pair p.""" return p(i) 13 Sunday, September 11, 2011 return (n//g, d//g) Functional Pair Implementation # Functional pair def make_pair(x, y): """Return a functional pair.""" def dispatch(m): if m == 0: This function return x represents a pair elif m == 1: return y return dispatch def getitem_pair(p, i): Constructor is a """Return the element at index i of pair p.""" higher-order function return p(i) 13 Sunday, September 11, 2011 fromreturn fractions import (n//g, d//g)gcd def make_rat(n, d): """Construct a rational number x that represents n/ Functional Pair Implementation g = gcd(n, d) # Functional pair return (n//g, d//g) def make_pair(x, y): """Return a functional pair.""" # Functional pair def dispatch(m): if m == 0: This function def make_pair(x, returny): x represents a pair """Return pair.""" elif m a==functional 1: def dispatch(m): return y if dispatch m == 0: return return x elif m == 1:i): def getitem_pair(p, Constructor is a y """Returnreturn the element at index i of pair p.""" higher-order function return p(i) dispatch return def getitem_pair(p, i): """Return the element at index i of pair p.""" return p(i) 13 Sunday, September 11, 2011 fromreturn fractions import (n//g, d//g)gcd def make_rat(n, d): """Construct a rational number x that represents n/ Functional Pair Implementation g = gcd(n, d) # Functional pair return (n//g, d//g) def make_pair(x, y): """Return a functional pair.""" # Functional pair def dispatch(m): if m == 0: This function def make_pair(x, returny): x represents a pair """Return pair.""" elif m a==functional 1: def dispatch(m): return y if dispatch m == 0: return return x elif m == 1:i): def getitem_pair(p, Constructor is a y """Returnreturn the element at index i of pair p.""" higher-order function return p(i) dispatch return def getitem_pair(p, i): """Return the element at index i of pair p.""" return p(i) Selector defers to the object itself 13 Sunday, September 11, 2011 Using a Functionally Implemented Pair >>> p = make_pair(1, 2) >>> getitem_pair(p, 0) 1 >>> getitem_pair(p, 1) 2 14 Sunday, September 11, 2011 Using a Functionally Implemented Pair >>> p = make_pair(1, 2) >>> getitem_pair(p, 0) 1 As long as we do not violate the abstraction barrier, we don't need to know that pairs are just functions >>> getitem_pair(p, 1) 2 14 Sunday, September 11, 2011 Using a Functionally Implemented Pair >>> p = make_pair(1, 2) >>> getitem_pair(p, 0) 1 As long as we do not violate the abstraction barrier, we don't need to know that pairs are just functions >>> getitem_pair(p, 1) 2 If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. 14 Sunday, September 11, 2011 Using a Functionally Implemented Pair >>> p = make_pair(1, 2) >>> getitem_pair(p, 0) 1 As long as we do not violate the abstraction barrier, we don't need to know that pairs are just functions >>> getitem_pair(p, 1) 2 If a pair p was constructed from values x and y, then • getitem_pair(p, 0) returns x, and • getitem_pair(p, 1) returns y. This pair representation is valid! 14 Sunday, September 11, 2011
© Copyright 2025 Paperzz