1.求所有的长度为K的组合(不重复)
例:[1,2,3,4,5]
所有组合:[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
a = [1,2,3,4,5] def search(results, arr, k, path): if (k == 0): results.append(path) return for i in range(len(arr)): search(results, arr[i + 1: ], k - 1, path + [arr[i]]) return results result = search([], a, 3, []) print(result)
2.判断是否素数
def isPrime(num): for i in range(2, num): if num%i == 0: return False return True result = isPrime(7) print(result)