A201 Introduction to Programming 1

A201 Lab 7

Date: Tuesday, October 3, 2023.
Due Date: Tuesday, October 10, 2023.

Goal: to use functions in Python.

Create a Python file called lab7.py and add your name and the lab number as a comment at the top.

Ex. 1. Greatest Common Divisor

Write a Python function that computes the greatest common divisor of two numbers. The header of the this function could be:

def GCD(n, m):

In this function, first test if either of the numbers is 0, and if it is, return the other one. Otherwise, write a for loop that goes backwards from the minimum of n and m down to 1, including 1. If any of these numbers is a factor of both n and m, then return it. You can use the built-in function min.

Execute the program, then call the function GCD from the console with a few examples to see if it works fine.

Ex. 2. Fraction Simplification

Write a function that receives two parameters representing a fraction and simplifies it. The header of the function could be:

def simplify(n, m):

In this function, first check if either n or m is 0, and return 0 if that's the case. We need this test first to avoid a division by 0.

Otherwise, call the function GCD(n, m) and store the result in another variable. Then return n divided by the value of the GCD and m divided by the same, separated by a comma. For example, simply(12, 18) should return 2, 3.

Test this second function a few times the same way as the first one.

Lab Submission

Upload the file lab7.py in Canvas, Assignments, Homework 7. You can wait until you finish the homework to upload both files at the same time.