Created
October 22, 2018 07:10
-
-
Save tkfm-yamaguchi/36f7b3c33f39ba4722db8c355a28f375 to your computer and use it in GitHub Desktop.
CodingBat in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://codingbat.com/python/List-1 | |
# https://codingbat.com/python/Logic-1 | |
# https://codingbat.com/python/Logic-2 | |
# https://codingbat.com/python/String-2 | |
# https://codingbat.com/python/List-2 | |
def cigar_party(cigars, is_weekend): | |
if is_weekend: | |
return cigars >= 40 | |
else: | |
return 40 <= cigars and cigars <= 60 | |
def date_fashion(you, date): | |
if you <= 2 or date <= 2: | |
return 0 # no | |
if you >= 8 or date >= 8: | |
return 2 # yes | |
return 1 # maybe | |
def squirrel_play(temp, is_summer): | |
upper_lim = 90 | |
if is_summer: | |
upper_lim = 100 | |
return 60 <= temp and temp <= upper_lim | |
def caught_speeding(speed, is_birthday): | |
s_border = 60 | |
b_border = 80 | |
if is_birthday: | |
s_border += 5 | |
b_border += 5 | |
if speed <= s_border: | |
return 0 | |
if speed <= b_border: | |
return 1 | |
return 2 | |
def sorta_sum(a, b): | |
s = a + b | |
if 10 <= s and s <= 19: | |
return 20 | |
return s | |
def alarm_clock(day, vacation): | |
is_weekend = day == 0 or day == 6 | |
if vacation: | |
if is_weekend: | |
return 'off' | |
else: | |
return '10:00' | |
else: | |
if is_weekend: | |
return '10:00' | |
else: | |
return '7:00' | |
def love6(a, b): | |
return a == 6 \ | |
or b == 6 \ | |
or a + b == 6 \ | |
or abs(a - b) == 6 | |
def in1to10(n, outside_mode): | |
if outside_mode: | |
return n <= 1 or 10 <= n | |
else: | |
return 1 <= n and n <= 10 | |
def near_ten(num): | |
modulo = num % 10 | |
return modulo <= 2 or modulo >= 8 | |
def make_bricks(small, big, goal): | |
for n_big in range(big+1): | |
residual = goal - 5 * n_big | |
if residual < 0: | |
break | |
if residual <= small: | |
return True | |
return False | |
def lone_sum(a, b, c): | |
if a == b and b == c: | |
return 0 | |
if a == b: | |
return c | |
if a == c: | |
return b | |
if b == c: | |
return a | |
return a + b + c | |
def lucky_sum(a, b, c): | |
nums = [a, b, c] | |
s = 0 | |
for n in nums: | |
if n == 13: | |
break | |
s += n | |
return s | |
def no_teen_sum(*nums): | |
def fix_teen(n): | |
if n == 15 or n == 16: | |
return n | |
if 13 <= n and n <= 19: | |
return 0 | |
return n | |
return reduce(lambda x,y:x + fix_teen(y), nums, 0) | |
def round_sum(*nums): | |
def round10(num): | |
modulo = num % 10 | |
if modulo < 5: | |
return num - modulo | |
return num + (10 - modulo) | |
return reduce(lambda x,y: x+round10(y), nums, 0) | |
def close_far(a, b, c): | |
ab_far = abs(a - b) >= 2 | |
ac_far = abs(a - c) >= 2 | |
bc_far = abs(b - c) >= 2 | |
return (ab_far ^ ac_far) and bc_far | |
def make_chocolate(small, big, goal): | |
for n_big in reversed(range(big+1)): | |
residual = goal - 5 * n_big | |
if residual < 0: | |
continue | |
if residual > small: | |
break | |
return residual | |
return -1 | |
def double_char(s): | |
return reduce(lambda x, y: x + y + y, s, '') | |
def count_hi(s): | |
count = 0 | |
for i in range(len(s) - 1): | |
if s[i:i+2] == 'hi': | |
count += 1 | |
return count | |
def cat_dog(s): | |
c_cat = 0 | |
c_dog = 0 | |
for i in range(len(s)-2): | |
if s[i:i+3] == 'cat': c_cat += 1 | |
if s[i:i+3] == 'dog': c_dog += 1 | |
return c_cat == c_dog | |
def count_code(s): | |
count = 0 | |
for i in range(len(s) - 3): | |
target = s[i:i+4] | |
if target[0:2] == 'co' and target[3] == 'e': | |
count += 1 | |
return count | |
def end_other(a, b): | |
la = a.lower() | |
lb = b.lower() | |
if len(la) > len(lb): | |
l = la | |
s = lb | |
else: | |
l = lb | |
s = la | |
return l[-len(s):] == s | |
def xyz_there(s): | |
for i in range(len(s)-2): | |
if not s[i:i+3] == 'xyz': | |
continue | |
if i == 0 or s[i-1] != '.': | |
return True | |
return False | |
def count_evens(nums): | |
count = 0 | |
for n in nums: | |
if n % 2 == 0: | |
count += 1 | |
return count | |
def big_diff(nums): | |
return max(nums) - min(nums) | |
def centered_average(nums): | |
return (sum(nums) - max(nums) - min(nums)) / (len(nums) - 2) | |
def sum13(nums): | |
if len(nums) == 0: | |
return 0 | |
i = -1 | |
s = 0 | |
while True: | |
i += 1 | |
if i >= len(nums): | |
break | |
if nums[i] == 13: | |
i += 1 | |
continue | |
s += nums[i] | |
return s | |
def sum67(nums): | |
i = -1 | |
s = 0 | |
in67 = False | |
while True: | |
i += 1 | |
if i >= len(nums): | |
break | |
if nums[i] == 6: | |
in67 = True | |
continue | |
if in67 and nums[i] == 7: | |
in67 = False | |
continue | |
if in67: | |
continue | |
s += nums[i] | |
return s | |
def has22(nums): | |
for i in range(len(nums)-1): | |
[prev, post] = nums[i:i+2] | |
if prev == 2 and prev == post: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment