+ 1

US dates to EU dates

#Convert US dates to EU dates #Input in mm/dd/yyyy or format like January 1, 2021 #Code passes first 2 tests but fails next 3 tests a = input() if not '/' in a: a = a.split() import calendar dic = {} for num in range(1,13): dic[calendar.month_name[num]] = num def usdteu(dt): if '/' in dt: return dt[3:5] + '/' + dt[:2] + '/' + dt[-4:] else: return dt[1][0] + '/' + str(dic[dt[0]]) + '/' + dt[-1] print(usdteu(a))

21st Jan 2023, 8:30 AM
Sanjay Sahu
Sanjay Sahu - avatar
3 odpowiedzi
+ 2
Sanjay Sahu Right now your code only works for input dates where the day and month values have exactly 2 digits (e.g. dt[3:5] expects 2 numbers only). I suspect the failed test cases are ones where the input date have 1 digit days and/or months like 1/31/2019 or April 1, 2020. To fix, just edit theusdteu() function: 1) Inside the if part, split dt using '/' as the separator. This will return a list of exactly month, day and year values, which you can then output in EU format. 2) Inside the else part, change dt[1][0] to dt[1][:-1] to include the whole day value without the comma character. Alternatively, I suggest using the datetime module instead to elegantly handle the date format conversions. https://docs.python.org/3/library/datetime.html Edit: Clarity and typos
21st Jan 2023, 8:52 AM
Mozzy
Mozzy - avatar
+ 1
Sanjay Sahu You're welcome! Happy to help :)
21st Jan 2023, 10:49 AM
Mozzy
Mozzy - avatar
0
Mozzy Thanks so much for your prompt response.. As rightly pointed out by you, the code was failing the last 3 test cases because it didn't take care of presence of extra digits in dates of the format 'January 21, 2023'. I tweaked the code accordingly and it was able to pass all 5 tests.
21st Jan 2023, 9:33 AM
Sanjay Sahu
Sanjay Sahu - avatar