|
1 | | -/// Performs long multiplication on string representations of non-negative numbers. |
| 1 | +/// Performs long multiplication on string representations of non-negative base-10 integers. |
| 2 | +/// |
| 3 | +/// # Panics |
| 4 | +/// Panics if either input contains non-ASCII digits, leading zeros (except "0"), |
| 5 | +/// or is empty. |
| 6 | +/// |
| 7 | +/// # Examples |
| 8 | +/// ``` |
| 9 | +/// use crate::big_integer::multiply; |
| 10 | +/// assert_eq!(multiply("123", "456"), "56088"); |
| 11 | +/// assert_eq!(multiply("99", "99"), "9801"); |
| 12 | +/// ``` |
2 | 13 | pub fn multiply(num1: &str, num2: &str) -> String { |
3 | | - if !is_valid_nonnegative(num1) || !is_valid_nonnegative(num2) { |
4 | | - panic!("String does not conform to specification") |
| 14 | + if !is_valid_nonnegative(num1) { |
| 15 | + panic!("Invalid input: `{num1}` is not a valid non-negative integer string"); |
| 16 | + } |
| 17 | + if !is_valid_nonnegative(num2) { |
| 18 | + panic!("Invalid input: `{num2}` is not a valid non-negative integer string"); |
5 | 19 | } |
6 | 20 |
|
7 | 21 | if num1 == "0" || num2 == "0" { |
8 | 22 | return "0".to_string(); |
9 | 23 | } |
10 | | - let output_size = num1.len() + num2.len(); |
11 | 24 |
|
| 25 | + let output_size = num1.len() + num2.len(); |
12 | 26 | let mut mult = vec![0; output_size]; |
| 27 | + |
13 | 28 | for (i, c1) in num1.chars().rev().enumerate() { |
14 | 29 | for (j, c2) in num2.chars().rev().enumerate() { |
15 | 30 | let mul = c1.to_digit(10).unwrap() * c2.to_digit(10).unwrap(); |
16 | | - // It could be a two-digit number here. |
17 | | - mult[i + j + 1] += (mult[i + j] + mul) / 10; |
18 | | - // Handling rounding. Here's a single digit. |
19 | | - mult[i + j] = (mult[i + j] + mul) % 10; |
| 31 | + let sum = mult[i + j] + mul; |
| 32 | + |
| 33 | + mult[i + j + 1] += sum / 10; |
| 34 | + mult[i + j] = sum % 10; |
20 | 35 | } |
21 | 36 | } |
| 37 | + |
22 | 38 | if mult[output_size - 1] == 0 { |
23 | 39 | mult.pop(); |
24 | 40 | } |
25 | 41 | mult.iter().rev().map(|&n| n.to_string()).collect() |
26 | 42 | } |
27 | 43 |
|
| 44 | +/// Checks whether a string represents a valid non-negative base-10 integer. |
| 45 | +/// Disallows empty strings, leading zeros (except "0"), and non-ASCII digits. |
28 | 46 | pub fn is_valid_nonnegative(num: &str) -> bool { |
29 | | - num.chars().all(char::is_numeric) && !num.is_empty() && (!num.starts_with('0') || num == "0") |
| 47 | + !num.is_empty() |
| 48 | + && num.chars().all(|c| c.is_ascii_digit()) |
| 49 | + && (!num.starts_with('0') || num == "0") |
30 | 50 | } |
31 | 51 |
|
32 | 52 | #[cfg(test)] |
33 | 53 | mod tests { |
34 | 54 | use super::*; |
| 55 | + |
35 | 56 | macro_rules! test_multiply { |
36 | 57 | ($($name:ident: $inputs:expr,)*) => { |
37 | | - $( |
38 | | - #[test] |
39 | | - fn $name() { |
40 | | - let (s, t, expected) = $inputs; |
41 | | - assert_eq!(multiply(s, t), expected); |
42 | | - assert_eq!(multiply(t, s), expected); |
43 | | - } |
44 | | - )* |
| 58 | + $( |
| 59 | + #[test] |
| 60 | + fn $name() { |
| 61 | + let (s, t, expected) = $inputs; |
| 62 | + assert_eq!(multiply(s, t), expected, "multiply({s}, {t})"); |
| 63 | + assert_eq!(multiply(t, s), expected, "multiply({t}, {s})"); |
| 64 | + } |
| 65 | + )* |
45 | 66 | } |
46 | 67 | } |
47 | 68 |
|
48 | 69 | test_multiply! { |
49 | | - multiply0: ("2", "3", "6"), |
50 | | - multiply1: ("123", "456", "56088"), |
| 70 | + multiply_small: ("2", "3", "6"), |
| 71 | + multiply_basic: ("123", "456", "56088"), |
51 | 72 | multiply_zero: ("0", "222", "0"), |
52 | | - other_1: ("99", "99", "9801"), |
53 | | - other_2: ("999", "99", "98901"), |
54 | | - other_3: ("9999", "99", "989901"), |
55 | | - other_4: ("192939", "9499596", "1832842552644"), |
| 73 | + multiply_double_digits: ("99", "99", "9801"), |
| 74 | + multiply_triple_digits: ("999", "99", "98901"), |
| 75 | + multiply_four_digits: ("9999", "99", "989901"), |
| 76 | + multiply_large: ("192939", "9499596", "1832842552644"), |
56 | 77 | } |
57 | 78 |
|
58 | 79 | macro_rules! test_multiply_with_wrong_input { |
59 | 80 | ($($name:ident: $inputs:expr,)*) => { |
60 | | - $( |
61 | | - #[test] |
62 | | - #[should_panic] |
63 | | - fn $name() { |
64 | | - let (s, t) = $inputs; |
65 | | - multiply(s, t); |
66 | | - } |
67 | | - )* |
| 81 | + $( |
| 82 | + #[test] |
| 83 | + #[should_panic] |
| 84 | + fn $name() { |
| 85 | + let (s, t) = $inputs; |
| 86 | + multiply(s, t); |
| 87 | + } |
| 88 | + )* |
68 | 89 | } |
69 | 90 | } |
| 91 | + |
70 | 92 | test_multiply_with_wrong_input! { |
71 | 93 | empty_input: ("", "121"), |
72 | 94 | leading_zero: ("01", "3"), |
|
0 commit comments