Skip to content

Commit 5fffbb8

Browse files
authored
Merge branch 'master' into neha-add-exercises
2 parents 39bd2f9 + 9df4757 commit 5fffbb8

37 files changed

+545
-0
lines changed

3_advanced/chapter13/examples/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Vector:
2+
"""
3+
Constructor
4+
5+
self: a reference to the object we are creating
6+
vals: a list of integers which are the contents of our vector
7+
"""
8+
9+
def __init__(self, vals):
10+
self.vals = (
11+
vals # We're using the keyword self to create a field/property
12+
)
13+
print("Assigned values ", vals, " to vector.")
14+
15+
"""
16+
String Function
17+
18+
Converts the object to a string in readable format for programmers
19+
"""
20+
21+
def __str__(self):
22+
return str(self.vals) # Returns the contents of the vector
23+
24+
25+
vec = Vector([2, 3, 2])
26+
print(str(vec)) # [2, 3, 2]
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Vector:
2+
"""
3+
Constructor
4+
5+
self: a reference to the object we are creating
6+
vals: a list of integers which are the contents of our vector
7+
"""
8+
9+
def __init__(self, vals):
10+
self.vals = vals
11+
# print("Assigned values ", vals, " to vector.")
12+
13+
"""
14+
String Function
15+
16+
Converts the object to a string in readable format for programmers
17+
"""
18+
19+
def __str__(self):
20+
return str(self.vals)
21+
22+
"""
23+
Elementwise power: raises each element in our vector to the given power
24+
"""
25+
26+
def __pow__(self, power):
27+
return Vector([i ** power for i in self.vals])
28+
29+
"""
30+
Addition: adds each element to corresponding element in other vector
31+
"""
32+
33+
def __add__(self, vec):
34+
return Vector(
35+
[self.vals[i] + vec.vals[i] for i in range(len(self.vals))]
36+
)
37+
38+
"""
39+
Multiplies each element in the vector by a specified constant
40+
"""
41+
42+
def __mul__(self, constant):
43+
return Vector([self.vals[i] * constant for i in range(len(self.vals))])
44+
45+
"""
46+
Elementwise subtraction: does same as addition, just subtraction instead
47+
"""
48+
49+
def __sub__(self, vec):
50+
return self + (vec * (-1))
51+
52+
53+
vec = Vector([2, 3, 2])
54+
otherVec = Vector([3, 4, 5])
55+
print(str(vec)) # [2, 3, 2]
56+
print(vec ** 2) # [4, 9, 4]
57+
print(vec - otherVec) # [-1, -1, -3]
58+
print(vec + otherVec) # [5, 7, 7]
59+
print(vec * 5) # [10, 15, 10]
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Vector:
2+
"""
3+
Constructor
4+
5+
self: a reference to the object we are creating
6+
vals: a list of integers which are the contents of our vector
7+
"""
8+
9+
def __init__(self, vals):
10+
self.vals = vals
11+
# print("Assigned values ", vals, " to vector.")
12+
13+
"""
14+
String Function
15+
16+
Converts the object to a string in readable format for programmers
17+
"""
18+
19+
def __str__(self):
20+
return str(self.vals)
21+
22+
def __pow__(self, power):
23+
return Vector([i ** power for i in self.vals])
24+
25+
# Calculates Euclidean norm
26+
def norm(self):
27+
return sum((self ** 2).vals) ** 0.5
28+
29+
# __lt__: implements the less than operator (<)
30+
def __lt__(self, other):
31+
return self.norm() < other.norm()
32+
33+
# __gt__: implements the greater than operator (>)
34+
def __gt__(self, other):
35+
return self.norm() > other.norm()
36+
37+
# __le__: implements the less than equal to operator (<=)
38+
def __le__(self, other):
39+
return self.norm() <= other.norm()
40+
41+
# __ge__: implements the greater than equal to operator (>=)
42+
def __ge__(self, other):
43+
return self.norm() >= other.norm()
44+
45+
# __eq__: implements the equals operator (==)
46+
def __eq__(self, other):
47+
return self.norm() == other.norm()
48+
49+
# __ne__:implements the not equals operator (!=)
50+
def __ne__(self, other):
51+
return self.norm() != other.norm()
52+
53+
54+
vec = Vector([2, 3, 2])
55+
vec2 = Vector([3, 4, 5])
56+
print(vec < vec2) # True
57+
print(vec > vec2) # False
58+
59+
print(vec <= vec2) # True
60+
print(vec >= vec2) # False
61+
print(vec <= vec) # True
62+
print(vec >= vec) # True
63+
64+
print(vec == vec2) # False
65+
print(vec == vec) # True
66+
67+
print(vec != vec2) # True
68+
print(vec != vec) # False

3_advanced/chapter13/practice/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later

3_advanced/chapter13/solutions/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later

3_advanced/chapter14/examples/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler content. You can only add 1 folder at a time?
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Problem name: add_10
2+
# A messy teacher named Bob would like to add 10 points to each student’s recent test score.
3+
# There are four students, and going from highest score to lowest score, it is Mike, Dan, Stan, and Ban.
4+
# Add 10 to each score and assign those values to the correct student.
5+
# Solve this problem by adding no more than 2 lines of code.
6+
# Hint: Use tuple unpacking and list comprehension.
7+
8+
# the scores are given
9+
scores = (100, 90, 80, 70)
10+
11+
# write your code below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Problem Name: bob_selection
2+
3+
#Bob is choosing a person to go to the moon with him. The way he chooses is quite strange. He will choose the first person from a list given to him whose age is divisible by 5 and whose index within the list is divisible by 5. If he does find such a person, print the person’s name. If he doesn’t, don’t print anything. The list given to him contains lists which contain the person’s name and age. Use enumerate to solve this problem.
4+
5+
#the list is given to him
6+
people_list = [(“Ana”, 22), (“Mark”, 41), (“Dan”, 10), (“Jack”, 14), (“Ben”, 51), (“Jorge”, 65)]
7+
8+
#write your code below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Problem Name: darwin_raccoon
2+
3+
#Darwin is observing raccoons’ growths on an unnamed island. He spends 7 days in total on this island, and on every day, he would record the average growth of raccoons in inches. He loses data on day 7, so he decides to make the data on that day to be the maximum of the previous 6 days. He needs to make a dictionary for use later where the key is the day number and the value is the average growth of raccoons on that day. You help him make the dictionary. Use zip to solve this problem.
4+
5+
#the lists are already given to you
6+
days_list = [“Day 1”, “Day 2”, “Day 3”, “Day 4”, “Day 5”, “Day 6”, “Day 7”]
7+
growths_list = [1.4, 2.1, 1.3, 0.1, 0.4, 1.9]
8+
9+
#write your code below
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Problem name: even_words
2+
# Given a string that the user inputs create a list that contains the square of the lengths of words with an even amount of characters.
3+
# For example, if the string is “I am cool”, the list would be [4, 16].
4+
# Use a list comprehension to solve it.
5+
6+
# the user inputs the string
7+
string = input()
8+
9+
# write your code below

3_advanced/chapter14/practice/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is filler content. You can only add 1 folder at a time?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Problem name: odd_squares
2+
# Given a list of integers, create a list of all the squares of the odd integers within the list.
3+
# Use a list comprehension to solve it.
4+
5+
# the given code takes an input and makes it a list of numbers
6+
# for example, entering “1 23 4” as the input will result in the list [1,23,4]
7+
ex_list = input().split()
8+
for idx in range(len(ex_list)):
9+
ex_list[idx] = int(ex_list[idx])
10+
11+
# write your code below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Problem Name: pokemon_presentation
2+
3+
Youre a teacher giving a presentation on the types(https://pokemon.fandom.com/wiki/Types) of pokemons(https://www.pokemon.com/us/pokedex/). You have a list pokemons_list, and you have another list types_list(this has the types of the pokemons in pokemons_lists in the same order). Since you are presenting, print “[insert pokemon] is a [insert type] typefor all the pokemons in pokemons_list with their corresponding types in types_list. Use zip to solve this problem.
4+
5+
#the lists are already given to you
6+
pokemons_list = [“Charmander”, “Squirtle”, “Bulbasaur”, “Pikachu”]
7+
types_list = [“Fire”, “Water”, “Grass and Poison”, “Electric” ]
8+
9+
#write your code below
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem Name: square_root_list
2+
# Take a user given list of numbers and make a list of all the square roots of the numbers using list comprehension.
3+
# Use a list comprehension to solve it.
4+
# Hint: The square root of a number is the same as taking the ½ power of a number.
5+
6+
# the given code takes an input and makes it a list of numbers
7+
# for example, entering “1 23 4” as the input will result in the list [1,23,4]
8+
ex_list = input().split()
9+
for idx in range(len(ex_list)):
10+
ex_list[idx] = int(ex_list[idx])
11+
12+
# write your code below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Problem name: update_score
2+
# A hacker named Dan wishes to hack into a competition where they judge participants in three categories on a scale of 10.
3+
# Dan wants his friend Bob to win.
4+
# Bob will only win if he has all 10s while the other competitors, Jo and Stan, don’t.
5+
6+
# Judges will store the scoring within a tuple ([...], [...], [...]).
7+
# The scores before Dan hacked are given.
8+
# Bob will be located as the first person the judges scored and will have the lowest points out of any participant.
9+
# Create a program to help Dan help Bob win.
10+
# Also, print Bob’s score at the end.
11+
# Use tuple unpacking to solve this problem.
12+
13+
# the scores are given
14+
scores = ([1, 1, 1], [2, 2, 2], [3, 3, 3])
15+
16+
# write your code below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem Name: worried_josh
2+
3+
"""
4+
5+
Josh is worried about his test score. He wants to score in the top n,
6+
where n is a positive integer that the user inputs. Given a list of
7+
student names where the student with the highest score is the 0th index
8+
and the score goes down from there, print “YES!” if Josh scores in the top n,
9+
and “NO :(“ if he doesn’t. Assume n will not be greater than the number of students.
10+
Use enumerate to solve this problem.
11+
12+
"""
13+
14+
# the list of student names is given and the n is a user input
15+
# remember the leftmost student = highest score, rightmost student = lowest score
16+
students = [“Dan”, “Sherlocks”, “Jo”, “Josh”, “Dennis”, “Erwin”, “Ivan”, “Penny”]
17+
n = int(input())
18+
19+
# write your code below
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Problem name: add_10
2+
# A messy teacher named Bob would like to add 10 points to each student’s recent test score.
3+
# There are four students, and going from highest score to lowest score, it is Mike, Dan, Stan, and Ban.
4+
# Add 10 to each score and assign those values to the correct student.
5+
# Solve this problem by adding no more than 2 lines of code.
6+
# Hint: Use tuple unpacking and list comprehension.
7+
8+
# the scores are given
9+
scores = (100, 90, 80, 70)
10+
11+
# write your code below
12+
scores_added = [n + 10 for n in scores]
13+
Mike, Dan, Stan, Ban = scores_added
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Problem Name: bob_selection
2+
3+
#Bob is choosing a person to go to the moon with him. The way he chooses is quite strange. He will choose the first person from a list given to him whose age is divisible by 5 and whose index within the list is divisible by 5. If he does find such a person, print the person’s name. If he doesn’t, don’t print anything. The list given to him contains lists which contain the person’s name and age. Use enumerate to solve this problem.
4+
5+
#the list is given to him
6+
people_list = [(“Ana”, 22), (“Mark”, 41), (“Dan”, 10), (“Jack”, 14), (“Ben”, 51), (“Jorge”, 65)]
7+
8+
#write your code below
9+
for index, person_data in enumerate(people_list):
10+
if person_data[1]%5 == 0 and index %5 == 0:
11+
print(person_data[0])
12+
break
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Problem Name: darwin_raccoon
2+
3+
#Darwin is observing raccoons’ growths on an unnamed island. He spends 7 days in total on this island, and on every day, he would record the average growth of raccoons in inches. He loses data on day 7, so he decides to make the data on that day to be the maximum of the previous 6 days. He needs to make a dictionary for use later where the key is the day number and the value is the average growth of raccoons on that day. You help him make the dictionary. Use zip to solve this problem.
4+
5+
#the lists are already given to you
6+
days_list = [“Day 1”, “Day 2”, “Day 3”, “Day 4”, “Day 5”, “Day 6”, “Day 7”]
7+
growths_list = [1.4, 2.1, 1.3, 0.1, 0.4, 1.9]
8+
9+
#write your code below
10+
data_dictionary = dict(zip(days_list, growths_list + [max(growths_list)]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#Problem name: even_words
3+
Given a string that the user inputs create a list that contains the square of the lengths of words with an even amount of characters.
4+
#For example, if the string is “I am cool”, the list would be [4, 16].
5+
#Use a list comprehension to solve it.
6+
7+
#the user inputs the string
8+
string = input()
9+
10+
#write your code below
11+
evenwords = [len(word) ** 2 for word in string.split() if len(word) % 2 == 0]
12+

3_advanced/chapter14/solutions/filler

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is filler content. You can only add 1 folder at a time?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem name: odd_squares
2+
# Given a list of integers, create a list of all the squares of the odd integers within the list.
3+
# Use a list comprehension to solve it.
4+
5+
# the given code takes an input and makes it a list of numbers
6+
# for example, entering “1 23 4” as the input will result in the list [1,23,4]
7+
ex_list = input().split()
8+
for idx in range(len(ex_list)):
9+
ex_list[idx] = int(ex_list[idx])
10+
11+
# write your code below
12+
odds_quares = [n ** 2 for n in list if n % 2 == 1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Problem Name: pokemon_presentation
2+
3+
#You’re a teacher giving a presentation on the types(https://pokemon.fandom.com/wiki/Types) of pokemons(https://www.pokemon.com/us/pokedex/). You have a list pokemons_list, and you have another list types_list(this has the types of the pokemons in pokemons_lists in the same order). Since you are presenting, print “[insert pokemon] is a [insert type] type” for all the pokemons in pokemons_list with their corresponding types in types_list. Use zip to solve this problem.
4+
5+
#the lists are already given to you
6+
pokemons_list = [“Charmander”, “Squirtle”, “Bulbasaur”, “Pikachu”]
7+
types_list = [“Fire”, “Water”, “Grass and Poison”, “Electric” ]
8+
9+
#write your code below
10+
for pokemon, tp in zip(pokemons_list, types_list):
11+
print(pokemon +is a+ tp +type”)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#Problem Name: square_root_list
3+
#Take a user given list of numbers and make a list of all the square roots of the numbers using list comprehension.
4+
#Use a list comprehension to solve it.
5+
#Hint: The square root of a number is the same as taking the ½ power of a number.
6+
#the given code takes an input and makes it a list of numbers
7+
#for example, entering “1 23 4” as the input will result in the list [1,23,4]
8+
ex_list = input().split()
9+
for idx in range(len(ex_list)):
10+
ex_list[idx] = int(ex_list[idx])
11+
12+
#write your code below
13+
new_list = [number**(½) for number in ex_list]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Problem name: update_score
2+
# A hacker named Dan wishes to hack into a competition where they judge participants in three categories on a scale of 10.
3+
# Dan wants his friend Bob to win.
4+
# Bob will only win if he has all 10s while the other competitors, Jo and Stan, don’t.
5+
6+
# Judges will store the scoring within a tuple ([...], [...], [...]).
7+
# The scores before Dan hacked are given.
8+
# Bob will be located as the first person the judges scored and will have the lowest points out of any participant.
9+
# Create a program to help Dan help Bob win.
10+
# Also, print Bob’s score at the end.
11+
# Use tuple unpacking to solve this problem.
12+
13+
# the scores are given
14+
scores = ([1, 1, 1], [2, 2, 2], [3, 3, 3])
15+
16+
# write your code below
17+
scores[0][0] = 10
18+
scores[0][1] = 10
19+
scores[0][2] = 10
20+
(Bob, Jo, Stan) = scores
21+
print(Bob)

0 commit comments

Comments
 (0)