(1)使用y-cruncher工具得待PI的亿万位的txt文档;
(2)代码很简单,首先是得到可操作的文档,然后提示用户输入要查找的生日,默认的格式是year-mouth-day,例如94-01-01,先检查输入的生日格式是否正确,要是正确的话就从文档中进行查找,若查找到就返回其存在的个数,从查找的结果来看,本人的生日出现的次数还是挺多的,但是要是查找的8位的生日,就没有那么幸运了。
(3)本程序只能对特定格式的数进行查找,要是想要查找别的数据,稍微的更改下程序就可以了。
import sys
import re
def get_file():
#get the file to operate
f = open(r"K:\_\Python\Pi.txt", "r")
lines = f.read()
f.close()
return lines
def judge_birthday(number):
#judge the format of birthday is righy or not
dir_birthday = {'01':31, "02":28, "03":31, "04":30,
"05":31, "06":30, "07":31, "08":31,
"09":30, "10":31, "11":30, "12":31
}
if len(number) < 8:
number = '19' + number
year = int(number[:4])
mouth = int(number[4:6])
day = int(number[6:])
if (year > 2000) or (year < 1900):
return False
if (mouth > 12) or (mouth < 1):
return False
else:
day_correct = dir_birthday[number[4:6]]
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
day_correct += 1
if (day < 1) or (day > day_correct):
return False
return True
def search_in_PU(number, lines):
#search the number of birthday in the PI file
length = len(re.findall(number, lines))
if length < 1:
print("Not found!")
else:
print(number + ": " + str(length))
if __name__ == "__main__":
lines = get_file()
print("Enter the year that you want to search: ")
number = input()
flag = judge_birthday(number)
if flag:
search_in_PU(number, lines)
else:
print("The format of your birthday is not correct!")