Skip to content

Instantly share code, notes, and snippets.

@mizushou
Last active October 31, 2019 05:33
Show Gist options
  • Save mizushou/f56fa7d1da91b3ae4d9f3dc787f508ae to your computer and use it in GitHub Desktop.
Save mizushou/f56fa7d1da91b3ae4d9f3dc787f508ae to your computer and use it in GitHub Desktop.
A sample answer of Coding Bat
"""
CodingBat: Warmup-2
array_front9
"""
# I think it's okay in Python.
def array_front9(nums):
return 9 in nums[:4]
# Recursive case.
def array_front9(nums):
# base case: length = 0
if len(nums) == 0:
return False
# base case: length = 1
if len(nums) == 1:
return nums[0] == 9
# base case: 2 <= length <= 4
if len(nums) <=4 and nums[-1] == 9:
return True
# recursive case: 5 <= length
else:
return array_front9(nums[:-1])
# Loop case. This is a sample answer.
def array_front9(nums):
# First figure the end for the loop
end = len(nums)
if end > 4:
end = 4
for i in range(end): # loop over index [0, 1, 2, 3]
if nums[i] == 9:
return True
return False
"""
CodingBat: Warmup-1
diff21
"""
# Not need abs function. It's obvious n is less than 21
def diff21(n):
if n < 21:
return abs(n - 21)
else:
return abs(n - 21) *2
"""
CodingBat: Warmup-1
front3
"""
def front3(str):
# base case
if len(str) <= 3:return 3*str
# recursive case
# strのlenが3より長い場合は後ろかあら1文字削ってfront3を呼ぶ
return front3(str[:-1])
"""
CodingBat: Warmup-1
front_back
"""
def front_back(str):
if str == "":
return str
front = str[0]
back = str[-1]
ans = ''
for i in range(len(str)):
if i == 0:
ans += back
elif i == len(str)-1:
ans += front
else:
ans += str[i]
return ans
"""
Error answer
def front_back(str):
if str == "":
return str
front = str[0]
back = str[-1]
ans = ''
for i in range(len(str)):
if i == 0:
ans += back
if i == len(str)-1:
ans += front
# こうすると、一個上のifとこのelseでif-elseの構造になってしまう。なので2個目のifがfalseの場合に常にelseでansに文字が追加されてしまう。
else:
ans += str[i]
return ans
"""
"""
CodingBat: Warmup-2
front_times
"""
# Slice
def front_times(str, n):
return str[:3] * n
# Recursive
def front_times(str, n):
# base case
if len(str) <= 3:
return str * n
# recursive case
else:
return front_times(str[:-1], n)
# Loop case. This is a sample answer.
def front_times(str, n):
front_len = 3
if front_len > len(str):
front_len = len(str)
front = str[:front_len]
result = ""
for i in range(n):
result = result + front
return result
"""
CodingBat: Warmup-2
last2
"""
# Get the last 2 charcters of a sring
# https://stackoverflow.com/questions/7983820/get-the-last-4-characters-of-a-string
def last2(str):
sub = str[-2:]
cnt = 0
for i in range(len(str)-2):
if (sub == str[i:i+2]):
cnt += 1
return cnt
"""
CodingBat: Warmup-1
makes10
"""
def makes10(a, b):
if (a == 10 or b ==10) or (a + b == 10):
return True
else:
return False
"""
CodingBat: Warmup-1
missing_char
"""
# 指定した箇所の文字をremoveして詰める処理はslice + concatでOK
def missing_char(str, n):
return str[:n] + str[n+1:]
"""
CodingBat: Warmup-1
monkey_trouble
"""
def monkey_trouble(a_smile, b_smile):
return a_smile == b_smile
"""
CodingBat: Warmup-1
near_hundred
"""
def near_hundred(n):
return abs(n - 100) <= 10 or abs(n - 200) <= 10
"""
CodingBat: Warmup-1
not_string
"""
def not_string(str):
# sliceはout of indexが起きないので、strの長さが3文字より短くても大丈夫
if str[:3] == 'not':
return str
else:
return "not " + str
"""
CodingBat: Warmup-1
parrot_trouble
"""
def parrot_trouble(talking, hour):
return not (7 <= hour <= 20) and talking
"""
CodingBat: Warmup-1
pos_neg
"""
def pos_neg(a, b, negative):
if negative:
# AND
return (a < 0 and b < 0)
else:
# XOR
# https://en.wikipedia.org/wiki/Exclusive_or
return (not (a < 0) and (b < 0)) or ((a < 0) and not (b < 0))
"""
CodingBat: Warmup-1
sleep_in
"""
def sleep_in(weekday, vacation):
# Don't forget not oprator
return not weekday or vacation
"""
CodingBat: Warmup-2
string_bits
"""
def string_bits(str):
# base case
if(len(str)==1 or len(str)==0):
return str
# recursive case
if(len(str)%2==0):
return string_bits(str[:-1])
else:
return string_bits(str[:-1]) + str[-1]
"""
CodingBat: Warmup-2
string_splosion
"""
# これもrecursionの典型例
# 文字列生成などはrecursionが相性がいい気がする
def string_splosion(str):
if len(str) == 1:
return str
else:
return string_splosion(str[:-1]) + str
"""
# loopを使う場合
# この問題はrecursionの方が若干イメージしやすい気がする
def string_splosion(str):
result = ""
# On each iteration, add the substring of the chars 0..i
for i in range(len(str)):
result = result + str[:i+1]
return result
"""
"""
CodingBat: Warmup-2
string_times
"""
# solution1
# recursionの典型的な型
def string_times(str, n):
if n == 0:
return ''
elif n == 1:
return str
else:
return str + string_times(str, n-1)
# solution2
# Pythonであれば基本はこれでいいと思う
def string_times(str, n):
return str * n
# solution1は以下のloopをrecursionに置き換えたもの
"""
def string_times(str, n):
result = ""
for i in range(n): # range(n) is [0, 1, 2, .... n-1]
result = result + str # could use += here
return result
"""
"""
CodingBat: Warmup-1
sum_double
"""
def sum_double(a, b):
# ternary conditional operator
# {True value} if {condition} else {False value}
#
# https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator
return (a+b)*2 if a == b else a+b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment