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.
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:
"12-25-21"
"21-25-12"
mm=12, dd=25, yy=21 → reassemble as yy-dd-mm = "21-25-12"
Solution approaches¶
Key takeaways¶
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.
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.
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.