Skip to content

Commit 5e17cab

Browse files
committed
2 parents cd38f90 + 4280b7d commit 5e17cab

63 files changed

Lines changed: 2220 additions & 106 deletions

Some content is hidden

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

‎materials/source/notes/session03.rst‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,22 @@ Are you ready? We'll do them in the middle of the session.
2626
Issues that came up during the week.
2727
====================================
2828

29+
Separation of concerns
30+
----------------------
31+
From print_grid: if you are going to have separate functions, better for them to return a string, and then put all the printing in the calling function, on one place. That would make it more re-usable -- say you want to write to a file?
32+
33+
This is a tiny example of what's known as "separation of concerns"
34+
35+
Make use of symmetry
36+
--------------------
37+
38+
nice trick:
39+
40+
.. code-block:: python
41+
42+
def gene_line(char_a, char_b, n):
43+
line = char_a + ' ' + ((n - 1) // 2) * (char_b + ' ')
44+
line = line + char_a + line[::-1]
45+
46+
47+

‎session01/break_me.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Description: In the break_me.py file write four simple Python functions:
2+
# Each function, when called, should cause an exception to happen
3+
# Each function should result in one of the four most common exceptions you’ll find.
4+
# for review: NameError, TypeError, SyntaxError, AttributeError
5+
# Comment: Execute this script in Python3.6
6+
# Last modified: 10/07/2017 - davidkan@
7+
8+
9+
class PracticeExceptions:
10+
11+
def __init__(self):
12+
"""
13+
Creates a constructor when the instance is initialized
14+
"""
15+
self.python = 'python'
16+
self.java = 'java'
17+
self.c_sharp = 'c#'
18+
19+
@staticmethod
20+
def SyntaxErrorExample():
21+
"""
22+
Function will demo a SyntaxError exception
23+
:return: None
24+
"""
25+
a = 1
26+
b = 'b'
27+
try:
28+
eval("1 + a")
29+
eval("1 === b")
30+
except SyntaxError as s:
31+
print("That won't work. Try again")
32+
print(s)
33+
34+
def AttributeErrorExample(self):
35+
"""
36+
Function will demo an AttributeError exception
37+
:return: None
38+
"""
39+
try:
40+
print(self.python)
41+
print(self.java)
42+
print(self.c_sharp)
43+
print(self.sql)
44+
except AttributeError as a:
45+
print("This does not exist. Try again.")
46+
print(a)
47+
48+
@staticmethod
49+
def TypeErrorExample():
50+
"""
51+
Function will demo a TypeError exception
52+
:param self:
53+
:return: None
54+
"""
55+
a = 1
56+
b = 'b'
57+
try:
58+
eval("1 + a")
59+
eval("1 + b")
60+
except TypeError as t:
61+
print("That won't work. Try again")
62+
print(t)
63+
64+
@staticmethod
65+
def NameErrorExample():
66+
"""
67+
Function will demo a NameError exception
68+
:return: None
69+
"""
70+
# Declare a variable and assign it a string
71+
school = "University of Washington"
72+
73+
try:
74+
if school:
75+
print(schools)
76+
except NameError as n:
77+
print("The varable does not exist. Try again")
78+
print(n)
79+
80+
81+
if __name__ == "__main__":
82+
"""
83+
Create an instance of the class and call all four functions
84+
"""
85+
# Create an instance
86+
common_errors = PracticeExceptions()
87+
88+
# Call the functions
89+
common_errors.NameErrorExample()
90+
common_errors.TypeErrorExample()
91+
common_errors.SyntaxErrorExample()
92+
common_errors.AttributeErrorExample()
93+
94+

‎solutions/Session01/codingbat/Logic-1/cigar_party.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def cigar_party2(cigars, is_weekend):
1818
"""
1919
if is_weekend:
2020
return (cigars >= 40)
21-
else:
22-
return (cigars >= 40 and cigars <= 60)
21+
return (cigars >= 40 and cigars <= 60)
2322

2423

2524
def cigar_party3(cigars, is_weekend):

‎students/Amit/first.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hello There,
2+
3+
I am Amit. I am learning python
4+
5+
update1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Function 1: Produce NameError.
2+
def fun1():
3+
print(non_existing_name)
4+
5+
# Function 2: Produce TypeError.
6+
def fun2():
7+
a = 1 + '2'
8+
9+
# Function 3: Produce SyntaxError.
10+
def fun3():
11+
eval('1***3')
12+
13+
# Function 4: AttributeError.
14+
def fun4():
15+
c = 0
16+
c.append(1)
17+
18+
# Main Function
19+
def main():
20+
option = input("What error do you want? (NameError press '1', TypeError press '2', SyntaxError press '3', AttributeError press'4') ")
21+
if option == '1':
22+
fun1()
23+
elif option == '2':
24+
fun2()
25+
elif option == '3':
26+
fun3()
27+
elif option == '4':
28+
fun4()
29+
else:
30+
print("Try again.")
31+
32+
main()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def sleep_in(weekday, vacation):
2+
return(not weekday or vacation)
3+
4+
def monkey_trouble(a_smile, b_smile):
5+
return(not(a_smile ^ b_smile))
6+
7+
def sum_double(a, b):
8+
if a == b:
9+
return (a+b)*2
10+
else:
11+
return a+b
12+
13+
def diff21(n):
14+
m = n - 21
15+
if m > 0:
16+
m *= 2
17+
else:
18+
m = 21 - n
19+
return m
20+
21+
def parrot_trouble(talking, hour):
22+
if talking and ((hour < 7) or (hour > 20)):
23+
return True
24+
else:
25+
return False
26+
27+
def makes10(a, b):
28+
return((a == 10) or (b == 10) or (a + b == 10))
29+
30+
def near_hundred(n):
31+
return((abs(n-100) <= 10) or (abs(n-200) <= 10))
32+
33+
def pos_neg(a, b, negative):
34+
if negative:
35+
return (a<0 and b<0)
36+
else:
37+
return(((a<0) ^ (b<0)))
38+
39+
def not_string(str):
40+
if str.startswith("not"):
41+
return(str)
42+
else:
43+
return("not "+str)
44+
45+
def missing_char(str, n):
46+
return((str[:n]) + (str[n+1:]))
47+
48+
def front_back(str):
49+
if len(str) <= 1:
50+
return str
51+
else:
52+
return(str[-1:] + str[1:-1] + str[0])
53+
54+
def front3(str):
55+
return(str[:3]*3)
56+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
def string_times(str, n):
2+
return str*n
3+
4+
def front_times(str, n):
5+
return(str[:3]*n)
6+
7+
def string_bits(str):
8+
n = ""
9+
for i in range(len(str)):
10+
if i%2 == 0:
11+
n += str[i]
12+
return n
13+
14+
def string_splosion(str):
15+
n = ""
16+
for i in range(len(str)):
17+
n += str[:i+1]
18+
return n
19+
20+
def last2(str):
21+
count = 0
22+
n = str[-2:]
23+
for i in range(len(str)-2):
24+
if str[i:i+2] == n:
25+
count += 1
26+
return count
27+
28+
def array_count9(nums):
29+
count = 0
30+
for i in nums:
31+
if i == 9:
32+
count += 1
33+
return count
34+
35+
def array_front9(nums):
36+
if len(nums) > 4:
37+
for i in range(4):
38+
if nums[i] == 9:
39+
return True
40+
else:
41+
for i in nums:
42+
if i == 9:
43+
return True
44+
return False
45+
46+
def array123(nums):
47+
if len(nums) >= 3:
48+
for i in range(len(nums)-2):
49+
if nums[i:i+3] == [1, 2, 3]:
50+
return True
51+
return False
52+
53+
def string_match(a, b):
54+
count = 0
55+
if (len(a) >= 2) and (len(b) >= 2):
56+
j = ""
57+
if a <= b:
58+
for i in range(len(a)-1):
59+
if a[i:i+2] == b[i:i+2]:
60+
count += 1
61+
else:
62+
for i in range(len(b)-1):
63+
if a[i:i+2] == b[i:i+2]:
64+
count += 1
65+
return count
66+
67+
68+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
def fibu():
4+
for i in range(100):
5+
x = i+1
6+
if x%15 == 0:
7+
print('FizzBuzz')
8+
elif x%3 == 0:
9+
print('Fizz')
10+
elif x%5 == 0:
11+
print('Buzz')
12+
else:
13+
print(x)
14+
15+
# Test time
16+
if __name__ == "__main__":
17+
fibu()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
# A fixed size grid
4+
def print_grid():
5+
6+
for i in range(11):
7+
if i%5==0:
8+
print("+ - - - - + - - - - +")
9+
else:
10+
print("| | |")
11+
12+
# 2x2 grid with a size variable
13+
def print_grid2(n):
14+
for i in range(2):
15+
print('+' + n*' -' + ' +' + n* ' -' + ' +')
16+
for i in range(n):
17+
print('|' + n*' ' + ' |' + n* ' ' + ' |')
18+
print('+' + n*' -' + ' +' + n* ' -' + ' +')
19+
20+
# 2 size variables now
21+
def print_grid3(n, m):
22+
row = (('+ ' + m*'- ')*n + '+' + '\n')
23+
column = ((('| ' + m*' ')*n + '|' + '\n')*m)
24+
block = row+column
25+
print(block*n + row)
26+
27+
# Some tests
28+
if __name__ == "__main__":
29+
print_grid()
30+
print_grid2(6)
31+
print_grid3(3, 5)

0 commit comments

Comments
 (0)