Week 2 - Exercises

Exercise 1 - Implementing Euclid’s algorithm

In Week 0 we talked about Euclid’s ancient algorithm for finding the greatest common divisor of two numbers. By now we have covered everything you need to be able to write it in Python! Here it is again in visual format (refer to the presentation slides if the image is not visible).

Euclid’s algorithm

Implement the algorithm in the cell below. The final line should be a print statement that prints the highest common divisor of the numbers A and B.

Hint 1 - You can use the input statement to assign initial values to A and B

Hint 2 - while

[9]:
# Implement Euclid's algorithm here

A = int(input('Enter the value of A: '))
B = int(input('Enter the value of B: '))

while B != 0:
    if A > B:
        A -= B
    else:
        B -= A

print('The highest common divisor is ', A)
Enter the value of A:  420
Enter the value of B:  42
The highest common divisor is  42

Exercise 2 - Implement Euclid’s algorithm as a function

Now you have managed to implement Euclid’s algorithm, try doing it as a function. Instead of using the input statement to take input from the user, the numbers A and B should be passed as arguments to a function. You could call the function something like get_highest_common_divisor(A, B).

Don’t forget to include a docstring that provides a brief explanation of the function!

Hint - def

[26]:
# Define function here

def get_highest_common_divisor(A, B):
    """Calculate the highest common divisor of two integers, A and B.

    Args:
        A (int): The first integer
        B (int): The second integer

    Returns:
        int: Highest common divisor of A and B

    """

    while B != 0:
        if A > B:
            A -= B
        else:
            B -= A

    return A
[29]:
# Call function here

get_highest_common_divisor(420, 42)
[29]:
42

Exercise 3 - Fahrenheit to Celsius

Previously we used a script to convert Fahrenheit to Celsius. Here it is again.

"""Convert Fahrenheit to Celsius."""

# Ask user to enter a temperature in Fahrenheit
temp = float(input("Enter temperature in Fahrenheit: "))

# Convert to Celsius
celsius = (temp - 32) * (5 / 9)
celsius = round(celsius, 2)

print(f"{temp} degrees Fahrenheit is equal to {celsius} degrees Celsius.")

Try and implement this as a function in the cell below. Rather than taking input from the user and printing the result, the function should take a temperature in Fahrenheit as a single argument and return the temperature in Celsius.

[6]:
# Define function here

def fahrenheit_to_celsius(temp):
    """Convert fahrenheit to celsius.

    Args:
        temp (float): temperature in degrees fahrenheit

    Returns:
        celsius (float): temperature converted to degrees celsius

    """
    # Convert to Celsius
    celsius = (temp - 32) * (5 / 9)
    celsius = round(celsius, 2)

    return celsius
[13]:
# Call function here

fahrenheit_to_celsius(108.35)
[13]:
42.42

Exercise 4 - Celsius to Fahrenheit

Write another function that performs the reverse calculation.

\(Fahrenheit = (Celsius * 9/5) + 32\)

[5]:
# Define function here

def celsius_to_fahrenheit(temp):
    """Convert celsius to fahrenheit.

    Args:
        temp (float): temperature in degrees celcius

    Returns:
        celsius (float): temperature converted to degrees fahrenheit

    """
    # Convert to Celsius
    fahrenheit = (temp * (9/5)) + 32
    fahrenheit = round(fahrenheit, 2)

    return fahrenheit

[25]:
# Call function here

celsius_to_fahrenheit(42.42)
[25]:
108.36