记录一下这些练习的个人实作。
1.题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
list = []
list_num = [1,2,3,4]
for i in list_num:
for j in list_num:
for k in list_num:
if (j != i and k != j and k != i):
a = i*100 + j*10 + k
list.append(a)
print (list)
print ("排列列表:")
col_num = 0
#for i in range(len(list)):
for i in list:
col_num = col_num + 1
#print('%-5d' %(list[i]),end="")
print('%-5d' %i,end="")
if col_num > 3 :
col_num = 0
print ("")
2.题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
tc_dic = {0:0.1, 10:0.075, 20:0.05, 40:0.03, 60: 0.015, 100:0.01}
def cal_bonus(num,dic):
bonus = 0
gear = list(dic.keys())
rate = list(dic.values())
cal_num = num
i = 0
#while i < len(gear):
while i in range(len(gear)):
#print("i=%d gear=[%d ~ %d]" %(i,gear[i],gear[i+1]))
#print("cal_num=%d" %cal_num)
if cal_num <= gear[i+1]:
bonus += cal_num*10000*(rate[i])
#print("i=%d bonus=%d" %(i,bonus))
#print("over");
break
else:
bonus += gear[i+1]*10000*(rate[i])
cal_num -= gear[i+1]
i += 1
#print("i=%d bonus=%d" %(i,bonus))
print("总奖金:")
print(bonus)
prof = input("请输入利润:")
cal_bonus(int(prof),tc_dic)
3.题目:输入某年某月某日,判断这一天是这一年的第几天?
day_cnts = [31,28,31,30,31,30,31,31,30,31,30,31]
year = input("请输入年:")
year_num = int(year)
leap = 0
if (year_num % 400 == 0) or ((year_num % 4 == 0) and (year_num % 100 != 0)):
leap = 1
month = input("请输入月:")
month_num = int(month)
if month_num not in range(1,13):
print("错误的月份:%d月" %month_num)
exit()
day = input("请输入日:")
day_num = int(day)
if month_num == 2:
if day_num not in range(1,28+leap+1):
print("该年2月错误的天数:%d天" %day_num)
exit()
else:
if day_num not in range(1,day_cnts[month_num]):
print("错误的天数:%d天" %day_num)
exit()
day_in_year = 0
i = 0
while i < (month_num-1):
day_in_year += day_cnts[i]
if i == 2:
day_in_year += leap
i += 1
day_in_year += day_num
print("%4d/%2d/%2d是%4d年的第%d天" %(year_num,month_num,day_num,year_num,day_in_year))
4.题目:斐波那契数列。
l = []
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
l.append(a)
#return a
fib(10)
print(l)
5.题目:输出 9*9 乘法口诀表。
for i in range(1,10):
if i > 1:
print("")
for j in range(1,i+1):
print("%d*%d=%d " %(i,j,i*j),end="")
print("")
6.题目:文本颜色设置
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
print(bcolors.WARNING + "警告的颜色字体?" + bcolors.ENDC)
7.题目:使用lambda来创建匿名函数。这题示例有点意思,解读一下。
MAXIMUM = lambda x,y : (x > y) * x + (x < y) * y
MINIMUM = lambda x,y : (x > y) * y + (x < y) * x
if __name__ == '__main__':
a = 10
b = 20
print ('The largar one is %d' % MAXIMUM(a,b))
print ('The lower one is %d' % MINIMUM(a,b))
(x > y) * x + (x < y) * y
如 x > y成立,则 x > y == true == 1,反之此时 x < y == false == 0;
(x > y) * y + (x < y) * x这句也是一样的原理。