最新开始自学Python,下面是做的习题,自我记录一下,方便日后查看。
# 有4个数字,1,2,3,4 他们可以组成多少个互不相同且无重复数字的3位数,各是多少
def test1():
i=1
j=1
k=1
n=0
while i<5:
print('i')
while j<5:
print('j')
while k<5:
if i!=j and j!=k and i!=k :
print('k',i*100+j*10+k)
k=k+1
j=j+1
i=i+1
def test1():
i=1
j=1
k=1
n=0
while i<5:
print('i')
while j<5:
print('j')
while k<5:
if i!=j and j!=k and i!=k :
print('k',i*100+j*10+k)
k=k+1
j=j+1
i=i+1
#一个整数加上100后是一个完全平方数,在加上168有是一个完全平方数,,轻微该数是多少
def test2():
li = [x*x for x in range(50)]
while True:
i=n+100
j=n+100+168
if i in li and j in li:
print('n=',n)
break
else:
n = n+1
def test2():
li = [x*x for x in range(50)]
while True:
i=n+100
j=n+100+168
if i in li and j in li:
print('n=',n)
break
else:
n = n+1
#输入某年某月某日,判断这一天是这一年的第几天
#print("Please input datetime,as YYYY-MM-DD")
#d = input()
import re
def countDays(datetime):
if re.match(r'\d{4}-\d{2}-\d{2}',datetime):
year = datetime[:4]
month = datetime[5:7]
day=datetime[-2:]
days = int(day)
feb = 28
if month == '01':
print("这一天是",year,"的第",days,"天")
elif month == '02':
if int(year) %100 ==0 and int(year) %400 ==0:
feb=29
elif int(year) %100 !=0 and int(year) %4 ==0:
feb = 29
print("这一天是",year,"的第",31+days,"天")
elif month == '03':
print("这一天是",year,"的第",31+feb+days,"天")
elif month == '04':
print("这一天是",year,"的第",31*2+feb+days,"天")
elif month == '05':
print("这一天是",year,"的第",31*2+30+feb+days,"天")
elif month == '06':
print("这一天是",year,"的第",31*3+30+feb+days,"天")
elif month == '07':
print("这一天是",year,"的第",31*3+30*2+feb+days,"天")
elif month == '08':
print("这一天是",year,"的第",31*4+30*2+feb+days,"天")
elif month == '09':
print("这一天是",year,"的第",31*4+30*3+feb+days,"天")
elif month == '10':
print("这一天是",year,"的第",31*5+30*3+feb+days,"天")
elif month == '11':
print("这一天是",year,"的第",31*6+30*3+feb+days,"天")
elif month == '12':
print("这一天是",year,"的第",31*6+30*4+feb+days,"天")
else:
print("inut error")
else:
print("input error")
#print("Please input datetime,as YYYY-MM-DD")
#d = input()
import re
def countDays(datetime):
if re.match(r'\d{4}-\d{2}-\d{2}',datetime):
year = datetime[:4]
month = datetime[5:7]
day=datetime[-2:]
days = int(day)
feb = 28
if month == '01':
print("这一天是",year,"的第",days,"天")
elif month == '02':
if int(year) %100 ==0 and int(year) %400 ==0:
feb=29
elif int(year) %100 !=0 and int(year) %4 ==0:
feb = 29
print("这一天是",year,"的第",31+days,"天")
elif month == '03':
print("这一天是",year,"的第",31+feb+days,"天")
elif month == '04':
print("这一天是",year,"的第",31*2+feb+days,"天")
elif month == '05':
print("这一天是",year,"的第",31*2+30+feb+days,"天")
elif month == '06':
print("这一天是",year,"的第",31*3+30+feb+days,"天")
elif month == '07':
print("这一天是",year,"的第",31*3+30*2+feb+days,"天")
elif month == '08':
print("这一天是",year,"的第",31*4+30*2+feb+days,"天")
elif month == '09':
print("这一天是",year,"的第",31*4+30*3+feb+days,"天")
elif month == '10':
print("这一天是",year,"的第",31*5+30*3+feb+days,"天")
elif month == '11':
print("这一天是",year,"的第",31*6+30*3+feb+days,"天")
elif month == '12':
print("这一天是",year,"的第",31*6+30*4+feb+days,"天")
else:
print("inut error")
else:
print("input error")
#输入3个整数x,y,z, 请把这3个数由小到大输出
import types
def order_min_to_max(li):
i,j=0,1
n = 0
while i <len(li):
while j<len(li):
print("li=:",li)
pass
print("li order by min to max:",li)
def test3(li):
li.sort()
print("sorted order:",li)
li.sort()
print("sorted order:",li)
def test4():
print("sort order:",sorted(li))
bn
print("sort order:",sorted(li))
bn
#输出9*9乘法口诀
def multi():
for i in range(1,10):
for j in range(1,10):
if i>=j:
print(i,'*',j,'=',i*j,end=' ')
print()
for i in range(1,10):
for j in range(1,10):
if i>=j:
print(i,'*',j,'=',i*j,end=' ')
print()
#判断101到200之间有多少个素数,并输出所有素数
def isPrimeNumber(n1,n2):
for i in range(n1,n2):
k,f=2,0
while k<i:
if i%k ==0:
f=1
break
else:
k+=1
if f==0:
print(i,end=' ')
def isPrimeNumber(n1,n2):
for i in range(n1,n2):
k,f=2,0
while k<i:
if i%k ==0:
f=1
break
else:
k+=1
if f==0:
print(i,end=' ')
#输出所有“水仙花数”,水仙花 数是指一个3位数,并其各位数立方之和等于该数本省,例如 153 = 1**3+5**3+3**3
def isNarcissistic_number():
for i in range(100,999):
j = int(str(i)[0])
k = int(str(i)[1])
n = int(str(i)[2])
if i== j**3 + k**3 + n**3:
print(i,end=' ')
#对一个正整数分解质因数 例如 90=2*3*5*3; 234=[2, 3, 13]
def Comp_prime_factor(k):
n,f =2,0
for i in range(2,k):
if k%i ==0 :
#print('k',k,'%i',i,'=',k%i)
while 2<=n<i:
#print('i',i,'%n',n,'=',i%n)
if i%n ==0:
f=1
n+=1
if f==0:
print('i=',i)
n = 2
f = 0
#利用条件运算符的嵌套来完成,高于90为A,60 到 89之间 用B 表示, 60 分一下用C表示
def test_student_grade(score):
try:
if re.match(r'\d{2}',score):
if score>90:
print("Grade is A")
elif 60<score<89:
print("Grade is B")
elif score<60:
print("Grade is C")
else:
print("arg error")
except:
print("arg error")
def Comp_prime_factor(k):
n,f =2,0
for i in range(2,k):
if k%i ==0 :
#print('k',k,'%i',i,'=',k%i)
while 2<=n<i:
#print('i',i,'%n',n,'=',i%n)
if i%n ==0:
f=1
n+=1
if f==0:
print('i=',i)
n = 2
f = 0
#利用条件运算符的嵌套来完成,高于90为A,60 到 89之间 用B 表示, 60 分一下用C表示
def test_student_grade(score):
try:
if re.match(r'\d{2}',score):
if score>90:
print("Grade is A")
elif 60<score<89:
print("Grade is B")
elif score<60:
print("Grade is C")
else:
print("arg error")
except:
print("arg error")
'''#输出指定格式的日期,提示用detetime'''
#输入一行字符串,分别统计出其中的英文字母,空格,数字和其他字符的个数
def count_str_num(str):
num,abc,non,other=0,0,0,0
for s in str:
if s.isdigit():
num+=1
elif s.isalpha():
abc+=1
elif s==' ':
non+=1
else:
other+=1
print("alpha_count=",abc,"none_count=",non,"number_count=",num,'others_count=',other)
# 求s = a+aa+aaa+aaaa+aaaaad 值(a是一个数,相加字数的个数将由用户通过键盘输入来指定)
def sum_num(number,count_num):
s,temp=0,number
li=[number]
for i in range(0,count_num):
s = s + temp
print("temp = ",temp,'s=',s)
temp= temp*10 + number
li.append(temp)
print("s = ",str(li),'= ',s)
def count_str_num(str):
num,abc,non,other=0,0,0,0
for s in str:
if s.isdigit():
num+=1
elif s.isalpha():
abc+=1
elif s==' ':
non+=1
else:
other+=1
print("alpha_count=",abc,"none_count=",non,"number_count=",num,'others_count=',other)
# 求s = a+aa+aaa+aaaa+aaaaad 值(a是一个数,相加字数的个数将由用户通过键盘输入来指定)
def sum_num(number,count_num):
s,temp=0,number
li=[number]
for i in range(0,count_num):
s = s + temp
print("temp = ",temp,'s=',s)
temp= temp*10 + number
li.append(temp)
print("s = ",str(li),'= ',s)
#一个数如果切好等于他的因子之和,这个数就称为 “完数”。例如6=1+2+3,编程找出1000以内的所有完数
def is_perfect_number(n):
for i in range(1,n):
j = 1
li=[] # 将i中的因子放在li list 中
while j<i:
if i%j ==0:
li.append(j)
j+=1
j = 1
s = 0
for k in li:
s = s+k
if s == i:
print(i, end=' ')
def is_perfect_number(n):
for i in range(1,n):
j = 1
li=[] # 将i中的因子放在li list 中
while j<i:
if i%j ==0:
li.append(j)
j+=1
j = 1
s = 0
for k in li:
s = s+k
if s == i:
print(i, end=' ')
#将一个列表的数据复制到另一个列表中
li=[1,2,3]
li2 =li.copy()
#一个球从100米高度自由落下,每次落地后反弹回原高度的一半,在落下,呢么他在第10此了哟第时,
#共经过多少米?第10次反弹多高
li=[1,2,3]
li2 =li.copy()
#一个球从100米高度自由落下,每次落地后反弹回原高度的一半,在落下,呢么他在第10此了哟第时,
#共经过多少米?第10次反弹多高
#
#
#输出如下图案,arg=9
'''
*
***
*****
*******
*****
***
*
'''
def rhombus(n):
li=[]
if n%2!=0:
n = n-1
for i in range(0,n):
if i%2!=0:
k = 0
while k< n-int(i):
print(' ',end='')
k+=1
k=0
while k< int(i):
print('*',end='')
k+=1
print()
li.append(i)
list.sort(li,reverse=True)
# print(li)
for j in li[1:]:
m =0
# print(max(li), int(j))
while m<=max(li)-int(j):
print(' ',end='')
m+=1
m=0
while m<int(j):
print('*',end='')
m+=1
print()
'''
*
***
*****
*******
*****
***
*
'''
def rhombus(n):
li=[]
if n%2!=0:
n = n-1
for i in range(0,n):
if i%2!=0:
k = 0
while k< n-int(i):
print(' ',end='')
k+=1
k=0
while k< int(i):
print('*',end='')
k+=1
print()
li.append(i)
list.sort(li,reverse=True)
# print(li)
for j in li[1:]:
m =0
# print(max(li), int(j))
while m<=max(li)-int(j):
print(' ',end='')
m+=1
m=0
while m<int(j):
print('*',end='')
m+=1
print()
#有1分数序列:2/1, 3/2, 5/3 , 8/5, 13/8, 21/13,求这个数列前20项之和
def sum(n):
i,j,k,s = 1,1,0,0
maxnum = n/2 + 1
for m in range(1,int(maxnum)):
if int(m)==1:
k =i+j
s = k/i
print(k,'/',i,end =' ')
else:
k = k+i
s = s+k/i
print(k,'/',i,end =' ')
i = i+k
print(k,'/',i,end =' ')
s =s+i/k
print('s=',s)
def sum(n):
i,j,k,s = 1,1,0,0
maxnum = n/2 + 1
for m in range(1,int(maxnum)):
if int(m)==1:
k =i+j
s = k/i
print(k,'/',i,end =' ')
else:
k = k+i
s = s+k/i
print(k,'/',i,end =' ')
i = i+k
print(k,'/',i,end =' ')
s =s+i/k
print('s=',s)
def sum2(n):
i,j,s = 1,2,0
for m in range(0,n):
s = s + j/i
j = i+j
i = j-i
print('s=',s)
i,j,s = 1,2,0
for m in range(0,n):
s = s + j/i
j = i+j
i = j-i
print('s=',s)