Skip to content

Commit 33ca060

Browse files
committed
Adding Hw4
1 parent a308a05 commit 33ca060

8 files changed

Lines changed: 13365 additions & 0 deletions

File tree

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()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Created on Oct 22, 2014
3+
4+
@author: db345c
5+
'''
6+
7+
def safe_input():
8+
try:
9+
s = raw_input("Enter something: ")
10+
except EOFError, KeyboardInterupt:
11+
return None
12+
return s
13+
14+
if __name__ == "__main__":
15+
print safe_input()

0 commit comments

Comments
 (0)