Lab – Take the Python Challenge Answers Lab – Take the Python Challenge (Instructor Version)

This is an optional exercise to test your understanding of Python basics. However, we strongly suggest the student to complete these exercises to prepare yourself for the rest of this course. If you don’t know how to solve them, look into the Python Lectures available in the Course Materials/Tutorials and Demos folder.

Answer the questions or complete the tasks outlined below, use the specific method described if applicable.

1. What is 3 to the power of 5?

# Code cell 1

2. Create a variable, s, containing the string “This course is amazing!”. Using the variable, split the string into a list.

# Code cell 2
#s = ?

3) Given the variables height and mountain, use .format() to print the following string:

`The height of Mt. Everest is 8848 meters.`

# Code cell 3
mountain = "Mt. Everest"
height = 8848
#print("...".format( ? , ? ))

4) Given the following nested list, use indexing to grab the word “this”.

# Code cell 4
lst = ['a','b',[4,10,11],['c',[1,66,['this']],2,111],'e',7]

5) Given the following nested dictionary, grab the word “that”. This exercise is a little more difficult.

# Code cell 5
d = {'k1':['val1','val2','val3',{'we':['need','to','go',{'deeper':[1,2,3,'that']}]}]}

6) What is the main difference between a tuple and a list?

Type your answer here

7) Create a function, GetDomain(), that grabs the email website domain from a string in the form: [email protected].

So for example, passing “[email protected]” would return: domain.com

# Code cell 6
#def GetDomain(?):
# return ?.?()[-1]
GetDomain('[email protected]')

8) Create a basic function, findInternet(), that returns True if the word ‘Internet’ is contained in the input string. Don’t worry about edge cases like punctuation being attached to the word, but account for capitalization. (Hint: Please see https://docs.python.org/2/reference/expressions.html#in)

# Code cell 7
#def findInternet(?):
#return 'internet' in ?.lower().split()
findInternet('The Internet Engineering Task Force was created in 1986')

9) Create a function, countIoT(), that counts the number of times the word “IoT” occurs in a string. Ignore edge cases but take into account capitalization.

# Code cell 8
#def countIoT(st):
# return count
countIoT('I don\'t know how to spell IoT ! Is it IoT or iot ? What does iot mean anyway?')

10) Use lambda expressions and the filter() function to filter out words from a list that do not start with the letter ‘d’. For example:

seq = ['data','salt' ,'dairy','cat', 'dog']

should be filtered down to:

['data', 'dairy', 'dog']
# Code cell 9
seq = ['data','salt' ,'dairy','cat', 'dog']

11) Use lambda expressions and the map() function to convert a list of words to upper case. For example:

seq = [‘data’,’salt’ ,’dairy’,’cat’, ‘dog’]

should become:

['DATA', 'SALT', 'DAIRY', 'CAT', 'DOG']
# Code cell 10

12) Imagine a smart thermostat that is connected to the door, so that it can detect, in addition to the temperature, when people enter or leave the house.

Write a function that, if the temperature is lower than 20 degrees Celsius, and there are people in the house (encoded as a boolean value to be passed as a parameter to the function), turns on the heating by returning the string “Heating on”. When the temperature reaches 23 degrees or there are no people in the house, it returns the string “Heating off”. When none of these conditions are met, the function returns “Do nothing”.

# Code cell 11
#def smart_thermostat(temp, people_in):
# ...
# return command
# Code cell 12
# Verify smart_thermostat()
# Code cell 13
# Verify smart_thermostat()

13) The function zip(list1, list2) returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument lists. Use the zip function to create the following list of tuples:

zipped = [('Parking', -1), ('Shops',0), ('Food Court',1), ('Offices',2)]
# Code cell 14
floor_types = ['Parking', 'Shops', 'Food Court', 'Offices']
floor_numbers = range(-1,3)
#zipped = list(...) 

14) Use the zip function and dict() to create a dictionary, elevator_dict, where the keys are the floor types and the values are the corresponding floor number so that:

elevator_dict[-1] = ‘Parking’

# Code cell 15
floor_types = ['Parking', 'Shops', 'Food Court', 'Offices']
floors_numbers = range(-1,3)
#elevator_dict = dict(...)
# Code cell 16
# Verify elevator_dict[-1]

15) Create an Elevator class. The constructor accepts the list of strings floor_types and the list of integers floor_numbers. The class implemets the methods ‘ask_which_floor’ and ‘go_to_floor’. The output of this methods should look as follows:

`floor_types = [‘Parking’, ‘Shops’, ‘Food Court’, ‘Offices’] floors_numbers = range(-1,4)

el = Elevator(floor_numbers, floor_types)


Going to Food Court floor!


There is floor number -2 in this building.


The floor Offices is the number: 2

el.ask_which_floor(‘Swimming Pool’)

There is no Swimming Pool floor in this building.

# Code cell 17
class Elevator:

def __init__(self, floor_numbers, floor_types):
self._floor_numbers = floor_numbers
self._floor_types = floor_types
self._number_to_type_dict = dict(zip(floor_numbers, floor_types)) 
self._type_to_number_dict = dict(zip(floor_types, floor_numbers)) 

def ask_which_floor(self, floor_type): 
if floor_type in self._floor_types:
print('The {} floor is the number: {}.'.format(floor_type, self._type_to_number_dict[floor_type]))
print('There is no {} floor in this building.'.format(floor_type))

#def go_to_floor(self, floor_number):
# Verify code cell 18
el = Elevator(floor_numbers, floor_types)
# Verify code cell 19
# Verify code cell 20
# Verify code cell 21
el.ask_which_floor('Swimming Pool')


Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x