1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods –...

15
15 Rational numbers in Python Strange rounding-off errors: Create class for representing rational numbers by storing numerator and denominator >>> 2/3.0 0.6666666666666663 >>> 2/6.0 0.3333333333333331 >>> 5/6.0 0.8333333333333337

Transcript of 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods –...

Page 1: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

15

Rational numbers in Python

• Strange rounding-off errors:

• Create class for representing rational numbers by storing numerator and denominator

>>> 2/3.00.6666666666666663>>> 2/6.00.3333333333333331>>> 5/6.00.8333333333333337

Page 2: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

16

Rational numbersra

tiona

l.py

• Use float to make sure result of division will be a float, not

a rounded down integer

• Object reference self first argument in all methods

• self refers to the object on which the method is called

Page 3: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

17

Creating and using RationalNumber objects

>>> from rational import RationalNumber>>> a = RationalNumber(3, 4)>>> b = RationalNumber(3, 6)>>> a.getValue()0.75>>> b.getValue()0.5>>> b.getNumerator()1>>> b.getDenominator()2

No self argument in method calls!

Python puts object reference in place of self

Page 4: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

18

Forgetting the self. prefix when referencing object attributes

Initializes only local variables which go out of scope when constructor terminates!

Page 5: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

19

Binary search tree

a

elements < a

elements >= a

Page 6: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

20

Binary search tree

a

elements < b < a

b <= elements < a

b

elements >= a

elements < a

Page 7: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

21

Binary search tree of rational numbers

Page 8: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

22bina

ry_t

ree.

py

Node class from course

notes

Use this class to represent the search tree nodes:

Each data attribute will be a RationalNumber

Page 9: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

23sear

chtr

ee.p

y (p

art 1

)Creating RationalNumber objects, adding them to a tree

Page 10: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

24sear

chtr

ee.p

y (p

art 2

)Adding a node to a binary search tree

Recursive function, keeps order in tree

Page 11: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

25

Testing

%:~ python –i searchtree.pyNumerator: 2Denominator: 3Numerator: 3Denominator: 4Numerator: 7Denominator: 14Numerator:>>> root.getData().getvalue()0.666666666666663>>> root.getRight().getData().getvalue()0.75>>> root.getLeft().getData().getvalue()0.50

Page 12: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

26sear

chtr

ee.p

y (p

art 3

)Searching for a node

Fast searching if tree is balanced

Page 13: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

27sear

chtr

eete

st.p

y Searching for a node, demonstration

Search in big tree of random rational numbers

Page 14: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

28

Numerator: 2Denominator? 3Closest match: 433/650Numerator: 4Denominator? 5Closest match: 43/54Numerator: 1Denominator? 2Closest match: 370/737Numerator: 4Denominator? 9Closest match: 391/878Numerator: 10Denominator? 1Closest match: 994/103Numerator: 5Denominator? 3Closest match: 521/313

Page 15: 1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.

29

.. on to the exercises