Skip to content

Commit 50b44ad

Browse files
committed
2 parents 644ebb4 + 4fa50b0 commit 50b44ad

Some content is hidden

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

51 files changed

+15575
-73
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
slides_sources/build
2+
.idea
3+
.DS_Store

Examples/Session01/students.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Brand, Ralph P : C, C++, SQL,
66
Buer, Eric : matlab, VBA, SQL,
77
Claessens, Michel : VBA, basic SQL, pascal, matlab
88
Conde, Ousmane :
9-
Davis, Bryan L :
9+
Davis, Bryan L : Javascript, PHP, SQL, Bash
1010
Davis, Ian M : C++
1111
Evans, Carolyn J : SQL,
1212
Fischer, Henry B : C, VB, SQL, SAS,

Solutions/Session04/dict_set_solution.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@
7777

7878

7979
# replacing the values in the original dict:
80-
81-
for key, val in d.items():
82-
d[key] = val.count('t')
80+
d.update(a_dict)
8381
print d
8482

83+
# could also do it this way.
84+
#for key, val in d.items():
85+
# d[key] = val.count('t')
86+
#print d
87+
8588
# Create sets s2, s3 and s4 that contain numbers from zero through
8689
# twenty, divisible 2, 3 and 4.
8790

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Created on Oct 17, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
def rot13(s):
8+
9+
ret = ""
10+
11+
for c in s:
12+
var = ord(c)
13+
if var >= ord("a") and var <= ord("z"):
14+
if var > ord("m"):
15+
var -= 13
16+
else:
17+
var += 13
18+
if var >= ord("A") and var <= ord("Z"):
19+
if var > ord("M"):
20+
var -= 13
21+
else:
22+
var += 13
23+
24+
ret += chr(var)
25+
26+
return ret
27+
28+
if __name__ == "__main__":
29+
print rot13(rot13("Aleksey Kramer"))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Created on Oct 17, 2014
3+
4+
@author: db345c
5+
'''
6+
import string
7+
8+
def rot13(s):
9+
dt = string.maketrans(
10+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .!<>;:'",
11+
"NOPQRSTUVWXYZabcdefgjijklmnopqrstuvwxyzABCDEFGHIJKLM .!<>;:'")
12+
13+
return string.translate("Hello World!", dt)
14+
15+
if __name__ == '__main__':
16+
one = rot13("Zntargvp sebz bhgfvqr arne pbeare")
17+
two = rot13(one)
18+
print one
19+
print two
20+
21+
### Does not work propery
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Created on Oct 23, 2014
3+
4+
@author: db345c
5+
'''
6+
import os
7+
8+
def printCurrentDirectory(dir1=os.getcwd()):
9+
""" Print the content of the directory """
10+
lst = os.listdir(dir1)
11+
print dir1
12+
for f in lst:
13+
print os.path.join(dir1, f)
14+
15+
if __name__ == "__main__":
16+
""" Print the content of the directory """
17+
printCurrentDirectory()
18+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Created on Oct 23, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
8+
def fileCopy(src, dest):
9+
""" copy of a content of a text file into another text file """
10+
try:
11+
frm = open(src, "r")
12+
to = open(dest, "w")
13+
except IOError:
14+
print "Error opening either source or dest. files"
15+
else:
16+
# The actual copy
17+
for line in frm:
18+
to.write(line)
19+
print "Copy successful"
20+
try:
21+
frm.close()
22+
to.close()
23+
except IOError:
24+
print "Error closing either source or dest. files"
25+
26+
27+
if __name__ == "__main__":
28+
""" Copy single file """
29+
from1 = r"C:\Users\db345c\Desktop\Personal Folders\Eclipse_Wrokspaces\IntroToPythonUW\Week 4 Homework\sherlock.txt"
30+
to1 = r"C:\Users\db345c\Desktop\Personal Folders\Eclipse_Wrokspaces\IntroToPythonUW\Week 4 Homework\sherlock2.txt"
31+
fileCopy(from1, to1)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Created on Oct 22, 2014
3+
4+
@author: db345c
5+
'''
6+
import string
7+
8+
def uniqueLanguages(filename):
9+
# Creating set variable
10+
s = set()
11+
12+
# Opening the file
13+
try:
14+
f = open(filename, "r")
15+
except IOError:
16+
print "Unable to open the file " + filename
17+
try:
18+
f.close()
19+
except Exception:
20+
return
21+
22+
# Populating the set
23+
for student in f:
24+
(name, lang) = student.split(":")
25+
lst = lang.split()
26+
for i, j in enumerate(lst):
27+
lst[i] = j.strip(string.punctuation)
28+
s.add(lst[i].lower())
29+
30+
# Trying to close the file
31+
try:
32+
f.close()
33+
except IOError:
34+
try:
35+
f.close()
36+
except:
37+
return
38+
39+
# cheap way of removing fixed header
40+
try:
41+
s.remove("languages")
42+
except KeyError:
43+
print "languages is not in the set"
44+
45+
# print unique languages
46+
for i in s:
47+
print i.capitalize()
48+
49+
50+
if __name__ == "__main__":
51+
uniqueLanguages("students.txt")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Created on Oct 17, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
import string
8+
9+
def kata14(filename, start):
10+
""" Set up variables, split lines in the groups of three, populate
11+
dictionary, and call function to print kata14 """
12+
# create dictionary
13+
words = {}
14+
# open file
15+
f = open(filename, "r")
16+
count = 0
17+
for line in f:
18+
# Split the line into separate tokens
19+
lst = line.split()
20+
21+
# discard all the lines containing less than three words
22+
if len(lst) > 2:
23+
24+
# Remove punctuation with string.punctuation (import string)
25+
for j, k in enumerate(lst):
26+
lst[j] = k.strip(string.punctuation)
27+
28+
# split the line in the groups of three words
29+
lst_length = len(lst)
30+
for idx, i in enumerate(lst):
31+
# assure the last element of the list is reached, if so, break
32+
if idx == lst_length - 2:
33+
break
34+
else:
35+
# got the three strings to use and add those to the dictionary
36+
l = [ (lst[idx] + " " + lst[idx + 1]).strip(), lst[idx + 2].strip()]
37+
buildDictionary(l, words)
38+
count += 1
39+
# close file
40+
f.close()
41+
42+
# print kata14
43+
printKata14(words, start)
44+
45+
def buildDictionary(l, words):
46+
""" Populate the dictionary """
47+
if l[0] in words:
48+
words[l[0]].append(l[1])
49+
else:
50+
words[l[0]] = [l[1]]
51+
return words
52+
53+
def printKata14(word, start):
54+
""" Recursively print Kata14 """
55+
if start in word:
56+
if len(word[start]) > 0:
57+
temp = word[start].pop()
58+
print start, temp,
59+
printKata14(word, str(start.split()[1]) + " " + temp)
60+
else:
61+
return
62+
63+
if __name__ == "__main__":
64+
""" Execute Kata14 algorythm """
65+
kata14("sherlock.txt", "little use")
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'''
2+
Created on Oct 21, 2014
3+
4+
@author: Aleksey
5+
'''
6+
7+
def inClass():
8+
9+
l = range(10)
10+
11+
for i in range(len(l)):
12+
print i
13+
print
14+
15+
# iterate over the tuples in the list
16+
l = [(1,2), (3,4), (5,6)]
17+
18+
for i, j in l:
19+
print i, j
20+
print
21+
22+
# zip function
23+
l1 = [1,2,3]
24+
l2 = [4,5,6]
25+
print zip(l1, l2)
26+
for i, j in zip(l1, l2):
27+
print i, j
28+
print
29+
30+
# returns tuple
31+
k = ["Aleksey", "is", "my", "name"]
32+
for i in enumerate(k):
33+
print i
34+
35+
# sorting example
36+
fruits = ["Apple", "Pears", "Oranges", "Peaches"]
37+
numbers = range(4)
38+
combined = zip(fruits, numbers)
39+
combined.sort()
40+
print combined
41+
42+
# sorting by a key
43+
# need to create a function to pass to sort via key argument
44+
# the example is below
45+
def MySort(item):
46+
return item[1]
47+
48+
fruits = ["Apple", "Pears", "Oranges", "Peaches"]
49+
numbers = range(4)
50+
combined = zip(fruits, numbers)
51+
combined.sort(key=MySort)
52+
print combined
53+
54+
def print_me(nums):
55+
s = str(nums).strip("()")
56+
print "Print the first %d numbers: %s"%(len(nums),s)
57+
print_me((2,3,4,5))
58+
59+
d = {}
60+
d["Aleksey"] = "Kramer"
61+
d["Jon"] = "Doe"
62+
d["Jane"] = "Doe"
63+
64+
print d
65+
print d.__contains__("Aleksey")
66+
print d.keys()
67+
print d.values()
68+
69+
d["My"] = "Name"
70+
71+
print "%s, %s"%(d.keys(), d.values())
72+
73+
print "Aleksey" in d
74+
print "Nona" in d
75+
print d.items()
76+
77+
for i, j in d.items():
78+
print i, j
79+
80+
d = {}
81+
d["name"] = "Chris"
82+
d["city"] = "Seattle"
83+
d["cake"] = "Chocolate"
84+
85+
print d
86+
d.__delitem__("name")
87+
d.pop("city")
88+
print d
89+
d["fruit"] = "Mango"
90+
print d
91+
print "cake" in d
92+
print "fruit" in d
93+
94+
95+
96+
if __name__ == "__main__":
97+
inClass()

0 commit comments

Comments
 (0)