3280. 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.
https://leetcode.cn/problems/convert-date-to-binary/
Example 1:
Input:
date = "2080-02-29"
Output:"100000100000-10-11101"
Explanation:
100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.
Example 2:
Input:
date = "1900-01-01"
Output:"11101101100-1-1"
Explanation:
11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.
Constraints:
date.length == 10date[4] == date[7] == '-', and all otherdate[i]’s are digits.- The input is generated such that
daterepresents a valid Gregorian calendar date between Jan 1st, 1900 and Dec 31st, 2100 (both inclusive).
Test Cases
class Solution:
def convertDateToBinary(self, date: str) -> str:solution_test.py下载完整源码
import pytest
from solution import Solution
@pytest.mark.parametrize('date, expected', [
("2080-02-29", "100000100000-10-11101"),
("1900-01-01", "11101101100-1-1"),
])
@pytest.mark.parametrize('sol', [Solution()])
def test_solution(sol, date, expected):
assert sol.convertDateToBinary(date) == expected
Thoughts
Code
solution.py下载完整源码
class Solution:
def convertDateToBinary(self, date: str) -> str:
return '-'.join(bin(int(x))[2:] for x in date.split('-'))
评论需要 JavaScript。