Skip to content

Commit 3eb8c92

Browse files
Exercises 5.3 finished
1 parent 224b58f commit 3eb8c92

9 files changed

+290
-0
lines changed

part5/3. Dictionary/factorials.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Please write a function named factorials(n: int), which returns the factorials of the numbers 1 to n in a dictionary.
2+
# The number is the key, and the factorial of that number is the value mapped to it.
3+
4+
# A reminder: the factorial of the number n is written n! and is calculated by multiplying the number by each integer smaller than itself.
5+
# For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24.
6+
7+
# An example of the function in action:
8+
9+
# k = factorials(5)
10+
# print(k[1])
11+
# print(k[3])
12+
# print(k[5])
13+
14+
# 1
15+
# 6
16+
# 120
17+
18+
def factorials(n: int):
19+
dictionary = {}
20+
for i in range(1, n + 1):
21+
fact = 1
22+
for x in range(1, i + 1):
23+
fact *= x
24+
dictionary[i] = fact
25+
return dictionary
26+

part5/3. Dictionary/find_movies.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Please write a function named find_movies(database: list, search_term: str), which processes the movie database created in the previous exercise.
2+
# The function should formulate a new list, which contains only the movies whose title includes the word searched for.
3+
# Capitalisation is irrelevant here. A search for ana should return a list containing both Anaconda and Management.
4+
5+
# An example of its use:
6+
7+
# database = [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116},
8+
# {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94},
9+
# {"name": "Dawn of the Dead Programmers", "director": "M. Night Python", "year": 2011, "runtime": 101}]
10+
11+
# my_movies = find_movies(database, "python")
12+
# print(my_movies)
13+
14+
# [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116}, {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94}]
15+
16+
def find_movies(database: list, search_term: str):
17+
movies_searched = []
18+
for movie in database:
19+
if search_term.lower() in movie["name"].lower():
20+
movies_searched.append(movie)
21+
return movies_searched

part5/3. Dictionary/histogram.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Please write a function named histogram, which takes a string as its argument.
2+
# The function should print out a histogram representing the number of times each letter occurs in the string.
3+
# Each occurrence of a letter should be represented by a star on the specific line for that letter.
4+
5+
# For example, the function call histogram("abba") should print out
6+
7+
8+
# a **
9+
# b **
10+
11+
# while histogram("statistically") should print out
12+
13+
14+
# s **
15+
# t ***
16+
# a **
17+
# i **
18+
# c *
19+
# l **
20+
# y *
21+
22+
def histogram(my_string):
23+
dictionary = {}
24+
for word in my_string:
25+
if word not in dictionary:
26+
dictionary[word] = 0
27+
dictionary[word] += 1
28+
for key, value in dictionary.items():
29+
print(f"{key} {value * '*'}")
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Please write a function named invert(dictionary: dict), which takes a dictionary as its argument.
2+
# The dictionary should be inverted in place so that values become keys and keys become values.
3+
4+
# An example of its use:
5+
6+
# s = {1: "first", 2: "second", 3: "third", 4: "fourth"}
7+
# invert(s)
8+
# print(s)
9+
10+
# {"first": 1, "second": 2, "third": 3, "fourth": 4}
11+
12+
# NB: the principles regarding lists covered here also hold for dictionaries passed as arguments.
13+
14+
def invert(dictionary: dict):
15+
dictionary_copy = {}
16+
17+
for key, value in dictionary.items():
18+
dictionary_copy[value] = key
19+
dictionary.clear()
20+
for key, value in dictionary_copy.items():
21+
dictionary[key] = value
22+

part5/3. Dictionary/movie_database.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Please write a function named add_movie(database: list, name: str, director: str, year: int, runtime: int), which adds a new movie object into a movie database.
2+
3+
# The database is a list, and each movie object in the list is a dictionary.
4+
# The dictionary should contain the following keys.
5+
6+
# name
7+
# director
8+
# year
9+
# runtime
10+
# The values attached to these keys are given as arguments to the function.
11+
12+
# An example of its use:
13+
14+
# database = []
15+
# add_movie(database, "Gone with the Python", "Victor Pything", 2017, 116)
16+
# add_movie(database, "Pythons on a Plane", "Renny Pytholin", 2001, 94)
17+
# print(database)
18+
19+
# [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116}, {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94}]
20+
21+
def add_movie(database: list, name: str, director: str, year: int, runtime: int):
22+
database.append({"name":name,"director":director,"year":year,"runtime":runtime})
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Please write a function named dict_of_numbers(), which returns a new dictionary.
2+
# The dictionary should have the numbers from 0 to 99 as its keys.
3+
# The value attached to each key should be the number spelled out in words.
4+
# Please have a look at the example below:
5+
6+
# numbers = dict_of_numbers()
7+
# print(numbers[2])
8+
# print(numbers[11])
9+
# print(numbers[45])
10+
# print(numbers[99])
11+
# print(numbers[0])
12+
13+
# two
14+
# eleven
15+
# forty-five
16+
# ninety-nine
17+
# zero
18+
19+
# NB: Please don't formulate each spelled out number by hand.
20+
# Figure out how you can use loops and dictionaries in your solution.
21+
22+
def dict_of_numbers():
23+
numbers = {}
24+
first_19 = ["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
25+
for i in range(20):
26+
numbers[i] = first_19[i]
27+
dec = ["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]
28+
index = -1
29+
dec_number = 10
30+
for i in range(20,100):
31+
if i % 10 == 0:
32+
index += 1
33+
dec_number += 10
34+
numbers[i] = dec[index]
35+
else:
36+
numbers[i] = f"{dec[index]}-{numbers[i % dec_number]}"
37+
38+
return numbers
39+

part5/3. Dictionary/phone_book_v1.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Please write a phone book application. It should work as follows:
2+
3+
4+
# command (1 search, 2 add, 3 quit): 2
5+
# name: peter
6+
# number: 040-5466745
7+
# ok!
8+
# command (1 search, 2 add, 3 quit): 2
9+
# name: emily
10+
# number: 045-1212344
11+
# ok!
12+
# command (1 search, 2 add, 3 quit): 1
13+
# name: peter
14+
# 040-5466745
15+
# command (1 search, 2 add, 3 quit): 1
16+
# name: mary
17+
# no number
18+
# command (1 search, 2 add, 3 quit): 2
19+
# name: peter
20+
# number: 09-22223333
21+
# ok!
22+
# command (1 search, 2 add, 3 quit): 1
23+
# name: peter
24+
# 09-22223333
25+
# command (1 search, 2 add, 3 quit): 3
26+
# quitting...
27+
28+
# As you can see above, each name can be attached to a single number only.
29+
# If a new entry with the same name is added, the number attached to the old entry is replaced with the new number.
30+
31+
# NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == "__main__" block.
32+
33+
contacts = {}
34+
35+
while True:
36+
command = int(input("command (1 search, 2 add, 3 quit): "))
37+
if command == 1:
38+
name_search = input("name: ")
39+
number = ""
40+
for key, value in contacts.items():
41+
if key == name_search:
42+
number += value
43+
print(number)
44+
if not number:
45+
print("no number")
46+
elif command == 2:
47+
name = input("name: ")
48+
number = input("number: ")
49+
contacts[name] = number
50+
print("ok!")
51+
else:
52+
print("quitting...")
53+
break
54+

part5/3. Dictionary/phone_book_v2.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Please write an improved version of the phone book application.
2+
# Each entry should now accommodate multiple phone numbers.
3+
# The application should work otherwise exactly as above, but this time all numbers attached to a name should be printed.
4+
5+
6+
# command (1 search, 2 add, 3 quit): 2
7+
# name: peter
8+
# number: 040-5466745
9+
# ok!
10+
# command (1 search, 2 add, 3 quit): 2
11+
# name: emily
12+
# number: 045-1212344
13+
# ok!
14+
# command (1 search, 2 add, 3 quit): 1
15+
# name: peter
16+
# 040-5466745
17+
# command (1 search, 2 add, 3 quit): 1
18+
# name: mary
19+
# no number
20+
# command (1 search, 2 add, 3 quit): 2
21+
# name: peter
22+
# number: 09-22223333
23+
# ok!
24+
# command (1 search, 2 add, 3 quit): 1
25+
# name: peter
26+
# 040-5466745
27+
# 09-22223333
28+
# command (1 search, 2 add, 3 quit): 3
29+
# quitting...
30+
31+
def add(contacts):
32+
name = input("name: ")
33+
number = input("number: ")
34+
if not name in contacts:
35+
contacts[name] = []
36+
contacts[name].append(number)
37+
print("ok!")
38+
39+
def search(contacts):
40+
name_search = input("name: ")
41+
if name_search in contacts:
42+
for number in contacts[name_search]:
43+
print(number)
44+
else:
45+
print("no number")
46+
47+
def main():
48+
contacts = {}
49+
50+
while True:
51+
command = int(input("command (1 search, 2 add, 3 quit): "))
52+
if command == 1:
53+
search(contacts)
54+
elif command == 2:
55+
add(contacts)
56+
else:
57+
print("quitting...")
58+
break
59+
60+
main()

part5/3. Dictionary/times_ten.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Please write a function named times_ten(start_index: int, end_index: int), which creates and returns a new dictionary.
2+
# The keys of the dictionary should be the numbers between start_index and end_index inclusive
3+
4+
# The value mapped to each key should be the key times ten.
5+
6+
# For example:
7+
8+
# d = times_ten(3, 6)
9+
# print(d)
10+
11+
# {3: 30, 4: 40, 5: 50, 6: 60}
12+
13+
def times_ten(start_index: int, end_index: int):
14+
dictionary = {}
15+
for i in range(start_index, end_index + 1):
16+
dictionary[i] = i * 10
17+
return dictionary

0 commit comments

Comments
 (0)