Convert Date to Binary - Problem

You are given a string date representing a Gregorian calendar date in the yyyy-mm-dd format.

date can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in year-month-day format.

Return the binary representation of date.

Input & Output

Example 1 — Standard Date
$ Input: date = "2019-06-10"
Output: "11111100011-110-1010"
💡 Note: 2019 in binary is 11111100011, 06 in binary is 110, 10 in binary is 1010. Join with dashes: 11111100011-110-1010
Example 2 — Early Year
$ Input: date = "1900-01-01"
Output: "11101101100-1-1"
💡 Note: 1900 in binary is 11101101100, 01 in binary is 1, 01 in binary is 1. Result: 11101101100-1-1
Example 3 — Large Numbers
$ Input: date = "2000-12-31"
Output: "11111010000-1100-11111"
💡 Note: 2000 in binary is 11111010000, 12 in binary is 1100, 31 in binary is 11111. Result: 11111010000-1100-11111

Constraints

  • date.length == 10
  • date[4] == date[7] == '-'
  • All other characters in date are digits
  • date represents a valid Gregorian calendar date between 1000-01-01 and 9999-12-31

Visualization

Tap to expand
Convert Date to Binary INPUT "2019-06-10" Date Components: YEAR 2019 MONTH 06 DAY 10 Decimal Values: 2019 6 10 Format: yyyy-mm-dd Gregorian Calendar Date ALGORITHM STEPS 1 Split by Delimiter Split date string by "-" 2 Parse to Integer Convert each part to int 3 Convert to Binary Use built-in bin() function 4 Join with "-" Concatenate results Binary Conversion: 2019 --> 11111100011 6 --> 110 10 --> 1010 FINAL RESULT Binary Date Representation: 11111100011 (Year: 2019) - 110 (Month: 6) - 1010 (Day: 10) Output: "11111100011-110-1010" OK Key Insight: Python's built-in bin() function converts integers to binary strings (prefixed with "0b"). Use slicing [2:] to remove the prefix. No leading zeros in output as per problem requirements. TutorialsPoint - Convert Date to Binary | Built-in Binary Conversion Approach
Asked in
Google 15 Microsoft 12 Amazon 8
12.4K Views
Medium Frequency
~8 min Avg. Time
543 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen