defsolv(): whileTrue: s = input() if s.isdigit(): num = int(s) print(f'{s} 是' + ('偶数'if num%2 == 0else'奇数')) else: print(f'{s} 不是整数')
判断闰年
判断用户输入的年份是否为闰年 非整百年:能被4整除的为闰年 整百年:能被400整除的是闰年
1 2 3 4 5 6 7 8 9 10 11 12 13
defsolv(): whileTrue: year = int(input()) if year % 100 == 0: if year % 400 == 0: print(f'{year}是闰年') else: print(f'{year}不是闰年') else: if year % 4 == 0: print(f'{year}是闰年') else: print(f'{year}不是闰年')
获取最大值函数
使用max()方法求最大值
1 2 3 4
defsolv(): whileTrue: numList = [float(x) for x in input().split(' ')] print(f'The max num of {numList} is {max(numList)}')
defisPrime(num): if num < 2: returnFalse n = int(num**0.5) for i in range(2,n+1): if num % i == 0: print(f'{num//i} * {i} = {num}') returnFalse returnTrue
defsolv(): whileTrue: num = int(input()) if isPrime(num): print(f'{num}是质数') else: print(f'{num}不是质数')
# 利用筛法判断[0,highBound]之间的数是否为素数 defgenerate(highBound): resList = [Truefor i in range(highBound+1)] resList[0:2] = [Falsefor i in range(2)] for i in range(2,int(highBound**0.5)+1): # 如果素数,采用筛法:筛去该数的所有倍数 if resList[i]: num = 2 * i while num <= highBound: resList[num] = False num += i else: pass return resList defsolv(): curHighBound = 1000 primsTillHighBound = generate(curHighBound) whileTrue: lowBound,highBound = [int(x) for x in input().split(' ')] if highBound > curHighBound: curHighBound = highBound primsTillHighBound = generate(curHighBound) res = [i for i in range(lowBound,highBound+1) if primsTillHighBound[i]] print(f'{lowBound}-{highBound}间的素数集合:{res}')
defsolv(): whileTrue: originNum = int(input()) num = originNum n = len(str(num)) res = 0 while num != 0: res += pow(num%10,n) num //= 10 if originNum == res: print(f'{originNum}是阿姆斯特朗数') else: print(f'{originNum}不是阿姆斯特朗数')
十进制转二进制、八进制、十六进制
实现十进制转二进制、八进制、十六进制
1 2 3 4
defsolv(): whileTrue: dec = int(input()) print(f'十进制数:{dec};二进制:{bin(dec)};八进制:{oct(dec)};十六进制:{hex(dec)}')
ASCII码与字符相互转换
实现ASCII码与字符相互转换
1 2 3 4 5 6 7 8 9 10
defsolv(): whileTrue: print(f'1.字符转ASCII码\n2.ASCII码转字符') choice = int(input()) if choice == 1: c = input() print(f'字符:{c} => ASCII码 = {ord(c)}') elif choice == 2: num = int(input()) print(f'ASCII码:{num} => 字符 = {chr(num)}')
最大公约数
用户输入两个数,返回这两个数的最大公约数
1 2 3 4 5 6 7
defgcd(a,b): return b if a%b == 0else gcd(b,a%b) defsolv(): whileTrue: a,b = [int(x) for x in input().split(' ')] print(f'{a}和{b}的最大公约数为:{gcd(a,b)}')
最小公倍数算法
用户输入两个数,返回这两个数的最大公倍数
1 2 3 4 5 6 7
defgcd(a,b): return b if a%b == 0else gcd(b,a%b) defsolv(): whileTrue: a,b = [int(x) for x in input().split(' ')] print(f'{a}和{b}的最大公约数为:{a*b//gcd(a,b)}')
deffib(n): if n <= 1: return n return fib(n-1) + fib(n-2)
defsolv(): count = int(input()) for i in range(count): print(fib(i),end=' ')
文件 IO
演示Python基本的文件操作,包括 open,read,write
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defsolv(): file = input("请输入您要写入的文件名:") with open(file,'wt') as f: whileTrue: s = input("请输入要写入的内容:") if len(s) > 0: f.write(s) else: break print('写入完成!')
print('读取文件:') with open(file,'r') as f: for line in f.readlines(): print(line)
defsolv(): peopleNum = 30 peoples = [Truefor i in range(peopleNum)] index = 0 while peopleNum > 15: count = 0 whileTrue: if peoples[index]: count += 1 if count == 9:break index = (index+1)%30 peoples[index] = False print(f'{index+1}号下船了') peopleNum -= 1
五人分鱼
A、B、C、D、E 五人在某天夜里合伙去捕鱼,到第二天凌晨时都疲惫不堪,于是各自找地方睡觉。 日上三杆,A 第一个醒来,他将鱼分为五份,把多余的一条鱼扔掉,拿走自己的一份。 B 第二个醒来,也将鱼分为五份,把多余的一条鱼扔掉拿走自己的一份。 C、D、E依次醒来,也按同样的方法拿鱼。 问他们至少捕了多少条鱼?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defsolv(): # finalFish是最后一个人分到的鱼数目 finalFish,fish = 1,1 flag = False whilenot flag: flag = True fish = finalFish for _ in range(5): if fish % 4 == 0: fish = fish // 4 * 5 + 1 else: flag = False break finalFish += 1 else: print(f'至少捕鱼{fish}条')
定义一个整型数组,并将指定个数的元素翻转到数组的尾部。 例如:(ar[], d, n) 将长度为 n 的 数组 arr 的前面 d 个元素翻转到数组尾部。
1 2 3 4 5 6 7 8
defsolv(): n = int(input("请输入数组大小:")) arr = list(range(1,n+1)) print(f'已为您自动生成数组{arr}') d = int(input("请输入翻转个数:")) arr.extend(arr[:d]) del arr[:d] print(f'翻转后数组为:{arr}')
提取字符串中的 URL
给定一个字符串,里面包含 URL 地址,需要我们使用正则表达式来获取字符串的 URL
1 2 3 4 5 6 7
import re defsolv(): pattern = re.compile(r'https?://(?:[-./\w]|(?:%[\da-zA-Z]{2}))+') whileTrue: s = input() urls = pattern.findall(s) print(f'urls:{urls}')
将字符串作为代码执行
给定一个字符串代码,然后使用 exec() 来执行字符串代码。
1 2 3 4 5 6
def factorial(num): fact=1 for i in range(1,num+1): fact = fact*i return fact print(factorial(5))
1 2 3 4 5 6 7 8 9 10
defsolv(): code = ''' def factorial(num): fact=1 for i in range(1,num+1): fact = fact*i return fact print(factorial(5)) ''' exec(code)
# 快速排序 defpartion(arr,left,right): if right-left < 1: return i,j = left+1,right while i <= j: while i <= j and arr[j] > arr[left]: j -= 1 if i > j: break while i <= j and arr[i] < arr[left]: i += 1 if i > j: break arr[i],arr[j] = arr[j],arr[i] arr[left],arr[j] = arr[j],arr[left] partion(arr,left,j-1) partion(arr,j+1,right)
defquickSort(arr): partion(arr,0,len(arr)-1)
1 2 3 4 5 6 7 8
# 选择排序 defchooseSort(arr): for i in range(len(arr)): index = i for j in range(i+1,len(arr)): if arr[j] < arr[index]: index = j arr[i],arr[index] = arr[index],arr[i]
1 2 3 4 5 6 7 8 9 10 11
# 冒泡排序 defbubbleSort(arr): flag = False count = 0 whilenot flag: flag = True for i in range(0,len(arr)-1-count): if arr[i] > arr[i+1]: arr[i],arr[i+1] = arr[i+1],arr[i] flag = False count += 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 归并排序 defmerge(arr,left,right): if left == right: return res = [] s1,s2 = left,left + (right-left)//2 merge(arr,left,s2) merge(arr,s2+1,right) i,j = s1,s2+1 while i <= s2 and j <= right: if arr[i] <= arr[j]: res.append(arr[i]) i += 1 else: res.append(arr[j]) j += 1 res.extend(arr[i:s2+1]) if i <= s2 else res.extend(arr[j:right+1]) arr[left:right+1] = res[:]
# 堆排序 defadjust(arr,i,n): while i < n: leftNode = 2 * i + 1 rightNode = 2 * i + 2 largestIndex = i if leftNode < n and arr[leftNode] > arr[largestIndex]: largestIndex = leftNode if rightNode < n and arr[rightNode] > arr[largestIndex]: largestIndex = rightNode
if largestIndex != i: arr[i],arr[largestIndex] = arr[largestIndex],arr[i] # 继续向下调整 i = largestIndex else: break
defheapSort(arr): # 建立一个大根堆 # unNodeIndex是第一个非叶子结点的下标 unNodeIndex = (len(arr)-1)//2 for i in range(unNodeIndex,-1,-1): adjust(arr,i,len(arr))
for i in range(len(arr)-1,0,-1): # 将最大的元素(根元素)放入最后一个,然后调整剩余元素组成的堆,得到新的大根堆 arr[i],arr[0] = arr[0],arr[i] adjust(arr,0,i)
1 2 3 4 5 6 7 8 9 10 11 12 13
# 希尔排序 defshellSort(arr): n = len(arr) gap = n // 2 while gap > 0: for i in range(gap,n): j = i - gap num = arr[i] while j >= 0and arr[j] > num: arr[j+gap] = arr[j] j -= gap arr[j+gap] = num gap //= 2
1 2 3 4 5 6 7 8 9 10 11 12
# 计数排序 defcountSort(arr): s,e = min(arr),max(arr) countList = [0] * (e - s + 1) for item in arr: countList[item] += 1 ans = [] for i in range(len(countList)): while countList[i] > 0: ans.append(i) countList[i] -= 1 arr[:] = ans[:]