Skip to content

Commit b9b0bf8

Browse files
committed
Adding Homework 5
1 parent ad5fd1b commit b9b0bf8

3 files changed

Lines changed: 280 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'''
2+
Created on Oct 30, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
import copy
8+
9+
def formatter():
10+
""" Use string fromatting with dictionary """
11+
food_prefs = {"name": u"Chris",
12+
"city": u"Seattle",
13+
"cake": u"chocolate",
14+
"fruit": u"mango",
15+
"salad": u"greek",
16+
"pasta": u"lasagna"}
17+
18+
print "{name} is from {city}, and he likes {cake}, {fruit} fruit, {salad} salad, and {pasta} pasta".format(**food_prefs)
19+
20+
def createHex1():
21+
""" Populate set with integer and hex equivalent using loop """
22+
s = {}
23+
for i in range(16):
24+
s[i] = hex(i)
25+
print ", ".join(str(j) + "-" + str(s[j]) for j in s)
26+
27+
def createHex2():
28+
""" Populate set using dictionary comprehension """
29+
s = {i: hex(i) for i in range(16)}
30+
print ", ".join(str(j) + "-" + str(s[j]) for j in s)
31+
32+
def numberOfA():
33+
""" Count the number of 'a' in the dictionary values """
34+
food_prefs = {"name": u"Chris",
35+
"city": u"Seattle",
36+
"cake": u"chocolate",
37+
"fruit": u"mango",
38+
"salad": u"greek",
39+
"pasta": u"lasagna"}
40+
my_prefs = copy.deepcopy(food_prefs)
41+
my_prefs = {i: my_prefs[i].count('a') for i in my_prefs }
42+
print my_prefs
43+
44+
def sets():
45+
""" Create three sets with numbers divisible by 2, 3, and 4 """
46+
s2 = (i for i in range(1, 21) if i % 2 == 0)
47+
print "\tMod (%) 2 -> " + ", ".join(str(j) for j in s2)
48+
s3 = (i for i in range(1, 21) if i % 3 == 0)
49+
print "\tMod (%) 3 -> " + ", ".join(str(j) for j in s3)
50+
s4 = (i for i in range(1, 21) if i % 4 == 0)
51+
print "\tMod (%) 3 -> " + ", ".join(str(j) for j in s4)
52+
53+
if __name__ == "__main__":
54+
print "Formatting String:",
55+
formatter()
56+
print
57+
print "With Loop:",
58+
createHex1()
59+
print
60+
print "List comprehension:",
61+
createHex2()
62+
print
63+
print "Number of A's:",
64+
numberOfA()
65+
print
66+
print "Printing sets:"
67+
sets()
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'''
2+
Created on Oct 28, 2014
3+
4+
@author: Aleksey
5+
'''
6+
7+
# Review
8+
d = {}
9+
d[1] = "that"
10+
d[2] = "this"
11+
print d
12+
13+
for key in sorted(d.keys()):
14+
print key, d[key]
15+
print
16+
17+
# Advanced arguments passing
18+
def f(x, y, w=0, h=0):
19+
print "position: %s, %s --shape: %s, %s" %(x, y, w, h)
20+
21+
f (3, 4, h=7)
22+
23+
shape = {'w':40, 'h':60}
24+
25+
# positional arguments followed by the dictionary
26+
f(3, 4, **shape)
27+
28+
def f1(*args, **kwargs):
29+
print "the positional arguments are:", args
30+
print "the keyword arguments are:", kwargs
31+
32+
# Returns empty tulpe for *args and empty dictionary for *kwargs
33+
f1()
34+
35+
formatter = "My name is {first} {last}"
36+
37+
person = {'first':'Chris', 'last':'Baker'}
38+
print formatter.format(2, 1, **person)
39+
40+
print
41+
42+
g = {}
43+
g["fore_color"]="white"
44+
g["back_color"]="green"
45+
g["link_color"]="blue"
46+
g["visited_color"]="maroon"
47+
48+
def colors1(**kwargs):
49+
print "the colors are %s, %s, %s, %s" %(tuple(kwargs.values()))
50+
51+
colors1(**g)
52+
53+
print
54+
55+
# Print unknowun number of arguments
56+
def print_everything(*args):
57+
for count, thing in enumerate(args):
58+
print '{0}. {1}'.format(count, thing)
59+
print_everything('apple', 'banana', 'cabbage')
60+
61+
print
62+
63+
# print unknown number of items
64+
def table_things(**kwargs):
65+
for name, value in kwargs.items():
66+
print '{0} = {1}'.format(name, value)
67+
table_things(apple='fruit', cabbage='vegetable')
68+
69+
# Print whole dictionary
70+
table_things(**g)
71+
72+
print
73+
74+
def my_d(**kwargs):
75+
for name, value in kwargs.items():
76+
print "The {0} is {1}".format(name, value)
77+
my_d(**g)
78+
79+
print
80+
81+
def print_three_things(a, b, c):
82+
print 'a = {0}, b = {1}, c = {2}'.format(a,b,c)
83+
mylist = ['aardvark', 'baboon', 'cat']
84+
print_three_things(*mylist)
85+
86+
lst = [i for i in range(5)]
87+
print lst
88+
89+
lst1 = [i * 4 for i in range(5)]
90+
print lst1
91+
92+
lst2 = ['first', 'that', 'the', 'other']
93+
lst3 = [s.upper() for s in lst2]
94+
print lst3
95+
96+
lst4 = [ (i, j) for i in range(3) for j in range(4,6)]
97+
print lst4
98+
99+
lst5 = [s.upper() for s in lst2 if s.startswith('t')]
100+
print lst5
101+
102+
lst6 = [name for name in dir(__builtins__) if "Error" in name]
103+
for l in lst6:
104+
print l
105+
106+
print
107+
108+
# List Comprehension
109+
feast = ['lambs', "sloths", "orangutans", "breakfast cereals", "fruit bats"]
110+
comprehansion = [delicacy.capitalize() for delicacy in feast]
111+
print comprehansion[0]
112+
print comprehansion[2]
113+
print
114+
comprehension = [delicacy for delicacy in feast if len(delicacy) > 6]
115+
print comprehension
116+
print len(feast)
117+
print len(comprehension)
118+
print
119+
list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')]
120+
comprehension = [ skit * number for number, skit in list_of_tuples ]
121+
print comprehension[0]
122+
print comprehension[2]
123+
print
124+
list_of_eggs = ['poached egg', 'fried egg']
125+
list_of_meats = ['lite spam', 'ham spam', 'fried spam']
126+
comprehension = [ '{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats]
127+
print len(comprehension)
128+
for i in comprehension:
129+
print i
130+
print
131+
comprehension = { x for x in 'aabbbcccc'}
132+
print comprehension
133+
print
134+
dict_of_weapons = {'first': 'fear', 'second': 'surprise', 'third':'ruthless efficiency', 'forth':'fanatical devotion', 'fifth': None}
135+
dict_comprehension = { k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon}
136+
print 'first' in dict_comprehension
137+
print 'FIRST' in dict_comprehension
138+
print len(dict_of_weapons)
139+
print len(dict_comprehension)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
Created on Oct 30, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
# 1.
8+
feast = ['lambs', 'sloths', 'orangutans', 'breakfast cereals', 'fruit bats']
9+
comprehension = [delicacy.capitalize() for delicacy in feast]
10+
# Guessed: Lambs and Oranges
11+
print comprehension[0]
12+
print comprehension[2]
13+
14+
# 2.
15+
feast = ['spam', 'sloths', 'orangutans', 'breakfast cereals', 'fruit bats']
16+
comprehension = [delicacy for delicacy in feast if len(delicacy) > 6]
17+
# Guessed: 5 and 3
18+
print len(feast)
19+
print len(comprehension)
20+
21+
# 3.
22+
list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')]
23+
comprehension = [ skit * number for number, skit in list_of_tuples ]
24+
# Guessed: lumberjeck and spamspamspamspam -> 16
25+
print comprehension[0]
26+
print comprehension[2]
27+
print len(comprehension[2])
28+
29+
# 4.
30+
list_of_eggs = ['poached egg', 'fried egg']
31+
list_of_meats = ['lite spam', 'ham spam', 'fried spam']
32+
# Guessed: 6, poached eggs and little spam
33+
comprehension = [ '{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats]
34+
print len(comprehension)
35+
print comprehension[0]
36+
37+
#5.
38+
comprehension = { x for x in 'aabbbcccc'}
39+
# Guessed: wrong!!!
40+
print comprehension
41+
42+
# 6.
43+
dict_of_weapons = {'first': 'fear', 'second': 'surprise', 'third':'ruthless efficiency',
44+
'forth':'fanatical devotion', 'fifth': None}
45+
dict_comprehension = { k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon}
46+
# Guessed: FIRST, 5 and 4
47+
print 'first' in dict_comprehension
48+
print 'FIRST' in dict_comprehension
49+
print len(dict_of_weapons)
50+
print len(dict_comprehension)
51+
52+
# 7.
53+
# Returning the number of even ints
54+
arr = [2, 1, 2, 3, 4]
55+
arr = (x for x in arr if x % 2 == 0)
56+
print "The number of even ints in the [2, 1, 2, 3, 4] list is:", len(list(arr))
57+
58+
arr = [2, 2, 0]
59+
arr = (x for x in arr if x % 2 == 0)
60+
print "The number of even ints in the [2, 2, 0] list is:", len(list(arr))
61+
62+
arr = [1, 3, 5]
63+
arr = (x for x in arr if x % 2 == 0)
64+
print "The number of even ints in the [1, 3, 5] list is:", len(list(arr))
65+
66+
# The same as above but with function
67+
def count_events(nums):
68+
print "The number of even ints in the", nums, "is:",
69+
arr = (x for x in nums if x % 2 == 0)
70+
print len(list(arr))
71+
72+
if __name__ == "__main__":
73+
count_events([2, 1, 2, 3, 4])
74+

0 commit comments

Comments
 (0)