Skip to content

Commit fbdcd22

Browse files
committed
Uploading whole MIT Course file
1 parent 0d27371 commit fbdcd22

File tree

60 files changed

+3241
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3241
-0
lines changed

Final_Exam/Problem_3.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Problem 3
2+
# 10/10 points (graded)
3+
# Numbers in Mandarin follow 3 simple rules.
4+
5+
# There are words for each of the digits from 0 to 10.
6+
# For numbers 11-19, the number is pronounced as "ten digit", so for example, 16 would be pronounced (using Mandarin) as "ten six".
7+
# For numbers between 20 and 99, the number is pronounced as “digit ten digit”, so for example, 37 would be pronounced (using Mandarin) as
8+
# "three ten seven". If the digit is a zero, it is not included.
9+
# Here is a simple Python dictionary that captures the numbers between 0 and 10.
10+
trans = {'0':'ling', '1':'yi', '2':'er', '3':'san', '4': 'si', '5':'wu', '6':'liu', '7':'qi', '8':'ba', '9':'jiu', '10': 'shi'}
11+
12+
# We want to write a procedure that converts an American number (between 0 and 99), written as a string, into the equivalent Mandarin.
13+
14+
# Example Usage
15+
# convert_to_mandarin('36') will return san shi liu
16+
# convert_to_mandarin('20') will return er shi
17+
# convert_to_mandarin('16') will return shi liu
18+
19+
# Paste your function here
20+
def convert_to_mandarin(us_num):
21+
'''
22+
us_num, a string representing a US number 0 to 99
23+
returns the string mandarin representation of us_num
24+
'''
25+
if int(us_num) <= 10:
26+
return trans[us_num]
27+
elif int(us_num) <= 19:
28+
return 'shi ' + trans[us_num[1]]
29+
elif int(us_num[1]) == 0:
30+
return trans[us_num[0]] + ' shi'
31+
else:
32+
return trans[us_num[0]] + ' shi ' + trans[us_num[1]]
33+
34+
# Correct

Final_Exam/Problem_4.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def primes_list(N):
2+
'''
3+
N: an integer
4+
'''
5+
# Your code here
6+
primes_no = []
7+
for i in range(2, N+1):
8+
for j in range(2, i):
9+
if i % j == 0:
10+
break
11+
else:
12+
primes_no.append(i)
13+
14+
primes_no.sort()
15+
return primes_no
16+
17+
18+
print(primes_list(20))

Final_Exam/Problem_5.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# def uniqueValues(aDict):
2+
# '''
3+
# aDict: a dictionary
4+
# returns: a sorted list of keys that map to unique aDict values, empty list if none
5+
# '''
6+
# # Your code here
7+
# # old_aDict = aDict.values()
8+
# # for keys, values in aDict.items():
9+
# # uniqList.append(values)
10+
# # print (uniqList)
11+
12+
# # # Remove duplicates from uniqList
13+
# # uniqList = list(set(uniqList))
14+
# # print (uniqList)
15+
16+
# from collections import Counter
17+
# def uniqueValues(aDict):
18+
# count = Counter(aDict.values())
19+
# print(count)
20+
# # print(list_count)
21+
# return sorted(k for k, v in count.items() if count[v] == 1)
22+
23+
from collections import Counter
24+
def uniqueValues(aDict):
25+
'''
26+
aDict: a dictionary
27+
returns: a sorted list of keys that map to unique aDict values, empty list if none
28+
'''
29+
unique_values = [k for k,v in aDict.items() if list(aDict.values()).count(v)==1]
30+
return unique_values
31+
32+
aDict = {1: 1, 2: 1, 3: 1}
33+
print(uniqueValues(aDict))

Final_Exam/Problem_6.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## DO NOT MODIFY THE IMPLEMENTATION OF THE Person CLASS ##
2+
class Person(object):
3+
def __init__(self, name):
4+
#create a person with name name
5+
self.name = name
6+
try:
7+
firstBlank = name.rindex(' ')
8+
self.lastName = name[firstBlank+1:]
9+
except:
10+
self.lastName = name
11+
self.age = None
12+
def getLastName(self):
13+
#return self's last name
14+
return self.lastName
15+
def setAge(self, age):
16+
#assumes age is an int greater than 0
17+
#sets self's age to age (in years)
18+
self.age = age
19+
def getAge(self):
20+
#assumes that self's age has been set
21+
#returns self's current age in years
22+
if self.age == None:
23+
raise ValueError
24+
return self.age
25+
def __lt__(self, other):
26+
#return True if self's name is lexicographically less
27+
#than other's name, and False otherwise
28+
if self.lastName == other.lastName:
29+
return self.name < other.name
30+
return self.lastName < other.lastName
31+
def __str__(self):
32+
#return self's name
33+
return self.name
34+
35+
class USResident(Person):
36+
"""
37+
A Person who resides in the US.
38+
"""
39+
def __init__(self, name, status):
40+
"""
41+
Initializes a Person object. A USResident object inherits
42+
from Person and has one additional attribute:
43+
status: a string, one of "citizen", "legal_resident", "illegal_resident"
44+
Raises a ValueError if status is not one of those 3 strings
45+
"""
46+
# Write your code here
47+
Person.__init__(self, name)
48+
if status == "citizen" or status == "legal_resident" or status == "illegal_resident":
49+
self.status = status
50+
else:
51+
raise ValueError
52+
53+
def getStatus(self):
54+
"""
55+
Returns the status
56+
"""
57+
# Write your code here
58+
return self.status
59+

Final_Exam/Problem_6/Problem_6-1.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Problem 6-1
2+
# 10.0/10.0 points (graded)
3+
# This question has 3 parts
4+
5+
# Consider the following hierarchy of classes:
6+
7+
class Person(object):
8+
def __init__(self, name):
9+
self.name = name
10+
def say(self, stuff):
11+
return self.name + ' says: ' + stuff
12+
def __str__(self):
13+
return self.name
14+
15+
class Lecturer(Person):
16+
def lecture(self, stuff):
17+
return 'I believe that ' + Person.say(self, stuff)
18+
19+
class Professor(Lecturer):
20+
def say(self, stuff):
21+
return self.name + ' says: ' + self.lecture(stuff)
22+
23+
class ArrogantProfessor(Professor):
24+
def say(self, stuff):
25+
return 'It is obvious that ' + self.say(stuff)
26+
27+
# As written, this code leads to an infinite loop when using the Arrogant Professor class.
28+
29+
# Change the definition of ArrogantProfessor so that the following behavior is achieved:
30+
31+
# e = Person('eric')
32+
# le = Lecturer('eric')
33+
# pe = Professor('eric')
34+
# ae = ArrogantProfessor('eric')
35+
36+
# >>> e.say('the sky is blue')
37+
# eric says: the sky is blue
38+
39+
# >>> le.say('the sky is blue')
40+
# eric says: the sky is blue
41+
42+
# >>> le.lecture('the sky is blue')
43+
# I believe that eric says: the sky is blue
44+
45+
# >>> pe.say('the sky is blue')
46+
# eric says: I believe that eric says: the sky is blue
47+
48+
# >>> pe.lecture('the sky is blue')
49+
# I believe that eric says: the sky is blue
50+
51+
# >>> ae.say('the sky is blue')
52+
# eric says: It is obvious that eric says: the sky is blue
53+
54+
# >>> ae.lecture('the sky is blue')
55+
# It is obvious that eric says: the sky is blue
56+
57+
# For this question, you will not be able to see the test cases we run. This problem will test your ability to come up with your own
58+
# test cases.
59+
60+
61+
# Paste your class here
62+
class ArrogantProfessor(Person):
63+
def say(self, stuff):
64+
return self.name + ' says: ' + 'It is obvious that ' + self.name + ' says: ' + stuff
65+
def lecture(self, stuff):
66+
return 'It is obvious that ' + self.name + ' says: ' + stuff
67+
68+
# Correct

Final_Exam/Problem_6/Problem_6-2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Problem 6-2
2+
# 10.0/10.0 points (graded)
3+
# You change your mind, and now want the behavior as described in Part 1, except that you want:
4+
5+
# >>> ae.say('the sky is blue')
6+
# eric says: It is obvious that I believe that eric says: the sky is blue
7+
8+
# >>> ae.lecture('the sky is blue')
9+
# It is obvious that I believe that eric says: the sky is blue
10+
11+
# Change the definition of ArrogantProfessor so that the behavior described above is achieved.
12+
13+
# Paste ONLY your ArrogantProfessor class in the box below. Do not leave any debugging print statements.
14+
15+
# For this question, you will not be able to see the test cases we run. This problem will test your ability to come up with your own test
16+
# cases.
17+
18+
19+
# Paste your class here
20+
class ArrogantProfessor(Person):
21+
def say(self, stuff):
22+
return self.name + ' says: ' + 'It is obvious that I believe that ' + self.name + ' says: ' + stuff
23+
def lecture(self, stuff):
24+
return 'It is obvious that I believe that ' + self.name + ' says: ' + stuff
25+
26+
# Correct

Final_Exam/Problem_6/Problem_6-3.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Problem 6-3
2+
# 15.0/15.0 points (graded)
3+
# You change your mind once more. You want to keep the behavior from Part 2, but now you would like:
4+
5+
# >>> pe.say('the sky is blue')
6+
# Prof. eric says: I believe that eric says: the sky is blue
7+
8+
# >>> ae.say('the sky is blue')
9+
# Prof. eric says: It is obvious that I believe that eric says: the sky is blue
10+
11+
# Change the Professor class definition in order to achieve this. You may have to modify your implmentation for a previous part to get
12+
# this to work.
13+
14+
# Paste ONLY the Professor class in the box below. Do not leave any debugging print statements.
15+
16+
# For this question, you will not be able to see the test cases we run. This problem will test your ability to come up with your own test
17+
# cases.
18+
19+
20+
# Paste your class here
21+
class Professor(Lecturer):
22+
def say(self, stuff):
23+
return 'Prof. ' + self.name + ' says: ' + self.lecture(stuff)
24+
25+
# Correct

Final_Exam/Problem_7.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Container(object):
2+
""" Holds hashable objects. Objects may occur 0 or more times """
3+
def __init__(self):
4+
""" Creates a new container with no objects in it. I.e., any object
5+
occurs 0 times in self. """
6+
self.vals = {}
7+
def insert(self, e):
8+
""" assumes e is hashable
9+
Increases the number times e occurs in self by 1. """
10+
try:
11+
self.vals[e] += 1
12+
except:
13+
self.vals[e] = 1
14+
def __str__(self):
15+
s = ""
16+
for i in sorted(self.vals.keys()):
17+
if self.vals[i] != 0:
18+
s += str(i)+":"+str(self.vals[i])+"\n"
19+
return s
20+
21+
class Bag(Container):
22+
def remove(self, e):
23+
""" assumes e is hashable
24+
If e occurs in self, reduces the number of
25+
times it occurs in self by 1. Otherwise does nothing. """
26+
if e in self.vals.keys():
27+
self.vals[e] -= 1
28+
29+
def count(self, e):
30+
""" assumes e is hashable
31+
Returns the number of times e occurs in self. """
32+
if e in self.vals.keys():
33+
return self.vals[e]
34+
else:
35+
return 0
36+
37+
def __add__(self, other):
38+
""" assumes e is hashable
39+
if bag 1 + bag 2 represent new bag """
40+
new_bag = Bag()
41+
42+
for item in self.vals.keys():
43+
if item in new_bag.vals.keys():
44+
new_bag.vals[item] += self.vals[item]
45+
else:
46+
new_bag.vals[item] = self.vals[item]
47+
for element in other.vals.keys():
48+
if element in new_bag.vals.keys():
49+
new_bag.vals[element] += other.vals[element]
50+
else:
51+
new_bag.vals[element] = other.vals[element]
52+
53+
return new_bag
54+
55+
class ASet(Container):
56+
def remove(self, e):
57+
"""assumes e is hashable, then remove"""
58+
if e in self.vals.keys():
59+
del self.vals[e]
60+
61+
def is_in(self, e):
62+
"""assumes e is hashable
63+
returns True if subsequently in self and False otherwise."""
64+
if e in self.vals.keys():
65+
return True
66+
else:
67+
return False
68+
69+
d1 = ASet()
70+
d1.insert(4)
71+
print(d1.is_in(4))
72+
d1.insert(5)
73+
print(d1.is_in(5))
74+
d1.remove(5)
75+
print(d1.is_in(5))

MidTerm_Exam/Problem_1.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def myLog(x, b):
2+
'''
3+
x: a positive integer
4+
b: a positive integer; b >= 2
5+
6+
returns: log_b(x), or, the logarithm of x relative to a base b.
7+
'''
8+
# Your Code Here
9+
# Here I am checking exponential value what it will be. run the program!
10+
11+
exp = 2;
12+
if b**exp < x:
13+
while b**exp < x:
14+
exp = exp + 1
15+
if b**exp == x:
16+
return exp
17+
else:
18+
return exp-1
19+
else:
20+
return 0
21+
22+
23+
24+
print(myLog(15, 3))

0 commit comments

Comments
 (0)