Cashback Offer from 10th to 18th May 2021. Get Flat 20% Cashback credited to your account for a minimum transaction of \$80. Post Your Question Today!

Question DetailsNormal
\$ 18.00

# Homework 5 | Complete Solution

Question posted by

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

\$ 18.00

## [Solved] Homework 5 | Complete Solution

• This solution is not purchased yet.
• Submitted On 03 Nov, 2015 04:37:15
def gcd(a, b): """returns the greatest common divisor of a and b""" if b == 0: return a else: return gcd(b, a % b) class Ratio...
Buy now to view the complete solution
Attachment
Other Similar Questions
Symone

### Homework Assignment AA

Receive excel file too Homework Assignment AA all at 10 ...
Symone

### Homework Assignment AA

Kindly receive your completed Homework Assignment AA. There are two files namely word containing description and an excel file containing calculation of ratios...
quizh...

### Homework Assignment 3

Employee Salary Department dep1 dep2 dep3 dep4 3 \$32,782 1 \$32,782 \$35,467 \$23,654 \$65,487 8 \$32,920 1 \$32,920 \$35,468 \$36,578 \$46,184 9 \$29,548 1 \$29,548 \$29,876 \$37,548 \$54,899 18 \$39,828 1 \$39,828 \$43,674 \$53...
DEEPE...

### Stat Homework Help

STAT 200 Week 7 Homework Problems: 10.1.2 Table #10.1.6 contains the value of the house and the amount of rental income in a year that the house brings in ("Capital and rental," 2013). Create a scatter plot and find a reg...
Symone