+ 1

Anyone got any idea to shorten or improve this code

Program to input date in us format and covert it to eu format. Input example 11/25/2019 or November 25, 2019 Output example 25/11/2019 https://code.sololearn.com/cA234WQMw1Dy/?ref=app

10th Apr 2022, 4:10 PM
Shaurya Agarwal
Shaurya Agarwal - avatar
3 Answers
+ 4
date = input().replace(", ","/").replace(" ","/").split("/") if date[0] in month: date[0] = month[date[0]] print("/".join([date[1],date[0],date[2]]))
11th Apr 2022, 3:34 AM
Simba
Simba - avatar
+ 1
Yes...have a look at regular expressions and use re.findall rather than using split(). on the date.. It might look daunting an the beginning (and I was dreading having to learn it) but they make task like this a lot easier
10th Apr 2022, 5:56 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Same code, but much shorter with corrected use of dictionary lookup and other unnecessary loops eliminated. (I also shortened the dictionary keys for aesthetics): #using len() and split() func ia = input() month= {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7, "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12} def conv_date_format(x): x[0], x[1]= x[1], x[0] return if 8<=len(ia)<=10: lista= ia.split("/") else: lista = ia.replace(",", "").split() lista[0] = str(month[lista[0][:3]]) print("/".join(conv_date_format(lista)))
10th Apr 2022, 11:31 PM
Brian
Brian - avatar