为什么我的代码在少数测试用例中失败而在少数测试用例中通过?

问题描述 投票:0回答:1

我正在解决一个问题,其中日期字符串以“日月年”的形式给出,然后我必须将字符串输出返回为 YYYY-MM-DD 格式。以下是我的问题陈述。 **给定日月年形式的日期字符串,其中:

日期在集合 {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"} 中。 月份在集合 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", “十二月”}。 年份范围为 [1900, 2100]。 将日期字符串转换为 YYYY-MM-DD 格式,其中:

YYYY 表示 4 位数年份。 MM 表示 2 位数月份。 DD 表示 2 位数的日期。**

所以我已经为它编写了代码,但它只通过了少数测试用例,并且只有少数测试用例失败。像下面这样

class Solution(object):
    def reformatDate(self, date):
        list_date = date.split()
        YYYY = ""
        MM = ""
        DD = ""

        DD_dict = {
            "1st": "1", "2nd": "2", "3rd": "3", "4th": "4",
            "5th": "5", "6th": "6", "7th": "7", "8th": "8", "9th": "9", "10th": "10",
            "11th": "11", "12th": "12", "13th": "13", "14th": "14", "15th": "15", "16th": "16",
            "17th": "17", "18th": "18", "19th": "19", "20th": "20", "21st": "21", "22nd": "22",
            "23rd": "23", "24th": "24",
            "25th": "25", "26th": "26", "27th": "27", "28th": "28", "29th": "29", "30th": "30", "31st": "31"
        }

        MM_dict = {
            "Jan": "1", "Feb": "2", "Mar": "3", "Apr": "4",
            "May": "5", "Jun": "6", "Jul": "7", "Aug": "8", "Sep": "9", "Oct": "10",
            "Nov": "11", "Dec": "12"
        }

        for char in list_date:
            if char in DD_dict:
                if int(DD_dict[char]) < 10:
                    DD += '0' + DD_dict[char]
                else:
                    DD += DD_dict[char]
            elif char in MM_dict:
                if int(MM_dict[char]) < 10:
                    MM += '0' + MM_dict[char]
                else:
                    MM += MM_dict[char]
            else:
                YYYY += char

        return "".join(f'{int(YYYY)}-{int(MM)}-{int(DD)}')


if __name__ == '__main__':
    date = "20th Oct 2052"
    d2 = "26th May 1960"

    print(Solution().reformatDate(date))
    print(Solution().reformatDate(d2))

因此,在这里,输入日期 =“2052 年 10 月 20 日”的正确输出显示为 2052-10-20,但输入 d2 =“1960 年 5 月 26 日”的输出显示错误为“1960-5-26”。它应将输出显示为“1960-05-26”。你能告诉我哪里错了吗?

python python-3.x list python-2.7
1个回答
0
投票
here's You are Corrected Code   


class Solution(object):
    def reformatDate(self, date):
        list_date = date.split()
        DD = ""
        MM = ""
        YYYY = ""

        DD_dict = {
            "1st": "01", "2nd": "02", "3rd": "03", "4th": "04",
            "5th": "05", "6th": "06", "7th": "07", "8th": "08", "9th": "09", "10th": "10",
            "11th": "11", "12th": "12", "13th": "13", "14th": "14", "15th": "15", "16th": "16",
            "17th": "17", "18th": "18", "19th": "19", "20th": "20", "21st": "21", "22nd": "22",
            "23rd": "23", "24th": "24",
            "25th": "25", "26th": "26", "27th": "27", "28th": "28", "29th": "29", "30th": "30", "31st": "31"
        }

        MM_dict = {
            "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
            "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10",
            "Nov": "11", "Dec": "12"
        }

        # Extracting day, month, and year from the split list
        DD = DD_dict[list_date[0]]  # First element is the day
        MM = MM_dict[list_date[1]]   # Second element is the month
        YYYY = list_date[2]           # Third element is the year

        # Return the formatted date
        return f"{YYYY}-{MM}-{DD}"

if __name__ == '__main__':
    date = "20th Oct 2052"
    d2 = "26th May 1960"

    print(Solution().reformatDate(date))  # Output: "2052-10-20"
    print(Solution().reformatDate(d2))    # Output: "1960-05-26"
© www.soinside.com 2019 - 2024. All rights reserved.