# %% 1
a = list(range(1, 101))
# 100 (-> 98) -> 99
while len(a) > 9:
a.append(a[0]+a[1])
# a.pop(0)
# a.pop(0)
a[:2] = []
print(a)
# %% 2
def op(a, b):
return a - b if a >= b else a + b
b = 200
for a in range(198, 1, -2):
b = op(a, b)
print(b)
# %% 3
# ignore the case of x < 0, e.g. x=-1 when first die_no is 2
from random import randint
def get_die_no():
return randint(1, 6)
x = 1
count = 0
while x < 100:
x += get_die_no()
count += 1
if x >= 100:
break
if (x%3 == 0) and (x%7 == 4):
x -= 1
elif x%3 == 0:
x -= 4
elif x%7 == 4:
x += 3
print(count)
# %% 4
def fetch_input():
"""get input h, n, ls for height, number of ladders, and list of ladder height, all positive integers"""
h = int(input())
if h <= 0:
raise ValueError('Input need to be a positive integer!')
n = int(input())
if n <= 0:
raise ValueError('Input need to be a positive integer!')
ls = []
for i in range(n):
ls.append(int(input()))
return h, ls
def get_min_ladders(h, ls):
if sum(ls) < h:
# including edge case of empty list
return "No"
ls2 = sorted(ls, reverse=True) # possible to do faster bucket sort due to integer, but naive first..
cur_sum = 0 # use the naive way, possible lambda filter etc
for i, hi in enumerate(ls2):
cur_sum += hi
if cur_sum >= h:
return i + 1
h, ls = fetch_input()
print(get_min_ladders(h, ls))
# %% 5
def get_dollar_amount():
m = int(input())
if m <= 0:
raise ValueError('Input need to be a positive integer!')
return m
def get_drink_amount(m, price=3, bottle_ratio=2, cap_ratio=5):
full_bottles = m // price # no of bottle of drinks in hand
ret = 0 # no of bottle of drinks finished so far
empty_bottle = 0 # current empty bottle no
cap = 0 # current cap #
while full_bottles > 0:
# process of finishing current bottle of drinks, and exchange for new ones
# finish them and get empty bootle and cap
ret += full_bottles
empty_bottle += full_bottles
cap += full_bottles
# full_bottles = 0
# exchange for new bottle
full_bottles = empty_bottle // bottle_ratio + cap // cap_ratio
empty_bottle = empty_bottle % bottle_ratio
cap = cap % cap_ratio
return ret
print(get_drink_amount(get_dollar_amount()))
# %% 6
n = int(input())
if (n < 1) or (n > 5):
raise ValueError('Input need to be a positive integer in [1, 5]!')
cur = n
ret = 0
# find next from current
while cur != 1:
# use legacy elif, note new python has switch statement
if cur == 2:
cur = 4
elif cur == 3:
cur = 5
elif cur == 4:
cur = 1
elif cur == 5:
cur = 1
ret += 1
print(ret)
# %% 7
def get_segments():
n = int(input())
if n <= 1:
raise ValueError('Input need to be a positive integer larger than 1')
ls = []
for _ in range(n):
i = int(input())
if i <= 0:
raise ValueError('Input need to be a positive integer')
ls.append(i)
return ls
def get_min_ave_len(ls):
n = len(ls) # n>=2
# recurssion vs
# dynamic programming: loop with method of add one element... do this
# def get_cache(ls, k, min_ave_len, count, min_ave_len_last, count_last):
# maintain & return
# min_ave_len, count, the global result, and
# min_ave_len_last, count_last, the result must include the last element
# No need, we try all end with current anyway, no shortcut
min_ave_len = (ls[0] + ls[1])/2
for k in range(2, n):
# find the min_ave_len_new that in ls[0, ..., k], given min_ave_len in ls[0, ..., k-1]
# two cases min_ave_len_new includes ls[k], or not includes ls[k]
# compute the one include ls[k]
cur_sum = ls[k] + ls[k-1]
count = 2
min_ave_len_include_k = cur_sum/2
for a in ls[k-2::-1]:
cur_sum += a # may cache those for faster comput of cur_sum, but doesn't matter too much due to the /
count += 1
cur_ave = cur_sum / count
if cur_ave < min_ave_len_include_k:
min_ave_len_include_k = cur_ave
min_ave_len = min(min_ave_len, min_ave_len_include_k)
return min_ave_len
ls = get_segments()
min_ave_len = get_min_ave_len(ls)
print(f"{min_ave_len:.1f}")
# %% 8
ls = [18, 11, 22, 17, 12, 29, 28, 26, 20, 21]
ls = sorted(ls)
# (i, j, k), (a, b, c) = (ls[i], ls[j], ls[k])
# s = a + b + c, where s <= 60, but as close as 60,
# return
# oil = s + 24 - 60 # we know it is 24 by eye
# no. of ways of (a, b, c)
# brute force O(n^3) with
n = len(ls)
cur_best = - 1
ret_list = []
ways = 0
for i in range(n-2):
for j in range(i+1, n-1):
for k in range(j+1, n):
s = ls[i] + ls[j] + ls[k]
if s > 60:
# no need to try k+1... because that will be larger
break
if s > cur_best:
# new record
cur_best = s
ways = 1
ret_list = [[ls[i], ls[j], ls[k]]]
elif s == cur_best:
# same record, but increase one way
ways += 1
ret_list.append([ls[i], ls[j], ls[k]])
print(cur_best + 24 - 60)
print(ways)
print(ret_list)
# %% 9
# if first three of two numbers are different, then they are different
# same as asking how many choices of first three?
# must start with 1
print(9*10*10)
# %% 10. approaching time limit, bruteforce, no exception handling
m = int(input())
n = int(input())
ls = []
for i in range(n):
b = int(input())
e = int(input())
ls.append([b, e])
# 12 to 24
ls_count = []
for k in range(12, 25):
count = 0
for a in ls:
if a[0] <= k <= a[1]:
count += 1
ls_count.append(count)
if min(ls_count) >= m:
print("Yes")
print(max(ls_count))
else:
print("No")
print(min(ls_count))
# %% 11. close to 2 hours, bruteforce, no exception handling
x = int(input())
y = int(input())
time = 60*6
max_n = 0
choices = None
for a1 in range(0, time // 30 + 1):
for b1 in range(0, time // x + 1):
a2 = (time - a1 * 30) // 10
b2 = (time - b1 * x) // y
max_n = max(max_n, min(a1+b1, a2+b2))
# pairs = min(a1+b1, a2+b2)
# if pairs >= max_n:
# max_n = pairs
# choices = (a1, a2, b1, b2)
print(max_n)
# print(choices)
--
FROM 107.77.173.*