Skip to content
Open
12 changes: 12 additions & 0 deletions ClassWeek2/globalVariable_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

x = 1

def increment():
global x
x = x+1



increment()

print x
3 changes: 3 additions & 0 deletions ClassWeek2/week2quiz2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import math

help(math.sin)
12 changes: 12 additions & 0 deletions ClassWeek2/week2quiz4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

def k(a,b,c=6):
""" This is a function that takes 3 variales a,b,c and prints result (a*b)+c or 2 variables a,b and prints the result'
(a*b)+6(default)"""
print (a+b)*6


help(k)


print k(1,2,3)

17 changes: 17 additions & 0 deletions ClassWeek3/class3Boolean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import math
a=2+3j

if 0+0j:
print True
else:
print False


print 11.0/10==1.1

print 11.1/10

print 11.1/10==1.11


print 0,1,2,3,4 ,"done"
9 changes: 9 additions & 0 deletions ClassWeek4/Week4_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
f = open("../resource/Test.txt","r+")


for line in f:
print line

f.write("\nHello\n")

f.close()
11 changes: 11 additions & 0 deletions ClassWeek4/classquiz3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#c = file("test.dat")
f=file("test.dat","a+")
f.seek(20)
f.write("testing".ljust(20))
f.write("Eric".ljust(20))
f.seek(120)
f.write("Sam".ljust(20))
f.seek(40)
s=f.read(20)
print s
f.close()
1 change: 1 addition & 0 deletions ClassWeek4/test.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing Eric Sam testing Eric Sam
22 changes: 22 additions & 0 deletions ClassWeek7/classQuiz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

class c(object):
def __init__(self,x,y):
self.x = x
self.y = y

def __str__(self):
return "x = %g and y = %g" %(self.x,self.y)

def __repr__(self):
return repr("x = %g and y = %g" %(self.x,self.y))



if __name__ == "__main__":
instC = c(5,4)
print instC
print instC.x
print instC.y
print str(instC)
print repr(instC)
print str(instC) == repr(instC)
32 changes: 32 additions & 0 deletions chapter10/exercise10_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


def cumulativeSumOfList(origList):
"""
Function that takes a list and return a new list where ith position is
the cumulative sum of original list till ith position
"""
cSumList = list()
i =1
for i in range(len(origList)):
cSumList.append(sum(origList[0:i+1]))
return cSumList
k =[1,2,3]
l = cumulativeSumOfList(k)

print "original list = ",k,"\nCumulative Sum = ", l

k1 =[3,5,7,9]
l1 = cumulativeSumOfList(k1)

print "original List = ",k1,"\nCumulative Sum = ", l1


k2 =[1,2,3,4,5,6,7,8,9,10]
l2 = cumulativeSumOfList(k2)

print "original List= ",k2,"\nCumulative Sum = ", l2





27 changes: 27 additions & 0 deletions chapter10/exercise10_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

def chop(Origlist):
"""
This fuction takes a list and remove first and last element
"""
del Origlist[0]
del Origlist[len(list)-1]


list1 = [1,2,3,4,5]
print list1
chop(list1)
print list1


def middle(Origlist):
"""
This functon takes a list and returns the a new list that
contains all but first and last element of the origial list
"""
return Origlist[1:len(list)-1]

list2 = [1,2,3,4,5,6,7,8]
list3 = middle(list2)

print list2
print list3
24 changes: 24 additions & 0 deletions chapter10/exercise10_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

def is_sorted1(OrigList):
i = 0
while i< len(OrigList)-1:
if OrigList[i]>OrigList[i+1]:
return False
i +=1
return True

def is_sorted2(OrigList):
SortedList = sorted(OrigList)
return OrigList == SortedList




if __name__ == "__main__":
k1 =[1,1,1,2,3,3,3,4,4,4,5,100]
print "Given list ",k1," sorted = ",is_sorted1(k1)
print "Given list ",k1," sorted = ",is_sorted2(k1)
k2 =[1,1,1,2,5,5,5,0,0,0,4,4,4]
print "Given list ",k2," sorted = ",is_sorted1(k2)
print "Given list ",k2," sorted = ",is_sorted2(k2)

27 changes: 27 additions & 0 deletions chapter10/exercise10_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import string

def is_anagram(str1,str2):
if len(str1)!=len(str2):
return False
str1List = list(str1.lower())
str1List.sort()
str2List = list(str2.lower())
str2List.sort()

return str1List == str2List


if __name__ == '__main__':
def test_is_anagram(word1,word2):
result = is_anagram(word1, word2)
if result:
print " strings %s and %s are anagrams" %(word1,word2)
else:
print " strings %s and %s are not anagrams" %( word1 , word2 )

test_is_anagram("tops", "pots")
test_is_anagram("No", "on")
test_is_anagram("moon", "noon")



51 changes: 51 additions & 0 deletions chapter10/exercise10_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import random

def has_duplicates(OrigList):
SortedList = sorted(OrigList)
i = 0
while i< len(SortedList)-1:
if SortedList[i]==SortedList[i+1]:
return True
i +=1
return False

def random_bdays(n):
t = []
for i in range(n):
bday = random.randint(1, 365)
t.append(bday)
return t


def count_matches(students, samplesSize):
count = 0
for i in range(samplesSize):
t = random_bdays(students)
if has_duplicates(t):
count += 1
return count

if __name__ == "__main__":
def test_has_duplicates(OrgList):
if(has_duplicates(OrgList)):
print "list ",OrgList,"has duplicates"
else:
print "list ",OrgList,"has no duplicates"

list1 = [1,2,3,4,5,5,3,2]
list2 = [10,5,3,4,7,2,8,1]
list3 = [1,2,3,"one","two","an"]
list4 = [1,2,3,"one","two","an",3]

test_has_duplicates(list1)
test_has_duplicates(list2)
test_has_duplicates(list3)
test_has_duplicates(list4)

print "No of dupe bdays from class of 23 students, sampled 1000 times is :", count_matches(23, 1000)
print "No of dupe bdays from class of 23 students, sampled 5000 times is :", count_matches(23, 5000)
print "No of dupe bdays from class of 50 students, sampled 1000 times is :", count_matches(50, 1000)
print "No of dupe bdays from class of 100 students, sampled 1500 times is :", count_matches(100, 1500)



18 changes: 18 additions & 0 deletions chapter10/exercise10_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


def remove_duplicates(origList):
dupeLessList = list()

for item in origList:
if item not in dupeLessList:
dupeLessList.append(item)

return dupeLessList



if __name__ == "__main__":
list1 = [1,2,3,4,1,2,3,4,1,2,3,4,5]
print "list ",list1,"after removing dupes is",remove_duplicates(list1)
list2 = [1,2,"one","three",[1,3],"five",2,[1,2],1,3,[1,3]]
print "list ",list1,"after removing dupes is",remove_duplicates(list2)
40 changes: 40 additions & 0 deletions chapter10/exercise10_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import time


file_path = "../resource/words.txt"

def make_word_list1():
"""Reads lines from a file and builds a list using append."""
t = []
fin = open(file_path)
for line in fin:
word = line.strip()
t.append(word)
return t


def make_word_list2():
"""Reads lines from a file and builds a list using list +."""
t = []
fin = open(file_path)
for line in fin:
word = line.strip()
t = t + [word]
return t


start_time = time.time()
t = make_word_list1()
elapsed_time = time.time() - start_time

print len(t)
print t[:10]
print elapsed_time, 'seconds'

start_time = time.time()
t = make_word_list2()
elapsed_time = time.time() - start_time

print len(t)
print t[:10]
print elapsed_time, 'seconds'
Loading