Data analysis topics 2/27

1. Write a function to print common calendar that for any given year and month, press the map can be printed correctly correspond to the calendar:

 

(1) month of the year to test the general situation (size of months);

(2) ordinary year in February the situation;

(3) test cases leap year in February

(4) using the above function, the output of a calendar year for all months

Method 1: import calendar module

>>> import calendar
>>> yy = int(input("输入年份: "))
输入年份: 2020
>>> mm = int(input("输入月份: "))
输入月份: 2
>>> # 显示日历
print(calendar.month(yy,mm))
   February 2020
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29

Method 2: Hand knock Code

year = int(input("请输入年份:"))
if((year %4 == 0 and year %100 != 0) or (year % 400 == 0)):run = True
else:run = False     #这行也可以不用写
i = 1900
sum = 0
while i < year - 1:
    i += 1
    if((i % 4 == 0 and i % 100 != 0) or (i % 400 == 0)):sum += 366
    else: sum += 365
month = int(input("请输入月份:"))
j = 1
while j < month:
    if((j == 1) or (j == 3) or (j == 5) or (j == 7) or (j == 8) or (j == 10) or (j == 12)):sum += 31
    elif j == 2:
        if run:sum += 29
        else: sum += 28
    else: sum += 30
    j += 1
week = (sum + 1) % 7
if ((month == 1) or (month == 3) or (month == 5) or (month == 7) or (month == 8) or (month == 10) or (month == 12)):day = 31
elif month == 2:               #二月的嵌套判断,闰年二月29,平年28
    if run:day = 29
    else:day = 28
else:day = 30
print("日\t一\t二\t三\t四\t五\t六")
count = 0    #定义一个计数器,以便后面的换行
k = 0
while k <= week:   #每个月的开始第一周前面的空格数
    k += 1
    print("\t",end="")
    count += 1
    if (count % 7 == 0):print("\n")
   # count=7 进行换行
p = 1
while p <= day:    #显示天数
    print(p,"\t",end="")     #打印  table 不换行
    p += 1
    count += 1
    if(count % 7 == 0):print("\n")        #count=7 进行换行

2. Below are two tables, one table describes some US counties where the state, a form describing the population of these counties. Programming, please use the name of the county two tables will be merged into a table.

Table state

Table population

Tip: To observe, think of ways to both tables County name with unified rules combined into a single name, and then merge the two tables.

3. File lexicon.txt are some of the English word pronunciation dictionary file: each line of the file corresponds to an entry, the first column of each row corresponding to the word itself, the back row should pronounce phoneme sequences of words. Write a function from lexicon.txt relevant content read out into a dictionary structure, the return of the dictionary structure, and test the phoneme sequence peppers and piper is correct.

a ah
a ey
cat k ae t
of ah v
peck p eh k
peppers p eh p er z
peter p iy t er
picked p ih k t
pickled p ih k ah l d
piper p ay p er
the dh ah
the dh iy
where's w eh r z

Published 115 original articles · won praise 9 · views 8124

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/104508920