Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 8 - More on Strings and Special Methods - Programming Exercises - Page 269: 8.20

Answer

code

Work Step by Step

# 8.20 (Use the Rational class) Write a program that computes the following summation # series using the Rational class: # 1 / 2 + 2 / 3 + 3 / 4 + ... + 8 / 9 + 9 / 10 class Rational: def __init__(self, numerator=0, denominator=1): divisor = gcd(numerator, denominator) self.__numerator = (1 if denominator > 0 else -1) \ * int(numerator / divisor) self.__denominator = int(abs(denominator) / divisor) # Add a rational number to this rational number def __add__(self, secondRational): n = self.__numerator * secondRational[1] + \ self.__denominator * secondRational[0] d = self.__denominator * secondRational[1] return Rational(n, d) # Subtract a rational number from this rational number def __sub__(self, secondRational): n = self.__numerator * secondRational[1] - \ self.__denominator * secondRational[0] d = self.__denominator * secondRational[1] return Rational(n, d) # Multiply a rational number to this rational def __mul__(self, secondRational): n = self.__numerator * secondRational[0] d = self.__denominator * secondRational[1] return Rational(n, d) # Divide a rational number by this rational number def __truediv__(self, secondRational): n = self.__numerator * secondRational[1] d = self.__denominator * secondRational[0] return Rational(n, d) # Return a float for the rational number def __float__(self): return self.__numerator / self.__denominator # Return an integer for the rational number def __int__(self): return int(self.__float__()) # Return a string representation def __str__(self): if self.__denominator == 1: return str(self.__numerator) else: return str(self.__numerator) + "/" + str(self.__denominator) def __lt__(self, secondRational): return self.__cmp__(secondRational) < 0 def __le__(self, secondRational): return self.__cmp__(secondRational) <= 0 def __gt__(self, secondRational): return self.__cmp__(secondRational) > 0 def __ge__(self, secondRational): return self.__cmp__(secondRational) >= 0 # Compare two numbers def __cmp__(self, secondRational): temp = self.__sub__(secondRational) if temp[0] > 0: return 1 elif temp[0] < 0: return -1 else: return 0 # Return numerator and denominator using an index operator def __getitem__(self, index): if index == 0: return self.__numerator else: return self.__denominator def gcd(n, d): n1 = abs(n); n2 = abs(d) gcd = 1 k = 1 while k <= n1 and k <= n2: if n1 % k == 0 and n2 % k == 0: gcd = k k += 1 return gcd sum = Rational(0, 1) for i in range(1, 10): sum = sum + Rational(i, i + 1) # Display results print("Sum is " + str(sum))
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.