Python Practice Question & Solution

Khemlall Mangal
12 min readMay 9, 2022

Hey Guys, i have taken Coursera Python crash course and have decided to add the solution to some of the practice question and test.

Problem:
The skip_elements function returns a list containing every other element from an input list, starting with the first element. Complete this function to do that, using the for loop to iterate through the input list.

Initial Starting code to complete:

def skip_elements(elements):# Initialize variablesnew_list = []i = 0# Iterate through the listfor ___# Does this element belong in the resulting list?if ___# Add this element to the resulting list___# Increment i___return ___print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']print(skip_elements([])) # Should be []

Solution:

def skip_elements(elements):
# Initialize variables
new_list = []
i = 0
# Iterate through the list
for element in elements:
# Does this element belong in the resulting list?
if i%2 ==0:
# Add this element to the resulting list
new_list.append(element)
# Increment i
i += 1
return new_list
EXPECTATIONprint(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']print(skip_elements([])) # Should be []

Let do this using Enumerable:

def skip_elements(elements):
# code goes here
new_list = []
return [v for i, v in enumerate(elements, start=0) if not i % 2]


print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']

Using the format method, fill in the gaps in the convert_distance function so that it returns the phrase “X miles equals Y km”, with Y having only 1 decimal place. For example, convert_distance(12) should return “12 miles equals 19.2 km”.

Solution

def convert_distance(miles):
km = miles * 1.6
result = "{} miles equals {:.1f} km".format(miles,km)
return result

print(convert_distance(12)) # Should be: 12 miles equals 19.2 km
print(convert_distance(5.5)) # Should be: 5.5 miles equals 8.8 km
print(convert_distance(11)) # Should be: 11 miles equals 17.6 km

problem:
The is_palindrome function checks if a string is a palindrome. A palindrome is a string that can be equally read from left to right or right to left, omitting blank spaces, and ignoring capitalization. Examples of palindromes are words like kayak and radar, and phrases like “Never Odd or Even”. Fill in the blanks in this function to return True if the passed string is a palindrome, False if not.

In [1]:

SOLUTION

def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for letter in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if letter != " ":
new_string += letter.lower()
reverse_string = letter.lower() + reverse_string
# Compare the strings

if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Problem:
Fill in the gaps in the nametag function so that it uses the format method to return first_name and the first initial of last_name followed by a period. For example, nametag(“Jane”, “Smith”) should return “Jane S.”

In [3]:

def nametag(first_name, last_name):
return("{} {}.".format(first_name, last_name[0]))
print(nametag("Jane", "Smith"))
# Should display "Jane S."
print(nametag("Francesco", "Rinaldi"))
# Should display "Francesco R."
print(nametag("Jean-Luc", "Grand-Pierre"))
# Should display "Jean-Luc G."

The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending(“abcabc”, “abc”, “xyz”) should return abcxyz, not xyzxyz or xyzabc. The string comparison is case-sensitive, so replace_ending(“abcabc”, “ABC”, “xyz”) should return abcabc (no changes made).

In [4]:

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
if old[:] == sentence[-len(old):]:
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = len(old)
new_sentence = sentence[:-i] + new
return new_sentence
# Return the original sentence if there is no match
return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"
It's raining cats and dogs
She sells seashells by the seashore
The weather is nice in May
The weather is nice in April

Practice Quiz: Lists

Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.

In [5]:

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames = []
for file in filenames:
if '.hpp' in file:
newfilenames.append((file,file[:-2]))
else:
newfilenames.append((file,file))
print (newfilenames) # Should be [('program.c', 'program.c'), ('stdio.hpp', 'stdio.h'), ('sample.hpp', 'sample.h'), ('a.out', 'a.out'), ('math.hpp', 'math.h'), ('hpp.out', 'hpp.out')][('program.c', 'program.c'), ('stdio.hpp', 'stdio.h'), ('sample.hpp', 'sample.h'), ('a.out', 'a.out'), ('math.hpp', 'math.h'), ('hpp.out', 'hpp.out')]

The permissions of a file in a Linux system are split into three sets of three permissions: read, write, and execute for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission, with 4 corresponding to read, 2 to write, and 1 to execute. Or it can be written with a string using the letters r, w, and x or — when the permission is not granted. For example: 640 is read/write for the owner, read for the group, and no permissions for the others; converted to a string, it would be: “rw-r — — -” 755 is read/write/execute for the owner, and read/execute for group and others; converted to a string, it would be: “rwxr-xr-x” Fill in the blanks to make the code convert a permission in octal format into a string format.

In [6]:

def octal_to_string(octal):
result = ""
value_letters = [(4,"r"),(2,"w"),(1,"x")]
# Iterate over each of the digits in octal
for digit in [int(n) for n in str(octal)]:
# Check for each of the permissions values
for value, letter in value_letters:
if digit >= value:
result += letter
digit -= value
else:
result += '-'
return result

print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------
rwxr-xr-x
rw-r--r--
rwxr-x---
rw-------

Let’s create a function that turns text into pig latin: a simple text transformation that modifies each word moving the first character to the end and appending “ay” to the end. For example, python ends up as ythonpay.

In [7]:

def pig_latin(text):
say = ""
# Separate the text into words
words = text.split()
for word in words:
# Create the pig latin word and add it to the list
say += word[1:]+word[0]+'ay'
if word != words[len(words)-1]:
say +=' '
# Turn the list back into a phrase
return say

print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
ellohay owhay reaay ouyay
rogrammingpay niay ythonpay siay unfay

Practice Quiz: Dictionaries

The email_list function receives a dictionary, which contains domain names as keys, and a list of users as values. Fill in the blanks to generate a list that contains complete email addresses (e.g. diana.prince@gmail.com).

In [8]:

def email_list(domains):
emails = []
for domain,users in domains.items():
for user in users:
emails.append(user+'@'+domain)
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))['clark.kent@gmail.com', 'diana.prince@gmail.com', 'peter.parker@gmail.com', 'barbara.gordon@yahoo.com', 'jean.grey@yahoo.com', 'bruce.wayne@hotmail.com']

The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values.

In [9]:

def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for group in group_dictionary.keys():
# Now go through the users in the group
for users in group_dictionary[group]:
lst = []
for group in group_dictionary.keys():
if users in group_dictionary[group] and users not in lst:
lst.append(group)
user_groups[users] = lst
return user_groups
# Now add the group to the the list of
# groups for this user, creating the entry
# in the dictionary if necessary
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
{'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}

Module 4 Graded Assessment

The format_address function separates out parts of the address string into new strings: house_number and street_name, and returns: “house number X on street named Y”. The format of the input string is: numeric house number, followed by the street name which may contain numbers, but never by themselves, and could be several words long. For example, “123 Main Street”, “1001 1st Ave”, or “55 North Center Drive”. Fill in the gaps to complete this function.

In [10]:

def format_address(address_string):
# Declare variables
words = address_string.split()
num = ""
nam = ""
# Separate the address string into parts
# Traverse through the address parts
for i,word in enumerate(words):
if i == 0:
num += word
elif i != len(words)-1:
nam += word +" "
else:
nam += word
# Determine if the address part is the
# house number or part of the street name
# Does anything else need to be done
# before returning the result?

# Return the formatted string
return "house number {} on street named {}".format(num,nam)
print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"
print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"
print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"
house number 123 on street named Main Street
house number 1001 on street named 1st Ave
house number 55 on street named North Center Drive

The highlight_word function changes the given word in a sentence to its upper-case version. For example, highlight_word(“Have a nice day”, “nice”) returns “Have a NICE day”. Can you write this function in just one line?

In [11]:

def highlight_word(sentence, word):
ret = ""
l = len(word)
words = sentence.split()
for i,w in enumerate(words):
if w[:l] == word:
ret += w[:l].upper()+w[l:]
else:
ret += w
if i != len(words)-1:
ret += " "
return ret
print(highlight_word("Have a nice day", "nice"))
print(highlight_word("Shhh, don't be so loud!", "loud"))
print(highlight_word("Automating with Python is fun", "fun"))
Have a NICE day
Shhh, don't be so LOUD!
Automating with Python is FUN

A professor with two assistants, Jamie and Drew, wants an attendance list of the students, in the order that they arrived in the classroom. Drew was the first one to note which students arrived, and then Jamie took over. After the class, they each entered their lists into the computer and emailed them to the professor, who needs to combine them into one, in the order of each student’s arrival. Jamie emailed a follow-up, saying that her list is in reverse order. Complete the steps to combine them into one list as follows: the contents of Drew’s list, followed by Jamie’s list in reverse order, to get an accurate list of the students as they arrived.

In [12]:

def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
for i in range(len(list1)-1,-1,-1):
list2.append(list1[i])
return list2
# Followed by the elements of list1 in reverse order


Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']

Use a list comprehension to create a list of squared numbers (n*n). The function receives the variables start and end, and returns a list of squares of consecutive numbers between start and end inclusively. For example, squares(2, 3) should return [4, 9].

In [13]:

def squares(start, end):
return [x*x for x in range(start,end+1)]
print(squares(2, 3)) # Should be [4, 9]
print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[4, 9]
[1, 4, 9, 16, 25]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Complete the code to iterate through the keys and values of the car_prices dictionary, printing out some information about each one.

In [14]:

def car_listing(car_prices):
result = ""
for car,price in car_prices.items():
result += "{} costs {} dollars".format(car,price) + "\n"
return result
print(car_listing({"Kia Soul":19000, "Lamborghini Diablo":55000, "Ford Fiesta":13000, "Toyota Prius":24000}))Kia Soul costs 19000 dollars
Lamborghini Diablo costs 55000 dollars
Ford Fiesta costs 13000 dollars
Toyota Prius costs 24000 dollars

Taylor and Rory are hosting a party. They sent out invitations, and each one collected responses into dictionaries, with names of their friends and how many guests each friend is bringing. Each dictionary is a partial list, but Rory’s list has more current information about the number of guests. Fill in the blanks to combine both dictionaries into one, with each friend listed only once, and the number of guests from Rory’s dictionary taking precedence, if a name is included in both dictionaries. Then print the resulting dictionary.

In [15]:

def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
for key,val in guests1.items():
guests2[key] = val
return guests2
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests)){'David': 1, 'Nancy': 1, 'Robert': 4, 'Adam': 2, 'Samantha': 3, 'Chris': 5, 'Brenda': 3, 'Jose': 3, 'Charlotte': 2, 'Terry': 1}

Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters(“This is a sentence.”) should return {‘t’: 2, ‘h’: 1, ‘i’: 2, ‘s’: 3, ‘a’: 1, ‘e’: 3, ’n’: 2, ‘c’: 1}.

In [16]:

def count_letters(text):
result = {}
# Go through each letter in the text
for letter in text:
# Check if the letter needs to be counted or not
letter = letter.lower()
if letter.isalpha():
if letter in result:
result[letter] += 1
else:
result[letter] = 1
# Add or increment the value in the dictionary
return result
print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}
print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
{'a': 2, 'b': 2, 'c': 2}
{'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
{'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

The odd_numbers function returns a list of odd numbers between 1 and n, inclusively. Fill in the blanks in the function, using list comprehension. Hint: remember that list and range counters start at 0 and end at the limit minus 1.

def odd_numbers(n):
return [x for x in range(0, n+1) if x%2 != 0]
print(odd_numbers(5)) # Should print [1, 3, 5]
print(odd_numbers(10)) # Should print [1, 3, 5, 7, 9]
print(odd_numbers(11)) # Should print [1, 3, 5, 7, 9, 11]
print(odd_numbers(1)) # Should print [1]
print(odd_numbers(-1)) # Should print []

You can create lists from sequences using a for loop, but there’s a more streamlined way to do this: list comprehension. List comprehensions allow you to create a new list from a sequence or a range in a single line.

For example, [ x*2 for x in range(1,11) ] is a simple list comprehension. This would iterate over the range 1 to 10, and multiply each element in the range by 2. This would result in a list of the multiples of 2, from 2 to 20.

You can also use conditionals with list comprehensions to build even more complex and powerful statements. You can do this by appending an if statement to the end of the comprehension. For example, [ x for x in range(1,101) if x % 10 == 0 ] would generate a list containing all the integers divisible by 10 from 1 to 100. The if statement we added here evaluates each value in the range from 1 to 100 to check if it’s evenly divisible by 10. If it is, it gets added to the list.

List comprehensions can be really powerful, but they can also be super complex, resulting in code that’s hard to read. Be careful when using them, since it might make it more difficult for someone else looking at your code to easily understand what the code is doing.

Question 6

The guest_list function reads in a list of tuples with the name, age, and profession of each party guest, and prints the sentence “Guest is X years old and works as __.” for each one. For example, guest_list((‘Ken’, 30, “Chef”), (“Pat”, 35, ‘Lawyer’), (‘Amanda’, 25, “Engineer”)) should print out: Ken is 30 years old and works as Chef. Pat is 35 years old and works as Lawyer. Amanda is 25 years old and works as Engineer. Fill in the gaps in this function to do that.

def guest_list(guests):for name,age,profession in guests:print("{} is {} years old and works as {}".format(name, age, profession))guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])#Click Run to submit code"""Output should match:Ken is 30 years old and works as ChefPat is 35 years old and works as LawyerAmanda is 25 years old and works as Engineer"""

AnisahTiaraPratiwi’s gists (github.com)

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.