Python Exercises to Review and Enhance Computer Coding Skills (Part 1 of 2)

Jmstipanowich
7 min readNov 16, 2021

Python Practice Makes Python Perfect

Whether a person is learning Python code for the first time, preparing to execute Python code in job interviews, or working a job where code has not been utilized for a while and a clearing of the coding cobwebs in one’s mind is needed, performing basic Python code exercises works as a quick and effective way to review and improve computer coding skills. In this blog, I will display python coding exercises with both problems and solutions in order to help people enhance their Python coding skills for any situation they encounter. I will provide explanations of the solutions for the problems to further enrich the learning process. All of the Python exercises in this blog were obtained from two websites: https://www.practicepython.org/ and https://www.hackerrank.com/. Some problems are easier than others, but all of the problems reveal basic Python principles to support effective learning of Python code. Because of the large magnitude of problems I worked on and a desire for more thorough explanations, I am dividing the problems I offer for use into two blogs as the most effective way to manage them.

The Python Exercises to Review and Enhance Computer Coding Skills (Part 1 of 2) problems are written below:

1. List Less Than Ten Solutions Problem:

Take a list, say for example this one:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5. Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list and print out this new list.

1. List Less Than Ten Solutions Solution:

To create a new list for the numbers less than 5, a list titled: “a_new= []” is fashioned after the first line of code. The next line of code: “for i in a:” sets up a function to apply to all items in the a list. The “if i < 5:” statement applies to all items in the a list that are less than 5. The “a.new.append(i)” line of code is written so that all items in the a list that are less than 5 will be added to the new a_new list of items. The “print(a_new)” statement prints the solution to the problem with a new list of numbers less than 5 from the a list ([1, 1, 2, 3]).

2. Max of Three Solutions Problem:

Implement a function that takes as input three variables and returns the largest of the three. Do this without using the Python max() function!

2. Max of Three Solutions Solution:

The “def max_of_three(a , b, c):” line of code sets up the function to find the maximum of three variable values with the three variable values being represented as “a”, “b”, and “c”. The “nums = [a, b, c]” line of code puts the three values used in the function into a list format named “nums”. Next, the “x= sorted(nums)” code sorts the values in the nums list from lowest to highest and creates a new sorted list with the values titled “x”. The “z=len(x)” determines the number of values that were in the x list. The “print(x[z-1])” line of code takes the sorted x list and using the z value of the number of items in the x list, procures the final item ([z-1]) in the x list, which will be the highest value in the x list and the highest value of a, b, and c in the function. The function was tried using 72, 97, and 313 as the list of number values that replace a, b, and c in the function (max_of_three(72, 97, 313)) and 313 was discovered as the highest number in the list of numbers. The function worked correctly without using max(). Problem solved!

3. Leap Year Problem:

An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.

In the Gregorian calendar, three conditions are used to identify leap years:

The year can be evenly divided by 4 is a leap year, unless: The year can be evenly divided by 100, it is NOT a leap year, unless: The year is also evenly divisible by 400. Then it is a leap year. This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

Task

Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.

Input Format

Read the year to test.

Output Format

The function must return a Boolean value (True/False).

3. Leap Year Solution:

The “def is_leap(year):” code sets up a function to determine if a chosen year is a leap year. The “leap = False” line of code provides for the possible years where a year is not a leap year. The

“if (year % 4 == 0): leap = True if (year % 100 == 0): leap = False
if (year % 400 == 0): leap = True” delivers code to represent the conditions where a leap year may be truly a leap year or not (the year can be evenly divided by 4 is a leap year, unless: the year can be evenly divided by 100, it is NOT a leap year, unless: the year is also evenly divisible by 400). The “return leap” line of code produces the conclusion to if a year is a leap year or not. I tried the is_leap function on the year 2100 and the function correctly concluded 2100 will not be a leap year.

4. Reduce Function Problem:

Given a list of rational numbers, find their product.

Input Format

First line contains the number of rational numbers with numerators and denominators.

Output Format

Print only one line containing the numerator and denominator of the product of the numbers in the list in its simplest form, i.e. numerator and denominator have no common divisor other than 1.

4. Reduce Function Solution:

For this solution, fractions had to be imported under “Fraction” to create a fraction value. The reduce function had to be imported from “functools” so the reduce function could be used as part of a Python solution. The “def product(fracs):” line of code in the solution sets up the function that will take the product of a list of fractions. Fractions can be rational numbers because they are formed by dividing an integer by an integer and are used to solve this problem. The “t=reduce(lambda x, y : x * y, fracs)” code finds the product of a list of fractions and reduces the fraction product to its simplest form ( i.e., numerator and denominator have no common divisor other than 1). Then, the produced value is set equal to t. The “return t.numerator, t. denominator” delivers the numerator value and denominator value of t, the solution values. For my example use of the function I found the product of (1/2) * (3/7) written as “product([Fraction(1,2), Fraction(3,7)])” using the function. The result was the correct product value of (3, 14), or 3/14 in fraction form.

Python Problems Answered

The above list of problems was created to allow people to review and enhance their Python coding skills. I hope the problems and solutions were interesting, applicable, and helpful. Look for more exercises when I write Python Exercises to Review and Enhance Computer Coding Skills (Part 2 of 2) next week!

Resources:

--

--