PAT (Advanced Level) 1061 Dating Python题解

1061 Dating (20分)

Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 -- since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week -- that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

Sample Output:

THU 14:04

做这个系列主要是练习自己用Python做OJ题的输入输出问题。本题较为简单,代码时间复杂度为O(n)。先上AC代码:

s1 = input()
s2 = input()
s3 = input()
s4 = input()
weekday = {'A': 'MON', 'B': 'TUE','C': 'WED','D':'THU','E':'FRI','F':'SAT', 'G':"SUN" }
for i in range(min(len(s1),len(s2))):
    if(s1[i] == s2 [i] and 'A' <= s1[i] <= 'G'):
        day = s1[i]
        break
for j in range(i + 1, min(len(s1),len(s2))):
    if(s1[j] == s2[j] and ('0'<=s1[j]<='9' or 'A'<=s1[j]<='N')):
        H = s1[j]
        break
for i in range(min(len(s3),len(s4))):
    if(s3[i] == s4[i] and ('a'<=s3[i]<='z' or 'A'<=s3[i]<='Z')):
       M = i
       break

HH = str(ord(s1[j]) - ord('0') if '0'<=s1[j]<='9' else ord(s1[j]) - ord('A') + 10)
MM = str(M)
#print(HH)
#print(MM)
print('{} {}:{}'.format(weekday[day],HH.zfill(2),MM.zfill(2)))
       
       
       

主要注意点1是一开始的input也可以写成(且显然更好):

s = []
for i in range(4):
    s.append(input())

注意点2是Python中ascii码和字符转换函数。查看字符的ascii码函数为ord(str),将一个ascii码转换为字符的函数为chr(ascii_number)。

注意点3是输出的时候的格式。右对齐,左补0,代码为str.zfill(bits)。左对齐右补0比较少见,在此不作记录。

猜你喜欢

转载自blog.csdn.net/zjy997/article/details/105355578