Skip to content

S1Q2 · Convert Date from mm-dd-yy to yy-dd-mm

⚡ Quick Reference

Function: mm_dd_yy_to_yy_dd_mm(date: str) -> str

Core idea: split on -, reorder the three parts, rejoin.

def mm_dd_yy_to_yy_dd_mm(date: str) -> str:
    mm, dd, yy = date.split("-")
    return f"{yy}-{dd}-{mm}"

Key rules: - Input: "mm-dd-yy" → Output: "yy-dd-mm" - Only the month and year swap positions; day stays in the middle - split("-") gives [mm, dd, yy] - unpack directly


Problem Statement

Problem

Write a function mm_dd_yy_to_yy_dd_mm(date) that converts a date string from mm-dd-yy to yy-dd-mm format.

Example:

Input
"12-25-21"
Output
"21-25-12"

mm=12, dd=25, yy=21 → reassemble as yy-dd-mm = "21-25-12"


Solution approaches

def mm_dd_yy_to_yy_dd_mm(date: str) -> str:
    mm, dd, yy = date.split("-")
    return f"{yy}-{dd}-{mm}"
def mm_dd_yy_to_yy_dd_mm(date: str) -> str:
    mm = date[:2]
    dd = date[3:5]
    yy = date[6:]
    return f"{yy}-{dd}-{mm}"

Fixed positions: mm at 0-1, dd at 3-4, yy at 6-7. Direct slicing without splitting.

def mm_dd_yy_to_yy_dd_mm(date: str) -> str:
    p = date.split("-")
    return "-".join([p[2], p[1], p[0]])

Split into a list, reorder by index, rejoin. [p[2], p[1], p[0]] reverses the first and last parts.

mm_dd_yy_to_yy_dd_mm = lambda date: "-".join(date.split("-")[::-1][::-1][0:1] + date.split("-")[1:2] + date.split("-")[::-1][:1])

Simpler lambda:

mm_dd_yy_to_yy_dd_mm = lambda d: (lambda p: f"{p[2]}-{p[1]}-{p[0]}")(d.split("-"))

Split in an inner lambda, reassemble in the outer expression.


Key takeaways

01

split + unpack for structured strings

mm, dd, yy = date.split("-") splits and names the parts in one line. Unpacking directly is cleaner than indexing into the list.

02

Only month and year swap

The day stays in position 2 of the output. Only mm and yy exchange places. Re-read the format carefully - it's easy to miss that dd is unchanged.

03

Slicing vs splitting

Both work for fixed-format strings. split("-") is more readable when the separator is meaningful. Direct slicing (date[:2]) is useful when you know exact character positions.