04 Fractions

The goal of this assignment is to practice processing strings using string functions. For a detailed discussion of string processing see Chapter 8 of How to Think Like a Computer Scientist. For a  brief summary of string string processing, see Python Strings Tutorial

You will write a program that adds fractions mathematically as humans do, that is, by finding a common denominator - no converting to and from decimal.  Ask the user to enter the fractions as strings using the following notation:
You will have to check the string 'xxx/yyy' to make sure it is a valid fraction. If the fraction isn't valid, you have to tell the user how the fraction is formatted improperly and give them an opportunity to correct the error. Here is a sample run.

Fraction 1 ? 1/dog
ValueError!: Fractions may contain only digits, a leading negative ('-'), and '/'

Fraction 1 ? 1//2
ValueError!: More than one '/' in fraction.

Fraction 1 ? 1/0
ZeroDivisionError!: Denominator cannot be zero.

Fraction 1 ? 1/2

Fraction 2 ? 2

 1     2     5
--- + --- = ---
 2     1     2

Enter 'Done' to end: done

Specifically your code must
  • Use string functions to parse user-entered fractions such as "xxx/yyy" into the fraction tuple (xxx, yyy)
  • Handle negative fractions and add correctly. For example "-1/2" returns (-1,2).
  • Treat integers as xxx/1. For example, "3" returns (3,1).
  • Use string functions to catch user input errors that result in illegal fractions.
  • Pass and return numerators and denominators as tuples (n, d)
  • Use raise clauses to create your own exceptions to catch user input errors. 
  • Use Euclid's Algorithm return the sum in simplest form.
  • Output the answer as a well-formatted fraction. 
A sample run would be

Fraction 1 ? 6/12

Fraction 2 ? 5/15

 6      5      5  
---- + ---- = --- 
 12     15     6 

Enter 'Done' to end: done
Write a function add_frac((n1,d1), (n2,d2))that adds the fractions and outputs the answer as a fraction in simplest form (see Euclid's Algorithm below). Your function must pass the following doctests.

>>> add_frac((1,2),(1,3))
(5, 6)

>>> add_frac((1,3),(4,5))
(17, 15)

>>> add_frac((13,45),(22,74))
(976, 1665)

>>> add_frac((2,3),(4,11))
(34, 33)

>>> add_frac((-1,2),(2,1))
(3, 2)

>>> add_frac((-2,3),(2,3))
(0, 1)

>>> add_frac((-2,3),(-2,3))
(-4, 3)

 It is permissible to output improper fractions (e.g. 7/5) as long as there is no common factor between the numerator and the denominator.
Look up Euclid's Algorithm for finding the greatest common factor for two integers.  Implement a function gcf(num, den) that returns the greatest common factor between the numerator and denominator and use this function to express the answer in simplest form. Use the following doctests to verify your gcf() function is working.

    >>> gcf(120, 36)

    >>> gcf(182664, 154875)

Study Guide

Sample code for the Fractions project is attached below.

C-Level Questions will require you to understand
  1. Indexing into strings. For example, if L = 'elephant' , what does L[2] return?
  2. Using for-loops and while-loops to access string elements
  3. if s is a string, using s.find(), s.count(), s.split(), s.center()
  4. string slicing, e.g. if s = 'elephant', what does s[2:4] do?
C-level questions only require you to understand simple code fragments and write their output. You do not need to write any code for the C-level questions.

B- and A-Level Questions will require you understand 
  1. C-Level content
  2. Error handling i.e. try-except clauses
  3. len() command used with lists, e.g., if s = 'elephant' you should know what  len(s) returns
B- and A-level questions require you to not only understand code fragments and write their output, but also may ask you to write short code fragments from scratch that meet the question's requirements.

The coding test will require you to understand B and A-level content.

The Python Tutorials page is a great place to start for your studying.  There's also the unofficial textbook, How to Think Like a Computer Scientist.

04 AddFractions.sample.py
Nikhil Joshi,
Jun 7, 2013, 10:59 AM