[python] 小demo

 

BZ的python是个小白哈!

源论坛地址:http://www.pythonla.com/thread-3.html  注明下出处哈!!!

 

2014.12.15

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

 1 list = []
 2 count = 0
 3 for i in range(1,5):
 4     for j in range(1,5):
 5         if i != j:
 6             for k in range(1,5):
 7                 if i != k and j != k:
 8                     num.append(i*100+j*10+k)
 9                     count += 1
10 print(count)
11 print(list)

上边是BZ写的笨办法。在论坛中发现大神写的,我给赋在下边哈(特别简洁,膜拜啊):

1 lis = set([1,2,3,4])
2 l = [ x*100+y*10+z for x in lis for y in lis-set([x]) for z in lis-set([x])-set([y])]
3 print len(l),l

 

题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?

BZ的笨办法:

 1 while True:  
 2     money = input('input a number:')
 3     if money <=10:
 4         a = money * (1+0.1)
 5     if money > 10 and money <=20:
 6         a = (money-10) * (1+0.075) + 10 * (1+0.1)
 7     if money > 20 and money <= 40:
 8         a = (money-20) * (1+0.05) + 10 * (1+0.1+0.075)
 9     if money > 40 and money <= 60:
10         a = (money-40) * 0.03 + 10 * (1+0.1+0.075) + 20 * (1+0.05)
11     if money >60 and money <=100:
12         a = (money-60) * 0.015 + 10 * (1+0.1+0.075) + 20 * (1+0.08)
13     if money > 100:
14         a = (money-100) * 0.01 + 10 * (1+0.1+0.075) + 20 * (1+0.05) + 40 * (1+0.015)
15     print(a)

论坛大神的:

1 while True:
2     a=input('input a number:')
3     item={'a<=10':'a*(1+0.1)',' 10< a <=20':'10*(1+0.1)+(a-10)*(1+0.075)', 
4           '20< a <40':'(a-20)*(1+0.05)+10*1.175'}
5     print [ eval(item[i]) for i in item.keys() if eval(i)][0]

 

2014.12.19

 

题目:判断今天是2014年的第多少天?

BZ自己写的

 1 import time
 2 days = 0
 3 month_31 = [1,3,5,7,8,10,12]
 4 month_30 = [4,6,9,11]
 5 today = time.localtime()
 6 #today = [2014,12,31]
 7 year = today[0]
 8 month = today[1]
 9 day = today[2]
10 if year%4==0 and year%100!=0 or year%400==0:
11     month_2th = 29
12 else:
13     month_2th = 28
14 if month > 2:
15     for a in range(1,month):
16         if a in month_31:
17             days = days + 31
18         elif a in month_30:
19             days = days + 30
20         elif a == 2:
21             days = days + month_2th
22     days = days + day
23 elif month == 2:
24     days = 31+day
25 else:
26     days = day
27 print(days)

   time.time() 获取当前时间戳
   time.localtime() 当前时间的struct_time形式。struct_time对象共9个元素元素,分别为,年,月,日,时,分,秒,星期几,今年的第几天,是否夏令时。

当然也可以直接打印time.localtime()的第八项。

 论坛中大神的思想(很强大,有木有!!):

1 import time
2 print(int((time.time() - time.mktime(time.strptime('2014', '%Y'))) / (86400)) + 1)

 

 

2015.1.28

 

题目:随机生成指定数量的随机字符串,并统计每个字符出现的次数。

1 import random
2 char_list = [chr(i) for i in range(48,58)+range(65,91)+range(97,123)]
3 number = input('input length : ')
4 out_char_list = []
5 for i in range(number):
6     out_char_list.append(char_list[random.randrange(len(char_list))])
7 print(''.join(out_char_list))
8 for a in set(out_char_list):
9     print("%s display: %d times" %(a,out_char_list.count(a)))

 

posted @ 2014-12-15 11:48  战侠歌1994  阅读(265)  评论(0编辑  收藏  举报