python2.7:"Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0."
python3.5:"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice."
defabs(self, num): if num >= 0: return num else: return self.add(~num, 1)
defmultiply(self, num1, num2): abs1 = self.abs(num1) abs2 = self.abs(num2) ans = 0 while abs2 != 0: if abs2 & 1: ans = self.add(ans, abs1) abs2 = abs2 >> 1 abs1 = abs1 << 1 if self.is_negative(num1, num2): return self.add(~ans, 1) return ans
defdivide(self, num1, num2): # exception if num2 == 0: raise Exception("Divisor is zero.", num2)
abs1 = self.abs(num1) abs2 = self.abs(num2)
ans = 0 i = 31 while i >= 0: if (abs1 >> i) >= abs2: ans = self.add(ans, 1 << i) abs1 = self.subtract(abs1, abs2 << i) i = self.subtract(i, 1) if self.is_negative(num1, num2): return self.add(~ans, 1) return ans
列表的空白初始化
这是一个 copy 和 deep copy 问题。
1 2
# 解析语法初始化 lis = [[0for i in range(n)] for j in range(m)]
deffactorsXX(n): '''yield返回n的全部因子(计算减半,因子无序)''' k = 1 while k * k < n: if n % k == 0: yield k yield n // k k += 1 if k * k == n: yield k
deffactorsS(n): '''yield返回n的全部因子(计算减半,因子有序)''' k = 1 temp = [] while k * k < n: if n % k == 0: yield k temp.append(n // k) k += 1 if k * k == n: yield k for i in temp[::-1]: yield i
查找列表中相同元素出现的位置
1、破坏列表法
1 2 3 4 5 6 7 8 9 10 11 12
deffunction_s(l, x, n): # 获取第一个x的下标 index_one = l.index(x) # 删除第一个出现的"a"元素 l.pop(index_one) i = 1 for i in range(n-1): index_temp = l.index(x) i += index_temp l.pop(index_temp) return i # 这种方法必须保证列表内至少有n个元素
2、for循环法
1 2 3 4 5 6 7 8 9 10 11
deffunction_s(l, x): # 定义变量, 记录x出现次数 m = 0 # 定义变量, 记录循环到的列表下标 n = 0 for i in l: if i == x: print(i) n += 1 m += 1 return m
# 定义通用的获取某元素在列表中第n次出现的位置下标的函数 defget_index(l, x, n): # 函数作用: 获取某个元素第n次出现在列表的下标 # 参数列表: 第一个参数为可迭代对象, 第二个参数为要查找的数, 第三个参数为要查找第几个出现的x l_count = l.count(x) result = None if n <= l_count: num = 0 # enumerate is useful for obtaining an indexed list: # (0, seq[0]), (1, seq[1]), (2, seq[2]), ... for item in enumerate(l): if item[1] == x: num += 1 if num == n: result = item[0] break else: print("列表里总共有{}个{}".format(l_count, x)) return result
defflat(iters): res = [] for item in iters: # 如果是列表或者元组,则递归调用 if isinstance(item, list) or isinstance(item, tuple): res.extend(flat(item)) # 如果是其他类型,则直接加入列表 else: res.append(item) return res
素数算法
欧拉筛法
1 2 3 4 5 6 7 8 9 10 11 12 13
defOlaPrimeCalc(n): Prime = [] Compare = [1] * (n + 1) for x in range(2, n + 1): if Compare[x] == 1: Prime.append(x) for p in Prime: if x * p > n: break Compare[x * p] = 0 if x % p == 0: break return Prime
埃氏筛法
1 2 3 4 5 6 7 8 9
defeladuosai(n): l = list(range(1,n+1)) l[0] = 0 for i in range(2,n+1): if l[i-1] != 0 : for j in range(i*2,n+1,i): l[j-1] = 0 result = [x for x in l if x != 0] return set(result)
import sys sys.setrecursionlimit(5000000) # set the maximum depth as 5000000 global Answer times = 0 Answer = [0] * 10000 defCalc(x,k): '''从1开始分解x,第一位储存在第k位,将分解的情况放入列表''' for num in range(1, x+1): if num >= Answer[k-1]: Answer[k] = num rest = x - num if rest == 0: global times times += 1 else: Calc(rest, k + 1)
母函数法
1
五边形数定理法
1
文本读取的编码问题
使用 utf-8 读取 Windows 的 .txt 文本文件,读取的第一个字符串开头出现 \ufeff。
例如:
1 2
待读取的文本内容 文本第二行
1 2 3 4 5
with open(file_path, 'r', encoding = "utf-8") as fo: text_list = fo.readlines()
>>> text_list >>> ['\ufeff待读取的文本内容', '文本第二行']
原因:Windows 保存时采用了 UTF-8 with BOM 编码,并不是标准的 UTF-8。因此文本保存时包含了BOM(Byte Order Mark,字节顺序标记,出现在文本文件头部,Unicode 编码标准中用于标识文件是采用哪种格式的编码)