下文是我面试过程中遇到的算法题,可以给大家做个参考,如果有其他常见算法题可以评论下来,我会及时补充更新.
class Test:
arr = [7, 11, 2, 13, 4, 32, 12, 1, 3, 5, 66, 37, 8, 6]
s = '1213asdasd1123ssaaa'
def case1(self):
# 冒泡排序
pass
def case2(self):
# 二分之一查找
pass
def case3(self):
# 找出列表里那两个值相加=k,打印下标
pass
def case4(self):
# 判断是否是回文
pass
def case5(self):
# 找出字符串中重复的字符串,并记录重复的次数
pass
def case6(self):
# 找出字符串中重复的字符串,并记录重复的次数
pass
def case7(self):
# 随机去掉列表里任意一个数字,打印出去掉了几个,去掉的是什么
pass
def case8(self):
# 取出任意一个数,拼接上5,把5拼接到那个位置得到的数最大
pass
def case9(self):
# 两个列表下标的值相加,生成一个新的列表,下标不足的保留原来的值
pass
def case10(self):
# 替换字符串里的任意字符为指定的值
pass
def case11(self):
# 替换列表里的值
pass
def case12(self):
# 微信红包
import random
pass
def case13(self):
# 九九乘法表
pass
def case14(self):
# 打印出1234中互不相同切无重复的三位数
import itertools
pass
def case15(self):
# 给你一个字符串 `date` ,按 `YYYY-MM-DD` ,返回该日期是当年的第几天。
pass
if __name__ == '__main__':
t = Test()
t.case11()
冒泡排序
def case1():
arr = [11, 2, 13, 32, 12, 1, 5, 66, 37, 8]
for i in range(len(arr)):
for j in range(len(arr)-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print(arr)
二分之一查找
def case2():
arr = [1,2,3,5,8,11,12,17,22,25,30] # 生成列表
j = 30
low = 0
high = len(arr) -1
while low <= high:
mid = (low + high) // 2
if arr[mid] == j:
print(mid)
return
elif arr[mid] > j:
high = mid -1
else:
low = mid +1
print('没有找到')
找出列表里那两个值相加=k,打印下标
def case3():
arr = [1, 7, 5, 22, 13, 8, 4, 6, 2, 3, 8, 10, 0]
k = 7
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if arr[i] + arr[j] == k:
print(i,j)
判断是否是回文
def case4(x):
if str(x)[::-1] == str(x):
print('yes')
else:
print('no')
找出字符串中重复的字符串,并记录重复的次数
def case5():
s = "adaaaadd11111"
ss = list(set(s)) # 获取一个无序的集合,再转成列表
ss.sort(key=s.index) # 给列表排序
for i in ss:
k = s.count(i) # 找到重复的记录重复次数
print(i,k)
找出字符串中重复的字符串,并记录重复的次数
def case6():
# 字符串去重
s = "adaaaadd11111"
s1 = ''
for i in range(len(s)):
if s[i] not in s1:
s1 += s[i]
# 拿到去重的字符串计算出现的次数
for i in s1:
k = s.count(i)
print(i,k)
随机去掉列表里任意一个数字,打印出去掉了几个,去掉的是什么
def case7():
arr1 = [99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# arr2 = lambda : [x for x in arr1 if x % 2 == 0] # 去掉无法被2整除的数
arr2 = lambda : [x for x in arr1 if x > 10] # 去掉小于10的数
arr3 = []
for i in arr1:
if i not in arr2():
arr3.append(i)
print('去掉了{}个.去掉的是{}'.format(len(arr3), arr3))
取出任意一个数,拼接上5,把5拼接到那个位置得到的数最大
def case8():
i = -1516
key = list(str(abs(i)))
list2 = []
for i in range(len(key)+1):
list1 = key.copy()
list1.insert(i, '5')
s = ''.join(list1)
list2.append(int(s))
if i < 0:
print(max(list2))
else:
print(-min(list2))
两个列表下标的值相加,生成一个新的列表,下标不足的保留原来的值
def case9():
import numpy
list1 = [1, 3, 5, 7, 11, 13, 11, 1, 1, 2]
list2 = [2, 4, 6, 8, 10]
key = len(list1) - len(list2)
list2 = numpy.pad(list2, (0, key))
s = list(map(lambda x, y:x + y, list1, list2))
print(s)
替换字符串里的任意字符为指定的值
def case10():
str1 = 'aaaccnbabc'
print(str1.replace('a', '*'))
替换列表里的值
def case11():
list1 = ['a', 'a', 'a', 'c', 'c', 'n', 'b', 'a', 'b', 'c']
while 'a' in list1:
i = list1.index('a')
list1[i] = '*'
print(list1)
微信红包
import random
def case12(money, nums=10):
money *= 100
list1 = []
for i in range(nums-1, -1, -1):
key = money-(i+1) # 保证后面最少能拿到0.01
ren = random.randint(1, key) # 随机整数,避免小数因精度问题导致数值有极小的差异
money = money - ren
list1.append(ren / 100)
list1.append(money/100)
print(list1)
print('手气最佳:',max(list1))
九九乘法表
def case13(self):
for i in range(1, 10):
for j in range(1, i+1):
print('{}*{}={}'.format(j, i, i*j), end=' ')
print()
打印出1,2,3,4互不相同切无重复的三位数字
例如: 123 132 134
def case14(self):
# 打印出1234中互不相同切无重复的三位数
import itertools
s = 1234
list1 = list(str(s))
print(list1)
for i in range(len(list1)):
copy_list = list1.copy()
copy_list.pop(i)
print(copy_list)
list2 = []
list2.extend(itertools.permutations(copy_list, 3))
print('list2:', list2)
给你一个字符串date
,按YYYY-MM-DD
,返回该日期是当年的第几天。
def case15(date: str) -> int:
year, month, day = [int(x) for x in date.split("-")]
amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
amount[1] += 1
ans = sum(amount[:month - 1])
print(ans + day)
return ans + day
网友评论