**$ 18.00**

# Homework 5 | Complete Solution

- ExpertT
- Rating : 109
- Grade :
**A+** - Questions : 0
- Solutions : 1026
- Blog : 0
- Earned : $52966.04

**Problem 1 (15 points): Stack**

For this exercise, you will be coding a Stack class.

Stack is anotherfundamental computer science data structure. A stack implements the last-infirst-out (LIFO) mechanism. You can read up on stack at Wikipedia if you'dlike to learn more. A stack has two principal operations:

1. push: adds an element to the collection;

2. pop: removes the last element that was added.In your Stack class,

you will need three methods:__init__: initialize your Stack (think: how will you store the stack's elements?You'll need to initialize an appropriate object attribute in this method. Initially,the stack should contain nothing.)push: inserts one element in your Stackpop: pops one element from your Stack and returns it. If the stack is empty,raises an EmptyStackError with the error message “popfromanemptystack”.

When you're done, you should test your implementation by including thefollowing lines of code in your program.

stack = Stack()

stack.push(5)

stack.push (6)

print stack.pop ()

stack.push(7)

print stack.pop()

print stack.pop () (one space between "pop" and "()")

print stack.pop () (two spaces between "pop" and "()")

Your results should look something like this:

6

7

5

Traceback (most recent call last): File "", line26, in File “stack.py", line 15, in removeraise EmptyStackError(‘pop from an empty stack’)EmptyStackError: pop from an empty stack

Problem 2 (15 points):Rational Numbers Library

You are to write an object-oriented library of rational numbers, represented bythe pair of integers (nominator and denominator). The skeleton code is as follows:

class Rational:

"rational numbers"

def __init__(self, nom, denom):

self.nom = nom

self.denom = denom

self.reduce()

def __str__(self):

return str(self.nom) + "/" + str(self.denom)

where reduce() is a method of the class that reduces the rational number tolowest terms (e.g., if input nom and denom are 2 and 4, you should reduce thenom to 1 and denom to 2 because 2 and 4 have a greatest common divisor 2).You are to implement the operators shown in next page (p and q representrational objects and i represents an integer).

Caveats.

1. For all operations, the resulting rational number should already be in itslowest terms.

2. The resulting denominator cannot be negative (e.g., 2/-3 should be writtenas -2/3).

3. In case the denominator is 0, you'd raise an exception ofZeroDenominatorError with the error message "Denominator is zero!”.Therefore, you need to modify the __init_ method above to handle the casewhen the denominator is 0.

4. The first 9 operators should return a new Rational object, the __cmp__method should return True or False and the last 4 operators should modify theinternal data of the object p and return self.

5. You only need to submit your code of the class definition. Some examples to test your code assuming your code is saved in rational.py.

>>> from rational import *

>>> Rational(1,2) - Rational(2,1)-3/2

>>> Rational(1,2) * Rational(2,3)1/3

>>> print Rational(1,2) / ~Rational(2,-4)-1/4

>>> p = Rational(1,2)>>> p *= Rational(2,5)

>>> print p1/5

>>> 5 + (-Rational(1,2))9/2

>>> Rational(2,4) ** 101/1024

## [Solved] Homework 5 | Complete Solution

- This solution is not purchased yet.
- Submitted On 03 Nov, 2015 04:37:15

- ExpertT
- Rating : 109
- Grade :
**A+** - Questions : 0
- Solutions : 1026
- Blog : 0
- Earned : $52966.04