标签:fir break 对齐 pytho 练习 pac env lse range
#!/usr/bin/env /usr/bin/python3 # -*- coding:utf-8 -*- # 1.冒泡排序 l = [8,3,22,15,17] for i in range(1,len(l)): flag = False for j in range(0,len(l)-i): if l[j+1] < l[j]: l[j],l[j+1] = l[j+1],l[j] flag = True if not flag: break print(l) # 2.乘法表及对齐 for i in range(1,10): line = "" for j in range(1,i+1): proc = i * j if j == 1: line += "{}*{}={} ".format(j,i,proc) else: line += "{}*{}={:<2} ".format(j,i,proc) print(line) for i in range(1,10): for j in range(1,i+1): proc = i * j if j ==1 or proc >10: proc = str(proc) else: proc = str(proc) + ‘ ‘ print("%d*%d=%s"%(j,i,proc),end=" ") print() # 3.乘法表反写 for i in range(1,10): for j in range(1,10): if j < i: print(" "*7,end="") else: proc = i * j print("{}*{}={:<2}".format(i,j,proc),end=" ") print() # -*- coding:utf-8 -*- # 4.1-n阶乘的和 n = 5 sum = 0 b = 1 for i in range(1,n+1): b *= i sum += b print(sum) # 5.打印菱形 for i in range(-3,4): space = abs(i) print(‘ ‘*space + "*"*(7-space*2) + " "*space) # 5.小于100的斐波那契数列 a = 1 b = 1 while a < 100: print(a) a,b = b,a+b # 6.斐波那契数列的第101项 a = 1 b = 1 count = 0 while True: count += 1 if count == 101: print(a) break a,b = b,a+b # 7.列表["flower","flight","flow"],找出公共字符串最长匹配 l = ["flower","flight","flow"] # 方法一: def func(): n = min([len(x) for x in l]) s = ‘‘ for index in range(n): for i in range(len(l)-1): if l[i][index] != l[i+1][index]: return s else: s += l[i][index] print(func()) # 方法二: def func(): s = ‘‘ for each in zip(*l): if len(set(each)) == 1: s += each[0] return s print(func()) # 方法三: def func(): prefix = ‘‘ first = l[0] others = l[1:] for i in first: prefix += i for j in others: if not j.startswith(prefix): return prefix[:-1] print(func()) # 8.字符串assbdjsda去重 s = ‘assbdjsda‘ new_s = ‘‘ for i in s: if i not in new_s: new_s += i print(new_s)
标签:fir break 对齐 pytho 练习 pac env lse range
原文地址:https://www.cnblogs.com/kuangfengnulang/p/14715161.html