1、计算 N!
def mian(n):
for i in range(1,n):
n*=i
return n
2.计算并返回任意多个整数的和(不能用for循环)·······
def mian(*x):
return sum(x)
3.返回item在lst中第一次的位置
def main(lst,item):
i=0
for num in lst:
if num==item:
return i
i+=1
return '不存在'
4.返回列表包含大于或等于所有整数平均值的整数
def main(lst):
avg=sum(lst)/len(lst)
lst2=[]
for i in lst:
if i>=avg:
lst2.append(i)
return lst2
5.返回一个元组,第一个元素为p整除q的商,第二个元素为p对q的余数
def main(p,q):
return (int(p/q),p-q*int(p/q))
6.返回正整数num各位数字之和
def main(num):
s=str(num)
ls=list(map(int,s))
return sum(ls)
7.列表去重
def main(lst):
return list({}.fromkeys(lst).keys())
8.列表字符串转化为小写
def main(lst):
for in range(len(lst)):
lst[i]=lst[i].lower()
return lst
9.列表字符串排序
def main(lst):
lst.sort(key=lambda ele:len(ele),reverse=True)
return lst
10.计算20,30,40的阶乘
from functools import reduce
from operator import mul
def main(n):
return reduce(mul,range(1,n+1))
print(main(20))
print(main(30))
print(main(40))
11.向量的内积
def main(vector1,vector2):
return sum(list(map(lambda num1,num2:num1*num2,vector1,vector2)))
12.最长字符串
def main(lst):
return max(lst,key=len)
13.去除列表中的0
def main(lst):
return list(filter(lambda x:x!=0,lst))
14.查找列表绝对值最大
def main(lst):
Max=0
for i in range(len(lst)):
if abs(lst[Max]<abs(lst[i])):
Max=i
return lst[Max]
15.删除偶数
def main(lst):
return [i for i in lst if i%2]
16.字符串填充
def main(s):
return s.center(20."#")
17.字符串数字汉字转换
def main(s):
nums=['零','一','二','三','四','五','六','七','八','九']
s2=''
for i in range(len(s)):
if s[i].isdigit():
num=int(s[i])
s2+=nums[num]
continue
s2+=s[i]
return s2
18.表中重复元素
def main(lst):
# 数据很水头两行可加可不加
if len(lst)==1:
return 1
set_lst=set(lst)
if len(set_lst)==len(lst):
return 1
elif len(set_lst)==1:
return 0
else:
return 2
19.回文
import re
def main(s):
s=re.sub('^A-Za-z','',s)
return s[::-1]==s
20.接受任意字符串s,要求返回最多的前三个字符
from collections import Counter
def main(s):
c=Counter(s)
c=Counter(dict(counts.most_common(3)))
return list(counts)
21.最大素数
def isPrime(num):
for i in range(2,n-1):
if num%i==0:
return False
return True
def main(n):
i=n
while i!=0
if isPrime(i):
return i
i-=1
return i
22.组合数C(n,i)
def main(n,i):
nums1,num2=1,1
i=min(i,n-i)
for x in range(1,i+1):
nums1 *=x
nums2 *=x+n-i
return nums2//nums1
23.用map()计算a+aa+aaa
def main(n,a):
l=list(range(1,n+1))
return sum(list(map(lambda x:int(str(a)*x),l)))
24.读取文件用逗号分隔开的整数*10
def main():
f=open('data24.txt')
s=f.read().split(',')
return list(map(lambda x:int(x)*10,s))
print(main())
25.集合的并集
def main(lst):
l=set()
for i in lst:
l=l|i
return l
26.计算sin()
from math import sin radians
def main(lst):
return list(map(lambda x:sin(radians(x)),lst))
27.时间间隔
from datetime import date
def main(year1,month1,day1,year2,month2,day2)
return abs((date(year1,month1,day1)-date(year2,month2,day2)).days)
28.闰年
def main(year):
if year%400==0 or (year%4==0 and year%100 !=0):
return 'yes'
return 'no'
29.max(func(x))
def main(func,lst):
return max(list(map(func,lst)))
30.截尾平均数,保留一位小数
def main(tup):
return round((sum(tup)-max(tup)-min(tup))/(len(tup)-2),1)
31.斐波那契
def main(n):
a,b=1,1
if(n==1):
return 1
sum=1
for i in range(n-1):
t=a
a=b
b+=t
sum+=a
return sum
32.字符串*n+传参默认值设置
def main(s,n=3):
return s*n
33.循环左移
def main(s, n):
return s[n:]+s[0:n]
34.模拟整数类型操作
class Number:
def __init__(self, value):
self.value = value
def __add__(self, another):
return self.value + another.value
def __str__(self):
return str(self.value)
def main(x, y):
return x+y
35.模拟整数/实数部分操作
class Number:
def __init__(self, value):
if type(value) != int and type(value) != float:
self.__value = 0
else:
self.__value = value
def __set(self, value):
if type(value) == int or type(value) == float:
self.__value = value
def __get(self):
return self.__value
value = property(__get, __set)
def main(x, y):
obj = Number(x)
obj.value = y
return (type(obj), obj.value)
36.并联
def main(*para):
sum=0
for i in para:
sum+=1/i
return round(1/sum,1)
37.黑洞数
def main(n):
n=str(n)
max=""
min=""
for it in sorted(n,reverse = True):
max +=str(it)
for it in sorted(n):
min+=str(it)
if int (max) -int(min)==int(n):
return True
else:
return False
38.水仙花数
def main(n):
n_str=str(n)
sum=0
l=len(n_str)
for i in n_str:
sum+=int(i)**l
if sum==n:
return True
else:
return False
39.列表绝对值之和
def main(lst):
return sum(list(map(abs,lst)))
40.返回不同编码的字符串,如果不是字符串返回‘参数必须是字符串’
def main(s):
try:
return(s.encode("UTF-8",s.encode("GBK"))
except:
return '参数必须是字符串'
41.字符串包含
def main(s1,s2,*s3)
if s2 not in s1:
return False
for s in s3:
if s not in s1:
return False
return True
42.计算圆的面积
from math import pi as PI
def main(r):
if isinstance(r,(int)) and r>0:
return round(PI*r*r,2)
else:
return '参数必须是大于0的整数或实数'
43.正整数周围相接能够组成的最大的数
from itertools import permutations
def main(lst):
max=0
lst=list(map(str,lst))
for i in permutations(lst):
a=''.join(i)
re=eval(a)
if re>max:
max=re
return max
44.n*n棋盘放米
def main(n):
return 2**(n*n)-1
45.返回变成小写之后最大的字符串
def main(lst):
try:
L=[x.lower() for x in lst if isinstance(x,str)]
return lst[L.index(max(L))]
except:
return '数据格式不正确'
46.众数
def main(lst):
list_set=set(lst)
frequency_dict={}
for i in list_set:
frequency_dict[i]=lst.count(i)
max_frequency=max(frequency_dict.values())
for key,value in frequency_dict.items():
if value==max_frequency:
m=key
return m
47.余弦定理求C
from math import cos,radians
def main(a,b,theta):
try:
if a<=0 or b<=0:
return '数据不对'
return round((a*a+b*b-2*a*b*cos(radians(theta)))**0.5,1)
except:
return '数据不对'
48.列表差
def main(vector1,vector2):
try:
if len(vector1)!=len(vector2):
return'数据不对'
return sum(list(map(lambda x,y:abs(x-y),vector1,vector2)))
except:
return '数据不对'
49.成绩字母转换
def main(score):
try:
if score>=90 and score<=100:
return 'A'
elif score>=80 and score <90:
return 'B'
elif score>=70 and score <80:
return 'C'
elif score>=60 and score <70:
return 'D'
elif score>=0 and score <60:
return 'F'
else:
return '数据不对'
except:
return '数据不对'
50.小于n的最大斐波那契数
def main(n):
a,b=1,1
while b<n:
t=a
a=b
b=t+b
return a
51.鸡兔同笼
def main(n,m):
y=(m-2*n)/2
x=(4*n-m)/2
if y-int(y) or x-int(x) or x<0 or y<0:
return '数据不对'
return int(x),int(y)
52.列表中最大值的位置
def main(lst):
m= max(lst)
l=[]
for i in range(len(lst)):
if lst[i]==m:
l.append(i)
return l
53.文件后+_new
from os.path import splitext
import os
def main(s):
name,bac=os.path.splitext(s)
name+='_new'
return name+bac
54.列表元素相加等于10
from itertools import combinations
def main(lst):
l=[]
for i in combinations(lst,3):
if i[0]+i[1]+i[2]==10:
l.append(i)
return l
55.返回只出现一次的字符
def main(s):
s1=''
for i in s:
if s.count(i)==1:
s1+=i
return s1
56.列表数据重复
def main(lst):
set_lst=set(lst)
if len(set_lst)==len(lst):
return 1
elif len(set_lst)==1:
return 0
else:
return 2
57.去除多余空格
import re
def main(s):
s=s.strip()
s=re.sub(' +',' ',s)
return s
58返回最长的数字字符串
from re import findall
import re
def main(s):
try:
nums=re.findall(r'\d+',s)
nums.sort(key=lambda num:len(num),reverse=True)
return nums[0]
except:
return '没有数字'
59.列表最大公约数
from math import gcd
from funcrools import reduce
def main(lst):
return reduce(gcd,lst)
60.查找docx文件关键字
from docx import Document
def main():
dt=Document('data60.docx')
for i in range(len(dt.paragraphs)):
if '山东' and '烟台' in dt.paragraphs[i].text:
return dt.paragraphs[i].text
print(main())
61.docx整数相加
from docx import Document
def main():
dt=Document('data60.docx')
sum=0
table=dt.tables
table=table[0]
for i in range(0,len(table.rows)):
for j in range(0,len(table.row_cells(i)):
sum+=int(table.cell(i,j).text)
return sum
print(main())
62.千分位的逗号
def main(s):
try:
l=len(s)
if l<=3:
return int(s)
i=l-4
while i>=0:
if s[i]!=',':
return '数据错误'
i-=4
s=s.replace(',','')
return int(s)
execpt:
return '数据错误’
63.去除前导0
def main(s):
a=''
s1=''
for i in s:
if str.isdigit(i):
a+=i
else:
s1+=str(int(a))
s1+=i
a=''
return s1+str(int(a))
64.日期补0
def main(s):
s1=''
s1+=s[0:5]
a=''
for i in range(5,len(s)):
if s[i].isdigit():
a+=s[i]
else:
s1+=a.rjust(2,'0')
s1+=s[i]
a=''
return s1+a.rjust(2,'0')
65.插入千分符
def main(num):
s=str(num)
l=len(s)
if l<=3:
return str(num)
i=l%3
s1=''
if i:
s1=s[0:i]
s1+=','
while i<l:
s1+=s[i:i+3]
s1+=','
i+=3
return s1.rstrip(',')
66.整数拆分
def main(num):
try:
num=int(num)
s=str(num)
s1=''
for i in s:
s1+=i
s1+=','
return s1.rstrip(',')
except:
return '数据错误'
67.>=start第num个素数
def main(start,num):
while num>0:
if isPrime(start):
num-=1
start+=1
return start-1
68.丑数
def main(num):
while num % 2 == 0:
num = num / 2
while num % 3 == 0:
num = num / 3
while num % 5 == 0:
num = num / 5
if num == 1:
return True
else:
return False
69.正整数按各位数字相加之和大小排序的列表
def f(num):
s=0
while num:
s+=num%10
num/=10
return s
def main(lst):
lst.sort(key=f)
return lst
70.奇数位,偶数位之和
def main(lst):
odd=sum(lst[::2])
even=sum(lst[1::2])
return (even,odd)
71.判断列表中是否为实数
def main(lst):
try:
l=list(map(lambda x:int(x),lst))
return True
except:
return False
72.i>8&i%2==0
def main(lst):
try:
l=[]
for i in lst:
if isinstance(i,int):
if i>8 and i%2==0:
l.append(i)
else:
return '数据不符合要求'
except:
return '数据不符合要求'
73.字符串比对
def main(s1,s2):
num=0
for i,j in zip(s1,s2):
if i==j:
num+=1
return num
74.字符串拼接
def main(s1,s2):
m=min(len(s1),len(s2))
for i in range(m,0,-1):
if s1[-i:]==s2[:i]:
return s1+s2[i:]
return s1+s2
75.多个数的最大公约数
from math import gcd
from functools import reduce
def main(*integers):
try:
l=len(integers)
if l==0: return '必须提供至少一个整数'
return reduce(gcd,integers)
except:
return '必须都是整数'
76.二进制连续的0
import math
def main(n):
return int(math.log2(n&-n))
77.小猴吃桃
def f(n):
if n==1:
return 1
else:
return f(n-1)*2+2
def main(n):
return f(n)
78.计算颜色()
from operator import itemgetter
from docx import Document
from docx.shared import RGBColor
def main():
word=Document('data78.docx')
colors={}
for p in word.paragraphs:
for r in p.runs:
color=r.font.color.rgb
if color not in (RGBColor(0,0,0),None):
colors[color]=colors.get(color,0)+1
colors=sorted(colors.items(),key=itemgetter(1),reverse=True)
return tuple(map(str,map(itemgetter(0),colors[:3])))
print(main())
79.连乘
from operator import mul
from functools import reduce
def main(lst):
return reduce(mul,lst)
80.|a-b|
from operator import sub
def main(a,b):
return sum(map(abs,map(sub,a,b)))
81.a//b,a%b
def main(a,b):
return(a//b,a%b)
82.字符串s2在s1中的个数
def main(s1,s2):
l=list(set(list(s2)))
sum=0
for i in l:
sum+=s1.count(i)
return sum
83,返回闭区间所有整数之和
def main(start,end)
return sum(range(start,end),end)
84.返回绝对值的最大整数
def main(data):
return max(data,key=abs)
85.中位数
def main(data):
data_local=data[:]
data_local=sorted(data_local)
return data_local[len(data_local)//2]
86.素数
def main(num):
for n in range(2,num+1)[::-1]:
f=1
for i in range(2,int(n**0.5)+1):
if n%i==0:
f=0
break
if f:
return n
87.全局变量修改
x=3
def main(num):
global x
x=num
return globals()['x']
88.返回圆的面积
from math import pi as PI
def main(r):
if isinstance(r,(int,float)) and r>0:
return round(PI*r*r,3)
else:
return '半径必须为大于0的整数或实数'
89.返回对应位置字符相同的数量
def main(origin, userInput):
return sum(map(lambda oc,uc:oc==uc,origin,userInput))
90.约瑟夫环(因为不会用cycle,直接按思路来过了)
def main(lst,k):
d=k-1
for i in range(len(lst)-1):
del lst[d]
d=(d+k-1)%len(lst)
return lst[0]
91.替换字符
def main(s):
s=s.replace('a','A')
s=s.replace('e','E')
s=s.replace('i','I')
s=s.replace('u','U')
return s
92.计算字符串左边空格
def main(s):
i=0
while s[i]=='' and i<len(s):
i+=1
return i
93.计算第几天
def is_a_leap_year(year):
if year % 100 != 0 and year % 4 == 0:
return True
if year % 100 == 0 and year % 400 ==0:
return True
return False
def main(year,month,day):
leap_year = is_a_leap_year(year)
if leap_year:
day_list = [0,31,29,31,30,31,30,31,31,30,31,30,31]
else:
day_list = [0,31,28,31,30,31,30,31,31,30,31,30,31]
for i in range(month):
day+= day_list[i]
return day
94.判断升序
def main(data):
pre=0
for i in range(1,len(data)):
if data[pre]>data[i]:
return False
pre+=1
return True
95.拼音排序
from pypinyin import pinyin
def main(s):
s=sorted(s,key=lambda c:pinyin(c))
ans=''
for i in s:
ans+=i
return ans
96.密码判断
def main(pwd):
l=len(pwd)
if l<6:
return 'weak'
check = [0,0,0,0]
for char in pwd:
if char.islower():
check[0] = 1
if char.isupper():
check[1] = 1
if char.isdigit():
check[2] = 1
if not (char.isalpha()|char.isdigit()|char.isspace()):
check[3] = 1
if check_num==1:
return 'weak'
elif check_num==2:
return 'below_middle'
elif check_num==3:
return above_middle'
else:
return 'strong'
97.大小写判断
def main(s):
a=0
b=0
for ch in s:
if ch.isupper():
a+=1
if ch.islower():
b+=1
return (a,b)
98.多项式计算
def main(factors,x)
coeffs=factors
n = len(coeffs)
exponents = range(n-1, -1, -1)
powers_of_x = [x**i for i in exponents]
return reduce(lambda acc, pair: acc + pair[0]*pair[1], zip(coeffs, powers_of_x), 0)
99.上台阶
def main(n):
if n<3:
return n
if n==3:
return 4
x,y,z=1,2,4
for i in range(4,n+1):
t=x+y+z
z=y
y=z
z=t
return z
100.文件最长一行
def main():
with open("data100.txt") as file:
l=file.readlines()
return max(list(map(lambda s:len(s),l)))
print(main())
文章出处登录后可见!