1.filter 编程闰年
def judge_year(year):
if (year%4==0 and year%100!=0) or year%400 ==0:
return True
else:
return False
year_list=range(1987,2020)
leap_year=filter(judge_year,year_list)
print leap_year
#列表解析
[year for year in range(1987,2020) if (year%4==0 and year%100!=0) or year%400 ==0]
2.
mp = {0: "zero", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six",
7:"seven", 8:"eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve",
13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen",
18: "eighteen", 19: "nineteen",
20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty",
70: "seventy", 80: "eighty", 90: "ninety", 100: "hundred",
1000: "thousand", 1e6: "million", 1e9: "billion", 1e12: "trillon"};
def TranslateNum(snum):
snum = snum.lstrip('0');
if(snum == ""): return "";
dw = int(snum);
res = "";
if(dw <= 19):
res = mp[dw];
elif(dw < 100):
res = mp[dw//10*10];
a = TranslateNum(snum[-1]);
if(a != ""): res += '-' + a;
else:
bw = len(snum)-1;
if(bw <= 2): bw = 2;
elif(bw <= 5): bw = 3;
elif(bw <= 8): bw = 6;
elif(bw <= 11): bw = 9;
elif(bw <= 14): bw = 12;
else: return "infinity........";
weight = 10**bw;
res = TranslateNum(snum[:-bw]) + ' ' + mp[weight];
a = TranslateNum(snum[-bw:]);
if(a != ""):
if(bw == 2): res += " and ";
else: res += ", ";
res += a;
return res;
eres = input(">>> ");
if(int(eres) != 0):
eres = TranslateNum(eres);
else:
eres = 0;
print(eres);
3.
def
datedif(date1,date2):
"利用datetime模块计算两个日期字串之间的天数,格式YYYY-MM-DD"
if
date1
=
=
date2:
return
0
from
datetime
import
date
d1
=
map
(
int
,date1.split(
'-'
))
d2
=
map
(
int
,date2.split(
'-'
))
d1
=
date(d1[
0
],d1[
1
],d1[
2
])
d2
=
date(d2[
0
],d2[
1
],d2[
2
])
return
(d1
-
d2).days
def
dttoday(date1):
"计算date1到今天的天数,date1格式为YYYY-MM-DD"
from
datetime
import
date
date2
=
date.today().isoformat()
return
datedif(date1,date2)
def
dtb(date1):
"计算生日为date1的人还有多久过生日,date1格式为YYYY-MM-DD"
from
datetime
import
date
today
=
date.today()
d1
=
map
(
int
,date1.split(
'-'
))
d1
=
date(today.year,d1[
1
],d1[
2
])
if
today>d1:
d1
=
d1.replace(year
=
d1.year
+
1
)
return
(d1
-
today).days