add initial data
This commit is contained in:
1 parent
048a82b101
commit
51d935d06b
66 files changed
+1329
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,54 @@
|
||||
static ROMAN_VALUES: [(i32, &str); 13] = [
|
||||
(1000, "M"),
|
||||
(900, "CM"),
|
||||
(500, "D"),
|
||||
(400, "CD"),
|
||||
(100, "C"),
|
||||
(90, "XC"),
|
||||
(50, "L"),
|
||||
(40, "XL"),
|
||||
(10, "X"),
|
||||
(9, "IX"),
|
||||
(5, "V"),
|
||||
(4, "IV"),
|
||||
(1, "I"),
|
||||
];
|
||||
|
||||
pub fn int_to_roman(num: i32) -> String {
|
||||
let mut num = num;
|
||||
|
||||
let mut buf = String::new();
|
||||
|
||||
for (subtrahend, letters) in ROMAN_VALUES {
|
||||
while let Some(n) = num.checked_sub(subtrahend)
|
||||
&& n >= 0
|
||||
{
|
||||
num = n;
|
||||
buf.push_str(letters);
|
||||
}
|
||||
}
|
||||
|
||||
buf
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::int_to_roman;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
assert_eq!(int_to_roman(3749), "MMMDCCXLIX");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
assert_eq!(int_to_roman(58), "LVIII");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_3() {
|
||||
assert_eq!(int_to_roman(1994), "MCMXCIV");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user