def get_year_gap(A_datas, B_datas): gdp_total_a, gdp_ratio_a, pop_total_a, pop_ratio_a = A_datas gdp_total_b, gdp_ratio_b, pop_total_b, pop_ratio_b = B_datas gdp_avg_a = gdp_total_a / pop_total_a gdp_avg_b = gdp_total_b/ pop_total_b year = 0 while gdp_avg_b < gdp_avg_a: year += 1 gdp_total_a *= (1 + gdp_ratio_a) gdp_total_b *= (1 + gdp_ratio_b) pop_total_a *= (1 + pop_ratio_a) pop_total_b *= (1 + pop_ratio_b) gdp_avg_a = gdp_total_a / pop_total_a gdp_avg_b = gdp_total_b / pop_total_b return year print(get_year_gap([21.43, 0.023, 3.28, 0.005], [14.36, 0.061, 14.0, 0.0033]))
2. 为leetcode
的简化版本
def getSeq(num: int, n): start, tmp = 1, 0 for r in range(num//2+2): tmp += r while tmp > num: tmp -= start start += 1 if num == tmp: if r + 1 - start == n: return list(range(start, r + 1)) tmp -= start start += 1 print(getSeq(18, 4))
3.
# 利用排序辅助函数 from functools import cmp_to_key def sort_nums(x, y): if x[1] == y[1]: return 1 if len(x[0]) > len(y[0]) else -1 elif x[1] > y[1]: return 1 else: return -1 def specify_sort(words): dic = {} for word in words: num_count, char_count = 0, 0 for s in word: if s.isalpha(): char_count += 1 if s.isdigit(): num_count += 1 dic[word] = char_count / num_count result = sorted(dic.items(), key = cmp_to_key(sort_nums), reverse = True) return [i[0] for i in result] print(specify_sort(['abc123', 'abc+1234',"ababab--1", 'abcd1234']))
全部评论
(7) 回帖