Skip to content

Commit ced4d67

Browse files
committed
mandarin converter
1 parent 0faf92d commit ced4d67

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

class_hashable.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 ASet(Container):
22+
def remove(self, e):
23+
"""assumes e is hashable
24+
removes e from self"""
25+
try:
26+
del(self.vals[e])
27+
except:
28+
return None
29+
30+
def is_in(self, e):
31+
"""assumes e is hashable
32+
returns True if e has been inserted in self and
33+
not subsequently removed, and False otherwise."""
34+
return e in self.vals
35+
36+
d1 = ASet()
37+
d1.insert(4)
38+
print(d1.is_in(4))
39+
d1.insert(5)
40+
print(d1.is_in(5))
41+
d1.remove(5)
42+
print(d1.is_in(5))

class_resident.py

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

dict_interdiff.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def dict_interdiff(d1, d2):
2+
'''
3+
d1, d2: dicts whose keys and values are integers
4+
Returns a tuple of dictionaries according to the instructions above
5+
'''
6+
interselect = {}
7+
difference = {}
8+
9+
for key1, value1 in d1.items():
10+
for key2, value2 in d2.items():
11+
if key1 == key2:
12+
interselect[key1] = f(value1, value2)
13+
14+
for key1, value1 in d1.items():
15+
uniq = True
16+
for key2, value2 in d2.items():
17+
if key1 == key2:
18+
uniq = False
19+
if uniq:
20+
difference[key1] = value1
21+
22+
for key2, value2 in d2.items():
23+
uniq = True
24+
for key1, value1 in d1.items():
25+
if key1 == key2:
26+
uniq = False
27+
if uniq:
28+
difference[key2] = value2
29+
30+
return interselect, difference
31+
32+
33+
def f(a, b):
34+
return a + b
35+
36+
d1 = {1:30, 2:20, 3:30, 5:80}
37+
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
38+
39+
print(dict_interdiff(d1, d2))

mandarin_num.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def convert_to_mandarin(us_num):
2+
'''
3+
us_num, a string representing a US number 0 to 99
4+
returns the string mandarin representation of us_num
5+
'''
6+
trans = {'0':'ling', '1':'yi', '2':'er', '3':'san', '4': 'si',
7+
'5':'wu', '6':'liu', '7':'qi', '8':'ba', '9':'jiu', '10': 'shi'}
8+
num = int(us_num)
9+
10+
if num < 11:
11+
mandarin = trans[us_num]
12+
elif num < 20:
13+
mandarin = trans['10'] + " " + trans[str(num % 10)]
14+
elif num % 10 == 0:
15+
mandarin = trans[str(num // 10)] + " " + trans['10']
16+
else:
17+
mandarin = trans[str(num // 10)] + " " + trans['10'] + " " + trans[str(num % 10)]
18+
19+
return mandarin
20+
21+
print(convert_to_mandarin("36"))

odd_times.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def largest_odd_times(L):
2+
""" Assumes L is a non-empty list of ints
3+
Returns the largest element of L that occurs an odd number
4+
of times in L. If no such element exists, returns None """
5+
odd = 0
6+
nums = {}
7+
odd_times = {}
8+
for num in L:
9+
nums[num] = nums.get(num, 0) + 1
10+
11+
for key, num in nums.items():
12+
if num % 2 != 0:
13+
odd_times[key] = num
14+
15+
for num in odd_times.keys():
16+
if num > odd:
17+
odd = num
18+
19+
if odd == 0:
20+
return None
21+
22+
return odd
23+
24+
print(largest_odd_times([6, 8, 6, 8, 6, 8, 6, 8, 6, 8]))

0 commit comments

Comments
 (0)