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.
- 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.
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.
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)` ` 12` ` >>> gcf(182664, 154875)` ` 177` ## Study GuideSample code for the Fractions project is attached below. C-Level Questions will require you to understand - Indexing into strings. For example, if L = 'elephant' , what does L[2] return?
- Using for-loops and while-loops to access string elements
- if s is a string, using s.find(), s.count(), s.split(), s.center()
- 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 - C-Level content
- Error handling i.e. try-except clauses
- 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 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. |

Programming > Mini-Projects >