Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Question:Given a non-empty string and an int n, return a new string where
# the char at index n has been removed. The value of n will be a valid index
# of a char in the original string (i.e. n will be in the range 0..len(str)-1 inclusive).
#
# See example below:
# missing_char('kitten', 1) → 'ktten'
# missing_char('kitten', 0) → 'itten'
# missing_char('kitten', 4) → 'kittn'

# My solution
def missing_char(str, n):
strlength = len(str)
if n == 0:
str1 = str[1:strlength]
return str1
elif n == strlength:
str1 = str[:(strlength-1)]
return str1
else:
str1 = str[0:n] + str[(n+1):strlength]
return str1
# End

# Better solution:

def missing_char(str, n):
front = str[:n] # up to but not including n
back = str[n+1:] # n+1 through end of string
return front + back
# End
6 changes: 6 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/diff21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def diff21(n):
if n > 21:
return 2*abs(21-n)
else:
return abs(21-n)

6 changes: 6 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/front3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def front3(str):
if len(str) >= 3:
return str[:3] + str[:3] + str[:3]
else:
return str + str + str

10 changes: 10 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/front_back.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def front_back(str):
if len(str) == 1:
return(str)
else:
front = str[:1]
back = str[len(str)-1:]
middle = str[1:(len(str)-1)]
newstr = back + middle + front
return newstr

6 changes: 6 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/makes10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def makes10(a, b):
if a == 10 or b == 10 or a + b == 10:
return True
else:
return False

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def monkey_trouble(a_smile, b_smile):
if a_smile and b_smile:
return True
elif not a_smile and not b_smile:
return True
else:
return False

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def near_hundred(n):
if (abs(100 - n) <= 10) or (abs(200 - n) <= 10):
return True
else:
return False

5 changes: 5 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/not_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def not_string(str):
if str.find("not") == 0:
return str
else:
return "not" + " " + str
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def parrot_trouble(talking, hour):
if talking and (hour < 7 or hour >20):
return True
else:
return False

8 changes: 8 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/pos_neg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def pos_neg(a, b, negative):
if (a < 0 and b > 0 and not negative) or (a > 0 and b < 0 and not negative):
return True
elif (negative and a < 0 and b < 0):
return True
else:
return False

6 changes: 6 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/sleep_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
else:
return False

6 changes: 6 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-1/sum_double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def sum_double(a, b):
if a == b:
return 2*(a + b)
else:
return a + b

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def array123(nums):
n = len(nums)
if n < 3:
return(False)
elif n == 3:
if nums[0] == 1 and nums[1] == 2 and nums[2] == 3:
return(True)
else:
return(False)
elif n > 3:
count1 = 0
for i in range(0,n-2):
if (nums[i] == 1) and (nums[i+1] == 2) and (nums[i+2] == 3):
count1 = count1 + 1
if count1 >= 1:
return(True)
else:
return(False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def last2(str):
str1 = str[-2:]
n = str.count(str1) -1
return(n)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def array123(nums):
n = len(nums)
if n < 3:
return(False)
elif n == 3:
if nums[0] == 1 and nums[1] == 2 and nums[2] == 3:
return(True)
else:
return(False)
elif n > 3:
count1 = 0
for i in range(0,n-2):
if (nums[i] == 1) and (nums[i+1] == 2) and (nums[i+2] == 3):
count1 = count1 + 1
if count1 >= 1:
return(True)
else:
return(False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def array_count9(nums):
count1 = 0
for i in range(len(nums)):
if 9 - nums[i] == 0:
count1 = count1 + 1
return(count1)

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def array_front9(nums):
n = len(nums)
count1 = 0
if n >= 4:
for i in range(4):
if 9 - nums[i] == 0:
count1 = count1 + 1
if count1 >= 1:
return(True)
else:
return(False)
else:
for i in range(n):
if 9 - nums[i] == 0:
count1 = count1 + 1
if count1 >= 1:
return(True)
else:
return(False)
10 changes: 10 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-2/MUST --- last2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def last2(str):
str1 = str[-2:]
n = len(str)
count1 = 0
for i in range(n-1):
# only check substring with length 2 from far left to two to far right in the string.
if i+2 <= n-1:
if str[i:(i+2)] == str1:
count1 = count1 + 1
return(count1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def string_bits(str):
strlength = len(str)
newstr = ''
for i in range(1,strlength+1,2):
newstr = newstr + str[(i-1):i]
return(newstr)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def string_match(a, b):
count1 = 0
n = max(len(a),len(b))
for i in range(n-1):
if i+1 <= len(a) and i+1 <= len(b):
if a[i:(i+2)] == b[i:(i+2)]:
count1 = count1 + 1
return(count1)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def string_splosion(str):
newstr = str
newstr2 = str
strlength = len(str)
for i in range(1,strlength+1):
newstr1 = newstr[:(strlength - i)]
newstr2 = newstr1+ newstr2
return(newstr2)

20 changes: 20 additions & 0 deletions Students/Hui Zhang/Session 1/CodingBat/Warmup-2/array123-test.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def array123(nums):
print nums
n = len(nums)
print n
count1 = 0
for i in range(0,3):
if (nums[i] == 1) and (nums[i+1] == 2) and (nums[i+2] == 3):
print i
print nums[i], nums[i+1], nums[i+2]
count1 = count1 + 1
if count1 >= 1:
print count1
return(True)
else:
print count1
return(True)

array123([1, 1, 2, 3, 1])


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def front_times(str, n):
if len(str) >= 3:
return(n*str[:3])
else:
return(n*str)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def string_times(str, n):
return(n*str)

98 changes: 98 additions & 0 deletions Students/Hui Zhang/Session 1/break_me.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-------------
-------------
Create a funtcion to trigger AttributeError

def testatterror():
n = 5
n.append(4)
print n
.....:

In [104]: testatterror()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-104-e6cab665e2fa> in <module>()
----> 1 testatterror()

<ipython-input-103-d6290a627534> in testatterror()
1 def testatterror():
2 n = 5
----> 3 n.append(4)
4 print n
5

AttributeError: 'int' object has no attribute 'append'

In [105]:




-------------
-------------
Create a funtcion to trigger TypeError


def testtypeerror():
n = '5'
m = 5
mn = m + n
print mn
....:

In [99]: testtypeerror()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-99-2c3a32be449d> in <module>()
----> 1 testtypeerror()

<ipython-input-98-dc82486ac12c> in testtypeerror()
7 n = '5'
8 m = 5
----> 9 mn = m + n
10 print mn
11

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [100]:




-------------
-------------
Create a funtcion to trigger NameError
def testnameerror():
print nn


In [89]: testnameerror()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-89-23be1ea02b92> in <module>()
----> 1 testnameerror()

<ipython-input-88-e54aba5a1687> in testnameerror()
1 def testnameerror():
----> 2 print nn
3

NameError: global name 'nn' is not defined




-------------
-------------
Create a function to trigger SyntaxError

def testnameerror()
print n

In [85]: def testnameerror()
File "<ipython-input-85-0ef4b784a7a2>", line 1
def testnameerror()
^
SyntaxError: invalid syntax

8 changes: 8 additions & 0 deletions Students/Hui Zhang/Session 1/fungrid-p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# my fungrid
def fungrid():
for i in range(0, 2):
print'+ - - - - + - - - - +'
for n in range(0, 4):
print'| | |'
print'+ - - - - + - - - - +'
fungrid()
9 changes: 9 additions & 0 deletions Students/Hui Zhang/Session 1/fungrid-p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# my fungrid
n = input('Please input your number:' )
def fungrid():
for i in range(0, 2):
print '+' + n*'-' + '+' + n*'-' + '+'
for m in range(0, n):
print '|' + n*' ' + '|' + n*' ' + '|'
print '+' + n*'-' + '+' + n*'-' + '+'
fungrid()
9 changes: 9 additions & 0 deletions Students/Hui Zhang/Session 1/fungrid-p3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# my fungrid
n = input('Please input your number:' )
def fungrid():
for i in range(0, 3):
print '+' + n*'-' + '+' + n*'-' + '+' + n*'-' + '+'
for m in range(0, n):
print '|' + n*' ' + '|' + n*' ' + '|' + n*' ' + '|'
print '+' + n*'-' + '+' + n*'-' + '+' + n*'-' + '+'
fungrid()
8 changes: 8 additions & 0 deletions Students/Hui Zhang/Session 1/fungrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# my fungrid
def fungrid():
for i in range(0, 2):
print'+ - - - - + - - - - +'
for n in range(0, 4):
print'| | |'
print'+ - - - - + - - - - +'
fungrid()
Loading