Skip to content

Instantly share code, notes, and snippets.

@Youngestdev
Created July 11, 2020 15:08
Show Gist options
  • Save Youngestdev/34956f676e825136eea973f3715d0078 to your computer and use it in GitHub Desktop.
Save Youngestdev/34956f676e825136eea973f3715d0078 to your computer and use it in GitHub Desktop.
Leetcode contest

1. Reformat Date

Given a date string in the form Day Month Year, where:

Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}. Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}. Year is in the range [1900, 2100]. Convert the date string to the format YYYY-MM-DD, where:

YYYY denotes the 4 digit year. MM denotes the 2 digit month. DD denotes the 2 digit day.

class Solution:
    def reformatDate(self, date: str) -> str:
        arr = date.split(" ")
        store = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        arr[1] = str(store.index(arr[1]) + 1)
        if len(arr[1]) < 2:
          arr[1] = "0" + arr[1] 
        

        if len(arr[0]) == 4:
          arr[0] = arr[0][:2]
        elif len(arr[0]) == 3:
          arr[0] = arr[0][:1]
        
        if len(arr[0]) < 2:
          arr[0] = "0" + arr[0] 

        arr.reverse()
        ans = "-".join(arr)
        return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment