Write a Python program to find the factorial of a number.
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
Example:
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。
了解热门 Python Coding 面试题与答案,帮助应届生和有经验的候选人为求职面试做好准备。
了解热门 Python Coding 面试题与答案,帮助应届生和有经验的候选人为求职面试做好准备。
搜索问题以查看答案。
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
def longest_increasing_subsequence(arr):
n = len(arr)\n lis = [1]*n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
return max(lis)
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
def power_set(input_set):
n = len(input_set)
for i in range(1 << n):
subset = [input_set[j]
for j in range(n):
if (i & (1 << j)) > 0]
yield subset
Example:
收藏此条目、标记为困难题,或将其加入复习集合。
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Example:
收藏此条目、标记为困难题,或将其加入复习集合。