Rational Number Arithmetic Implementation def mul_rational(x, y): return rational(numer(x) * numer(y), nx ny nx*ny denom(x) * denom(y)) * = Constructor dx dy dx*dy Selectors Selectors def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) nx ny nx*dy + ny*dx def print_rational(x): + = print(numer(x), '/', denom(x)) dx dy dx*dy • rational(n, d) returns a rational number x These functions implement an • numer(x) returns the numerator of x abstract representation for rational numbers • denom(x) returns the denominator of x � 7
Rational Number Arithmetic Implementation def mul_rational(x, y): return rational(numer(x) * numer(y), nx ny nx*ny denom(x) * denom(y)) * = Constructor dx dy dx*dy Selectors Selectors def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) nx ny nx*dy + ny*dx def print_rational(x): + = print(numer(x), '/', denom(x)) dx dy dx*dy def rationals_are_equal(x, y): return numer(x) * denom(y) == numer(y) * denom(x) • rational(n, d) returns a rational number x These functions implement an • numer(x) returns the numerator of x abstract representation for rational numbers • denom(x) returns the denominator of x � 7
Pairs
Representing Pairs Using Lists � 9
Representing Pairs Using Lists >>> pair = [1, 2] � 9
Representing Pairs Using Lists >>> pair = [1, 2] >>> pair [1, 2] � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair >>> x 1 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair >>> x 1 >>> y 2 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 � 9
Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem Element selection function >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 � 9
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] � 10
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list � 10
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] � 10
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] � 10
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] Select item from a list � 10
Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] Select item from a list (Demo) � 10
Reducing to Lowest Terms Example: � 11
Reducing to Lowest Terms Example: 3 5 * 2 3 � 11
Reducing to Lowest Terms Example: 3 5 5 * = 2 3 2 � 11
Reducing to Lowest Terms Example: 3 5 5 * = 2 3 2 15 1/3 5 * = 6 1/3 2 � 11
Reducing to Lowest Terms Example: 2 1 3 5 5 * = + 5 10 2 3 2 15 1/3 5 * = 6 1/3 2 � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 * = 6 1/3 2 � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd Greatest common divisor def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] � 11
Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd Greatest common divisor def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] (Demo) � 11
Abstraction Barriers
Abstraction Barriers � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers to perform computation � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers whole data values to perform computation � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement rational operations � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational operations denominators � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and constructor for rationals � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists constructor for rationals � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals Implementation of lists � 13
Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals Implementation of lists � 13
Violating Abstraction Barriers add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14
Violating Abstraction Barriers Does not use constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14
Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14
Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] No selectors! � 14
Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] No selectors! And no constructor! � 14
Violating Abstraction Barriers � 14
Recommend
More recommend