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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later.
Lines changed: 26 additions & 0 deletions
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]
Lines changed: 59 additions & 0 deletions
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]
Lines changed: 68 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later

3_advanced/chapter13/solutions/filler

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This is filler. Remove later

3_advanced/chapter14/examples/filler

Lines changed: 1 addition & 0 deletions
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?
Lines changed: 11 additions & 0 deletions
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
Lines changed: 8 additions & 0 deletions
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
Lines changed: 10 additions & 0 deletions
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+

0 commit comments

Comments
 (0)