In [19]:
# http://codingbat.com/prob/p110166
def array_front9(nums):
subset = set(nums[:4])
newset = subset.intersection(set([9]))
return True if len(newset) > 0 else False
In [2]:
array_front9([1,2,3,4,5,6,7,9])
In [3]:
array_front9([1,2])
In [24]:
array_front9([9,9,3,9,4])
Out[24]:
In [15]:
[1,2,3][-1]
Out[15]:
In [25]:
def array123(nums):
for i in range(len(nums)):
if nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3:
print("found")
In [26]:
array123([1,1,2,3,4,5])
In [40]:
# http://codingbat.com/prob/p182414
def string_match(a, b):
min_len = min(len(a), len(b))
for i in range(min_len):
#print("A = %s" % a[i:i+2])
#print("B = %s" % b[i:i+2])
a_sub = a[i:i+2]
b_sub = b[i:i+2]
if a_sub == b_sub and len(a_sub) == 2 and len(b_sub) == 2:
print("Matches")
In [39]:
string_match("abc", "abc")
In [41]:
#http://codingbat.com/prob/p132290
def make_tag(tags, word):
return "<{t}>{w}</{t}>".format(t=tags, w=word)
In [42]:
make_tag("cite", "Yay")
Out[42]:
In [43]:
nums = [1, 2, 3]
In [46]:
reduce(lambda x, y: x+y, nums)
Out[46]:
In [48]:
def rotate_left3(nums):
return nums[1:].append(nums[0])
In [51]:
val = rotate_left3(nums)
In [52]:
val
In [53]:
print(val)
In [54]:
nums[1:]
Out[54]:
In [57]:
new = nums[1:].append([100])
In [56]:
nums
Out[56]:
In [58]:
new
In [59]:
nums
Out[59]:
In [60]:
nums[0]
Out[60]:
In [62]:
nums[1:] + [nums[0]]
Out[62]:
In [65]:
#http://codingbat.com/prob/p192589
#if len 0, return 0, otherwise add up first 2 elements and return
def sum2(nums):
if len(nums) == 0:
return 0
return reduce(lambda x, y: x+y, nums[:2])
In [66]:
sum2(nums)
Out[66]:
In [76]:
# http://codingbat.com/prob/p116620
def sorta_sum(a, b):
result = a + b
print(result)
returned_result = result if (result < 10 or result > 20) else 20
print(returned_result)
In [70]:
sorta_sum(9, 10)
Out[70]:
In [77]:
sorta_sum(3, 4)
In [78]:
sorta_sum(4, 6)
In [80]:
# http://codingbat.com/prob/p143951
def lone_sum(a, b, c):
return reduce(lambda x, y: x+y, set([a, b, c]))
In [94]:
def fix_teen(n):
if 13 <= n < 15:
print('Between 13 and 15')
if 16 < n <= 19:
print('Between 16 and 19')
return n
In [83]:
fix_teen(11)
Out[83]:
In [85]:
fix_teen(11)
Out[85]:
In [95]:
fix_teen(15)
Out[95]:
In [99]:
# http://codingbat.com/prob/p160533
# this one is poorly worded
def close_far(a, b, c):
b_is_close = True if abs(a - b) <= 1 else False
c_is_close = True if abs(a - c) <= 1 else False
if b_is_close:
return (abs(b - c) >= 2 and abs(a - c) >= 2)
elif c_is_close:
return (abs(b - a) >= 2 and abs(b - c) >= 2)
else:
return False
In [100]:
close_far(4, 5, 3)
Out[100]:
In [119]:
['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, 16)]
Out[119]:
In [120]:
"1".isdigit()
Out[120]:
In [126]:
player_input = raw_input("enter value")
if 1 <= player_input <= 9:
print("Digit")
else:
print("Not a digit")
In [146]:
# http://codingbat.com/prob/p190859
def make_chocolate(small, big, goal):
remaining = 0
for i in range(1, big + 1):
result = True if (goal - (i * 5)) > 0 else False
if result:
remaining = goal - (i * 5)
else:
break
print("Remaining {r}".format(r=remaining))
if small >= remaining:
return remaining
else:
return -1
In [152]:
make_chocolate(60, 100, 550)
Out[152]:
In [156]:
'codexxxxcoze'.count('co\[+]e')
Out[156]:
In [191]:
from re import *
m = findall('co[\w]{1}e', 'codecodexxxxcode')
print(m)
In [199]:
def count_code(str):
return_count = 0
for i in range(0, len(str) - 3):
print(str[i:i+2], str[i+3])
if str[i:i+2] == "co" and str[i+3] == "e":
return_count += 1
return return_count
count_code('codexxxcode')
Out[199]:
In [3]:
# http://codingbat.com/prob/p184853
def big_diff(nums):
largest = reduce(lambda x, y: x if x > y else y, nums)
smallest = reduce(lambda x, y: x if x < y else y, nums)
return largest - smallest
print big_diff([10, 3, 2, 4, 100])
print big_diff([7, 6, 8, 5])
print big_diff([10, 0])
In [15]:
def centered_average(nums):
largest = reduce(lambda x, y: x if x > y else y, nums)
smallest = reduce(lambda x, y: x if x < y else y, nums)
nums.remove(largest)
nums.remove(smallest)
return sum(nums) / len(nums)
centered_average([1,2,3,6,100])
Out[15]:
In [38]:
# http://codingbat.com/prob/p167025
# Return the sum of the numbers in the array,
# returning 0 for an empty array. Except the
# number 13 is very unlucky, so it does not
# count and numbers that come immediately
# after a 13 also do not count.
def sum13(nums):
while 13 in nums:
index = nums.index(13)
pre = nums[:index]
# the tricky bit: you need to remove the value immediately
# following the 13 too. while making sure that you don't go
# out of bounds
post = nums[index+2:] if len(nums) > index + 2 else []
nums = pre + post
if len(nums) < 1:
return 0
return reduce(lambda x, y: x+y, nums)
sum13([5, 13, 2, 4])
Out[38]:
In [44]:
# http://codingbat.com/prob/p108886
# Return the sum of the numbers in the array,
# except ignore sections of numbers starting
# with a 6 and extending to the next 7 (every
# 6 will be followed by at least one 7). Return
# 0 for no numbers.
def sum67(nums):
while 6 in nums:
index_six = nums.index(6)
index_seven = nums.index(7)
pre = nums[:index_six]
post = nums[index_seven+1:] if len(nums) > index_seven+1 else []
nums = pre + post
if len(nums) < 1:
return 0
return reduce(lambda x,y: x+y, nums)
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7])
Out[44]:
In [63]:
# http://codingbat.com/prob/p174314
def end_other(a, b):
lower_a = a.lower()
lower_b = b.lower()
a_in_b = lower_a in lower_b
b_in_a = lower_b in lower_a
a_in_b_at_end = True if lower_b[-len(a):] == lower_a else False
b_in_a_at_end = True if lower_a[-len(b):] == lower_b else False
return a_in_b_at_end or b_in_a_at_end
# print end_other('ab', 'ab12')
# print end_other('ab', '12ab')
print end_other('AbC', 'HiABC')
In [93]:
# http://codingbat.com/prob/p149391
def xyz_there(str):
if 'xyz' in str:
idx = str.find('xyz')
exists = None
if idx+4 < len(str):
remaining = str[idx+3:]
exists = xyz_there(remaining)
if idx-1 < 0:
return True
elif str[idx-1] == '.':
return False or (exists if exists != None else False)
else:
return True
return False
print xyz_there('abc.xyzxba')
print xyz_there('abc.xyzxyz')
print xyz_there('.xyz')
In [94]:
print 'hello'.__class__
In [95]:
print type('hello')
In [103]:
In [ ]:
In [ ]: