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

Jmstipanowich
7 min readNov 23, 2021

Python Practice to Improve Python Coding Performance

Is anyone searching for a fun and interactive place where they can better their Python coding skills? Look no further. Right here in this blog I am providing a group of Python computer coding exercises to enjoy and perform in order to improve and enhance a person’s ability to effectively execute the Python computer coding language. This blog is an extension of my Python Exercises to Review and Enhance Computer Coding Skills (Part 1 of 2) blog where I displayed Python coding exercises (with both problems and solutions) in order to help people heighten their Python coding abilities for different circumstances a person might encounter. My first coding exercise blog is available to read at https://jmstipanowich.medium.com/python-exercises-to-review-and-enhance-computer-coding-skills-part-1-of-2-98aab6d44b33. In this blog (Python Exercises to Review and Enhance Computer Coding Skills (Part 2 of 2)), I will do much the same activities as in my other blog with additional Python coding problems and solutions presented for improving coding skills. Similar to my other blog, I will offer explanations of the solutions for each problem to make the learning process more thorough and effective. As with my other blog, some problems provided are easier than others, but all of the problems reveal basic Python principles to support effective learning of Python code.

Note: These problems are not of my own creation, but were obtained from Python coding websites on the internet. All of the Python exercises in this blog were obtained from two websites: https://www.practicepython.org/ and https://www.hackerrank.com/. The solutions to the problems and solution explanations, however, were inspired by me from the obtained internet Python coding exercises.

The Python Exercises to Review and Enhance Computer Coding Skills (Part 2 of 2) exercises are as follows:

1. List Overlap Problem:

Take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

1. List Overlap Solution:

To solve this problem, I started by creating a new list to put all the common elements from lists a and b. The new list is written as “c=[]”. The “for i in b:” line of code creates a function to reference any item that is in list b. The “if i in a and i not in c:” code is indented to refer to only items in list b and signifies including items that are in both the a and b lists, but not already added to the new list c to prevent duplicate entries. The “c.append(i)” code takes all the elements that are common between lists a and b, but not duplicated from those lists, and adds them in a new list c. The “print(c)” prints the new list of common elements from lists a and b without duplication that solves this problem. In this case, the solution is c= [1, 2, 3, 5, 8, 13].

2. List Comprehensions Problem:

Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.

2. List Comprehensions Solution:

For this problem’s solution, the one line of Python code is “b= [element for element in a if element % 2 == 0]”. The new list of even elements is denoted list b and includes any element in the a list that is not an element that has a remainder other than 0 when it is divided by 2, which would be every even number. The line of code “print(b)” reveals the new list b as equal to [4, 16, 36, 64, 100] including all the even numbers from list a. Problem solved!

3. Word Score Problem:

In this challenge, the task is to debug the existing code to successfully execute all provided test files.

Consider the vowels in the alphabet are: a, e, i, o, u and y.

Function score_words takes a list of lowercase words as an argument and returns a score as follows:

The score of a single word is 2 if the word contains an even number of vowels. Otherwise, the score of the word is 1. The score for the whole list of words is the sum of scores of all words in the list.

Debug the given function score_words such that it returns a correct score.

Input Format

The input is there are n space-separated lowercase words.

Output Format

The output calls the function score_words with the list of words read from the input as the argument and prints the returned score to the output.

3. Word Score Solution:

The first two lines of code (“def is_vowel(letter):” and “return letter in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’])” create a function that returns True if the input letter is a vowel (a, e, i , o, u, or y) and False for any other case. The “def score_words(words):” function sets up the function to score words under the allotted conditions below that line of code. The “score =0” code creates a starting value for the score of the list of words. The “for word in words:” initializes conditions for a word in a list of words. The “num_vowels = 0” creates the starting value of 0 for the number of vowels in a word before conditions are included. The “for letter in word: if is_vowel(letter): num_vowels += 1” codes sums the number of vowels in a word with each vowel getting a count of 1. The “if num_vowels % 2 ==0: score += 2 else: score +=1” determines whether the number of vowels in a word is even (no remainder to the sum of the number of vowels when divided by 2) and scores the word 2 points if that is case, and otherwise provides for all other sums of numbers of vowels for words with allocating a word with a scoring of 1 point. The “return score” line returns the grand total score of points from all words in the provided list of lowercase words for the initial score_words(words) function. I tried the score_words(words) function on the list of lowercase words “i”, “perform”, “python”, and “code”, and correctly achieved a score of 7 points from these words with the given conditions for word points (“i” got 1 point, “perform” got 2 points, “python” got 2 points, and “code” got 2 points).

4. Capitalize! Problem:

You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, ‘joe biden’ should be capitalized correctly as ‘Joe Biden’.

Given a full name, your task is to capitalize the name appropriately.

Input Format

A single line of input containing the full name.

Output Format

Print the capitalized string.

4. Capitalize! Solution:

To generate a way to take a string of words and capitalize them, I started by writing a function for capitalizing a string of words s coded as “def solve(s):”. The following “d=s.split(“ ”)” line of code splits the string of words up in the function into individual word strings with a space between them and the new list of individual words becomes known as list d. The “return(“ ”.join(i.capitalize() for i in d))” code rejoins the words in the d list with a space between them and capitalizes each item in the list of words from list d. I tested the solve(s) function out with solve(‘joe biden’) and was correctly given the capitalized version of the ‘joe biden’ string as ‘Joe Biden’.

The End of Python Exercises For Now

These two Python Exercises to Review and Enhance Computer Coding Skills blogs highlight the Python coding exercises I have worked on so far. I hope these exercises were informative and something was learned from their distribution. No matter how good or bad a Python coder someone is, Python practice exercises can always enhance learning and provide creative new ways to solve various issues. There may be more exercises in the future, but for now simply enjoy the ones I have shared. To view the exercises from my blog last week, utilize the link below: https://jmstipanowich.medium.com/python-exercises-to-review-and-enhance-computer-coding-skills-part-1-of-2-98aab6d44b33. Also, the first two resources at the bottom of this current blog are a starting place if anyone is looking for more Python assistance. Best of luck to everyone in their coding!

Resources:

--

--