2021 年
By 20-软工-高歌
def func1(n): result = 0 for i in range(n): if not (i % 9 == 0 or '9' in str(i)): result += i * i return resultdef func1(n): return sum(i * i for i in range(n) if not (i % 9 == 0 or '9' in str(i)))def func2(lst): if len(lst) == 0: return None count = {} for num in lst: count[num] = count.get(num, 0) + 1 return max(count, key=count.get)from collections import Counter def func2(lst): if len(lst) == 0: return None return Counter(lst).most_common(1)[0][0]def func3(lst): def go(lst): if len(lst) == 0: return [[]] elif len(lst) == 1: return [lst] else: head, tail = lst[0], lst[1:] result = [] for l in go(tail): # 对每个子列表分别插值 for i in range(len(l) + 1): result.append(l[:i] + [head] + l[i:]) return result return sorted(go(lst)) # 最后排序,减少时间开销def func3(lst): def backtrack(first=0): # 所有数都填完了 if first == n: result.append(nums[:]) for i in range(first, n): # 动态维护数组 nums[first], nums[i] = nums[i], nums[first] # 继续递归填下一个数 backtrack(first + 1) # 撤销操作 nums[first], nums[i] = nums[i], nums[first] n = len(nums) result = [] backtrack() return sorted(result)from itertools import permutations def func3(lst): return sorted(map(list, permutations(lst)))def permutations(iterable, r=None): # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC # permutations(range(3)) --> 012 021 102 120 201 210 pool = tuple(iterable) n = len(pool) r = n if r is None else r if r > n: return indices = list(range(n)) cycles = list(range(n, n-r, -1)) yield tuple(pool[i] for i in indices[:r]) while n: for i in reversed(range(r)): cycles[i] -= 1 if cycles[i] == 0: indices[i:] = indices[i+1:] + indices[i:i+1] cycles[i] = n - i else: j = cycles[i] indices[i], indices[-j] = indices[-j], indices[i] yield tuple(pool[i] for i in indices[:r]) break else: returndef func4(string): words = string.split(' ') words.sort(key=lambda x: (-len(x), x.lower(), [ord(i) for i in x])) # 事实上Python本身对纯英文字母就是按照ASCII码排序的, # 因此这里可以简单写成key=lambda x: (-len(x), x.lower(), x) return ' '.join(words)def func4(string): words = string.split(' ') length = len(words) for i in range(length - 1): for j in range(length - 1 - i): a, b = words[j], words[j+1] if len(a) != len(b): if len(a) < len(b): words[j], words[j+1] = b, a elif words[j].lower() != words[j+1].lower(): if words[j].lower() > words[j+1].lower(): words[j], words[j+1] = b, a elif [ord(x) for x in a] != [ord(x) for x in b]: if [ord(x) for x in a] > [ord(x) for x in b]: # 事实上Python本身对纯英文字母就是按照ASCII码排序的, # 因此这里可以简单写成a != b和a > b words[j], words[j+1] = b, a return ' '.join(words)from functools import cmp_to_key def func4(string): def sortFn(a, b): if len(a) != len(b): if len(a) > len(b): return -1 elif len(a) < len(b): return 1 elif a.lower() != b.lower(): if a.lower() < b.lower(): return -1 elif a.lower() > b.lower(): return 1 else: # 按ASCII码排序 if [ord(i) for i in a] < [ord(j) for j in b]: return -1 elif [ord(i) for i in a] > [ord(j) for j in b]: return 1 return 0 words = string.split(' ') words.sort(key=cmp_to_key(sortFn)) return ' '.join(words)def func5(lst): score = {} for l in lst: score[l[0]] = score.get(l[0], 0) + l[2] return max(score.items(), key=lambda item: item[1])from itertools import groupby def func5(lst): return max({key: sum(g[2] for g in group) for key, group in groupby(sorted(lst), key=lambda x: x[0])} .items(), key=lambda item: item[1])def func6(n): def go(n): if n == 1: # 1阶只有一种爬法 return 1 if n == 2: # 2阶有爬两次1阶和爬一次2阶两种爬法 return 2 else: # 其他阶数为爬n-1阶再爬1阶和爬n-2阶再爬2阶的爬法之和 return go(n-1) + go(n-2) return go(n)def func6(n): cache = {} def go(n): if n == 1: # 1阶只有一种爬法 return 1 if n == 2: # 2阶有爬两次1阶和爬一次2阶两种爬法 return 2 else: # 其他阶数为爬n-1阶再爬1阶和爬n-2阶再爬2阶的爬法之和 if n in cache: return cache[n] else: cache[n] = go(n-1) + go(n-2) return cache[n] return go(n)>>> from timeit import timeit # 旧函数 >>> timeit(lambda: func6(30), number=100) 0.15679146766662597 # 新函数 >>> timeit(lambda: func6(30), number=100) 2.0000934600830078e-05from functools import lru_cache def func6(n): @lru_cache def go(n): if n == 1: return 1 if n == 2: return 2 else: return go(n-1) + go(n-2) return go(n)from functools import lru_cache def func6(n): def go(n): if n == 1: return 1 if n == 2: return 2 else: return go(n-1) + go(n-2) go = lru_cache(go) return go(n)def func6(n): def cache_it(fn): cache = {} def new_fn(*args): if args in cache: return cache[args] else: cache[args] = fn(*args) return cache[args] return new_fn @cache_it # 你当然也可以不使用装饰器,改在函数定义后补上一句go = cache_it(go) def go(n): if n == 1: return 1 if n == 2: return 2 else: return go(n-1) + go(n-2) return go(n)def func7(lst): result = [] for t2 in lst: is_dominated = False for t1 in lst: if t1 == t2: continue if t1[0] <= t2[0] and t1[1] <= t2[1]: is_dominated = True break if not is_dominated: result.append(t2) return sorted(result)def func8(lst): if len(lst) < 4: return None # 先排序 lst.sort() # 依次判断两两数字之差 diff = lst[1] - lst[0] for i in range(1, len(lst) - 1): d = lst[i+1] - lst[i] if d > diff: return lst[i] + diff elif d < diff: return lst[i-1] + ddef func8(lst): return (lambda l: (lambda diffs: l[diffs.index(max(diffs))] + min(diffs))( [j - i for i, j in zip(l[:-1], l[1:])]) )(sorted(lst))
总结
最后更新于