From 4897c3b81d0a7d41a0357372537b41bcffde9134 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 1 Jan 2025 15:53:34 +0800 Subject: [PATCH] 20250101 finished. --- src/problem/mod.rs | 2 + src/problem/p3280_convert_date_to_binary.rs | 49 +++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/problem/p3280_convert_date_to_binary.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5c0dca1..2a53ca0 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -404,3 +404,5 @@ mod p1366_rank_teams_by_votes; mod p1367_linked_list_in_binary_tree; mod p3219_minimum_cost_for_cutting_cake_ii; + +mod p3280_convert_date_to_binary; diff --git a/src/problem/p3280_convert_date_to_binary.rs b/src/problem/p3280_convert_date_to_binary.rs new file mode 100644 index 0000000..a598a6b --- /dev/null +++ b/src/problem/p3280_convert_date_to_binary.rs @@ -0,0 +1,49 @@ +/** + * [3280] Convert Date to Binary + */ +pub struct Solution {} + +// submission codes start here +use std::str::FromStr; + +impl Solution { + pub fn convert_date_to_binary(date: String) -> String { + date.split('-') + .map(|x| u32::from_str(x).unwrap()) + .map(|mut x| { + let mut result = vec![]; + + while x != 0 { + result.push(x % 2); + x = x / 2; + } + + result + .iter() + .rev() + .map(|c| c.to_string()) + .collect::() + }) + .reduce(|acc, e| acc + "-" + e.as_str()) + .unwrap() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3280() { + assert_eq!( + "100000100000-10-11101".to_owned(), + Solution::convert_date_to_binary("2080-02-29".to_owned()) + ); + assert_eq!( + "11101101100-1-1".to_owned(), + Solution::convert_date_to_binary("1900-01-01".to_owned()) + ); + } +}