Skip to content

Homework Exercise (Day 3)

ThePyProgrammer edited this page Jun 24, 2022 · 17 revisions

So you've just completed the calculator exercise from yesterday...

Now let's get more creative.

Give an option for fraction via the symbol "%".

If the user inputs this symbol and puts in any two numbers, the output should ideally be a mixed number. (This means that the inputs should not be typecasted as floats, but ints)

What is your operation? (+,-,*,/,%,q): %
What is the first number? : 5
What is the second number? : 3
The output of this operation is 1 2/3.
Hint for % operator to be implemented

Remember if you have two numbers a and b, and a > b, then a = q * b + r. You can derive q via the // operator and r via the % operator.

Hence, a/b = q r/b!

Example:

integer_component = a // b
numerator = a % b
denominator = b
Hint for output

To put the integer_component, numerator and denominator together, we need to convert each of them to strings via the str typecasting.

Basically:

output = str(integer_component)+" "+str(numerator)+"/"+str(denominator)

In some cases, the fraction may not be simplified. Hence, we need to find the Highest Common Factor (HCF) of the two numbers in the numerator and denominator. The HCF of two or more numbers is the greatest factor that divides the numbers. For example, 2 is the HCF of 4 and 6, because 2 is the largest number such that 4 divided by 2 and 6 divided by 2 both give integers.

This can be achieved via the Euclidean Algorithm. If you don't know what that is, don't worry.

Basically, if you have any two numbers a and b (given a > b), you can represent the two numbers as follows:

a = q * b + r

Basically, the algorithm states that the hcf(a, b) = hcf(b, r).

If r = 0, then hcf(a, b) = b.

We realise this is really, really challenging to implement. Hence, here is our implementation of the hcf function. Basically, r can be computed via the % operator.

def hcf(x, y):
	if x < y:
		# this is to standardize that x must be larger than or equal to y
		x, y = y, x # swaps both numbers. This is known as multiple assignment, where x and y are assigned each other's values at the same time.
		
	while y != 0:
		x, y = y, x % y
	return x

From here, all you need to do is divide the numerator and denominator by this highest common factor and you have the fraction in its simplest form.

Store all of this in a calculator function, which runs this continuously until the user puts q in the operator bracket.

Basically:

===============
# Calculator! #
===============
What is your operation? (+,-,*,/,%,q): /
What is your first number? : 5
What is your second number? : 3
The output of this operation is 1.6666666666666667.
What is your next operation? (+,-,*,/,%,q): +
What is your first number? : 5
What is your second number? : 3
The output of this operation is 8.
What is your next operation? (+,-,*,/,%,q): %
What is your first number? : 5
What is your second number? : 3
The output of this operation is 1 2/3.
What is your next operation? (+,-,*,/,%,q): q
Terminated!

Clone this wiki locally