+ 1
Is there a shorter way to code this challenge?
month = {'January':1, 'February':2, 'March':3, 'April':4, 'May':5, 'June':6, 'July':7, 'August':8, 'September':9, 'October':10, 'November':11, 'December':12} def usDate(a): y = a[0] if y.isdigit(): x = a.split("/") temp = x[0] x[0] = x[1] x[1] = temp return "/".join(x) elif y.isalpha(): c = a.replace(",","") x = c.split(" ") b = month.get(x[0]) x[0] = str(b) temp = x[0] x[0] = x[1] x[1] = temp return "/".join(x) res = usDate(input()) print(res)
2 Respostas
+ 4
Angelmar Dayangco some of the logic can be consolidated since both clauses in the if statement do a swap and then return the same join statement. Furthermore, the swap can be eliminated if you place elements of x in the desired order inside a tuple, and pass the tuple to join.
month = {'January':1, 'February':2, 'March':3, 'April':4, 'May':5, 'June':6, 'July':7, 'August':8, 'September':9, 'October':10, 'November':11, 'December':12}
def usDate(a):
y = a[0]
if y.isdigit():
x = a.split("/")
elif y.isalpha():
c = a.replace(",","")
x = c.split()
x[0] = str(month.get(x[0]))
return "/".join((x[1],x[0],x[2]))
res = usDate(input())
print(res)
My own version is a little shorter because I compare only the first 3 characters of the month names.
https://code.sololearn.com/cYLtndwVshDA/?ref=app
+ 3
Use
x[0], x[1] = x[1], x[0]
instead of
temp = x[0]
x[0] = x[1]
x[1] = temp
Good Luck