Huawei OD machine test-decrypting the crime time

 Question description

When the police are solving a case, they get the possible crime time given by the informant, which is expressed in the form of "HH:MM".

According to the agreement between the police and the informant, this time was modified in order to conceal it.

The decryption rule is: use the numbers that have appeared so far to construct the next moment closest to the current time, then this time is the possible crime time.

Each appearing number can be used an unlimited number of times.

Enter description

A string in the form of HH:SS, representing original input.

Output description

A string in the form of HH:SS, indicating the crime time of inference processing.

Remark

1. It can be guaranteed that the given string must be legal.

For example, "01:35" and "11:08" are legal, but "1:35" and "11:8" are illegal.

2. The closest moment may be the next day.

Example 1

20:12得到20:20

23:59得到22:22

12:58得到15:11

18:52得到18:55

23:52得到23:53

09:17得到09:19

07:08得到08:00

Code

# coding:utf-8
# 解密犯罪时间
import sys

try:
    while True:
        time = sys.stdin.readline().replace(":", "").strip()
        tmp = list(map(int, list(time)))
        HH = str(tmp[0]) + str(tmp[1])
        MM = str(tmp[2]) + str(tmp[3])
        ans = [HH, MM]
        print(f'tmp:{tmp}, HH:{ans[0]}, MM:{ans[1]}')
        ss = []
        for i in tmp:
            for j in tmp:
                s = str(i) + str(j)
                if s not in ss:
                    ss.append(s)
        ss.sort()
        flag = False
        for m in ss:
            if int(ans[1]) < int(m) < 60:
                ans[1] = m
                flag = True
                break
        if not flag:
            for h in ss:
                if int(ans[0]) < int(h) < 24:
                    ans[0] = h
                    ans[1] = min(ss)
                    flag = True
                    break
        if flag:
            print(f'Time:{ans[0]}:{ans[1]}')
        else:
            ans[0] = min(ss)
            ans[1] = min(ss)
            print(f'Time:{ans[0]}:{ans[1]}')

except:
    pass

Guess you like

Origin blog.csdn.net/SD_JZZ/article/details/132666591