Skip to content

Commit 8325fb6

Browse files
committed
Session 4 homework
1 parent 7c3a34a commit 8325fb6

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
sample_dict = {"name": "Chris", "city": "Seattle", "cake": "Chocolate"}
2+
3+
del sample_dict['cake']
4+
5+
print sample_dict
6+
7+
sample_dict['fruit'] = "Mango"
8+
9+
print sample_dict
10+
11+
print sample_dict.keys()
12+
print sample_dict.values()
13+
14+
print 'cake' in sample_dict.keys()
15+
print 'Mango' in sample_dict.values()
16+
17+
nums = list(range(0,15))
18+
hexidecimal = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
19+
20+
print dict(zip(nums, hexidecimal))
21+
22+
for key in sample_dict.keys():
23+
value = sample_dict[key].count('t')
24+
sample_dict[key] = value
25+
26+
s2 = set([i for i in range(1,21) if i % 2 == 0])
27+
s3 = set([i for i in range(1,21) if i % 3 == 0])
28+
s4 = set([i for i in range(1,21) if i % 4 == 0])
29+
30+
print s3.issubset(s2)
31+
print s4.issubset(s2)
32+
33+
python_set = set("Python")
34+
python_set.add("i")
35+
36+
marathon_set = frozenset("marathon")
37+
python_set.union(marathon_set)
38+
python_set.intersection(marathon_set)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def safe_input():
2+
try:
3+
input = raw_input("Try to escape this: ")
4+
except EOFError:
5+
print "That won't work"
6+
return None
7+
except KeyboardInterrupt:
8+
print "You used Ctrl-C or Ctrl-D."
9+
return None
10+
else:
11+
print "Okay that works"
12+
return input
13+
14+
15+
16+
17+
if __name__ == "__main__":
18+
safe_input()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from collections import Counter
3+
4+
print "The current working directory is: ", os.getcwd()
5+
print "\n"
6+
7+
path = 'C:\mystuff\pythonclass\introtopython\examples\session01'
8+
os.chdir(path)
9+
10+
print "Now the current working directory is: ", os.getcwd()
11+
print "\n"
12+
print "The files in this directory are: ", os.listdir(path)
13+
print "\n"
14+
15+
file = open("students.txt", "r")
16+
lines = file.readlines()
17+
18+
languages = []
19+
20+
for line in lines[1:]:
21+
for item in line[line.index(':') + 1:].split(', '):
22+
languages.append(item.strip())
23+
24+
for index, value in enumerate(languages):
25+
if value == '':
26+
languages.pop(index)
27+
28+
hash_languages = Counter(languages)
29+
30+
for key, value in hash_languages.iteritems():
31+
if key == '':
32+
pass
33+
else:
34+
print "%d student(s) know the '%s' language" % (value, key)
35+
36+
file.close()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def sieve_eratosthenes(n):
2+
"""
3+
Generate a list of odd integers from 3 to "n".
4+
Use the first element in the list to iterate over the
5+
remaining elements to remove it's multiples. Rinse and repeat.
6+
"""
7+
numbers = range(3, n + 1, 2)
8+
for index, value in enumerate(numbers):
9+
for num in numbers[index + 2:]:
10+
if num % value == 0:
11+
numbers.remove(num)
12+
numbers.append(2)
13+
return sorted(numbers)
14+
15+
if __name__ == "__main__":
16+
print sieve_eratosthenes(50)
17+
18+

0 commit comments

Comments
 (0)