PAT 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

题意:给出4个字符串,根据题目描述破解隐含的约会时间信息。

解题思路:
1.先把4个字符串保存起来,记为s1,s2,s3,s4。
2.对s1和s2,根据长度短的一个进行循环,把相同的字符先存到列表l中。
3.对l循环找出第一个在A到G之间的字符,获取字符在l中的位置index,以及获得天数day。
4.对l从index+1开始找出第一个0到9或者A到N的字符,获得小时hour。
5.对s3和s4,根据长度短的一个进行循环,找出第一个是英文字母且相同的字符的索引,获得分钟minute。
6.最后将(day,hour,minute)按照题目所给格式输出。

s1 = input()
s2 = input()
s3 = input()
s4 = input()
#s1 = '3485djDkxh4hhGE '
#s2 = '2984akDfkkkkggEdsb '
#s3 = 's&hgsfdkm '
#s4 = 'd&Hyscvnm'
l = []
Day = ['MON','TUE','WED','THU','FRI','SAT','SUN']
length = min(len(s1), len(s2))
for i in range(length):
    if s1[i] == s2[i]:
        l.append(s1[i])

for i in l:
    if 'A' <= i <= 'G':
        index = l.index(i)
        day = Day[ord(i) - ord('A')] 
        break

for i in range(index+1, len(l)):
    if 'A' <= l[i] <= 'N':
        hour = ord(l[i]) - ord('A') + 10
        break
    elif '0' <= l[i] <= '9':
        hour = int(l[i])
        break

length = min(len(s3), len(s4))
for i in range(length):
    if s3[i] == s4[i]:
        if 'a' <= s3[i] <= 'z' or 'A' <= s3[i] <= 'Z':
            minute = i
            break
print('%s %02d:%02d'%(day,hour,minute))

猜你喜欢

转载自blog.csdn.net/qq_36936510/article/details/86702847