| Crates.io | number_words |
| lib.rs | number_words |
| version | 0.0.5 |
| created_at | 2014-11-21 04:39:31.601272+00 |
| updated_at | 2026-01-16 04:38:27.698459+00 |
| description | Parse a number into possible words |
| homepage | https://github.com/FranklinChen/number-words-rust |
| repository | https://github.com/FranklinChen/number-words-rust |
| max_upload_size | |
| id | 214 |
| size | 42,813 |
A Rust crate that explores different algorithmic strategies to solve the number word problem.
The problem involves parsing a string of digits into all possible corresponding words based on a given mapping (e.g., 1 -> 'A', 2 -> 'B', ... 26 -> 'Z').
For example, the input "123" can be parsed as:
This crate provides three different parser implementations, each with distinct performance characteristics, serving as a pedagogical example of optimization in Rust.
NaiveParser)The original implementation using a functional approach with VecDeque.
DfsParser)A performance-optimized implementation using Depth-First Search with a single mutable buffer.
MemoizedParser)A hybrid approach using Dynamic Programming to precompute the solution graph and solution counts.
use number_words::{default_config, NumberParser, DfsParser};
fn main() {
// 1. Create a configuration (standard A=1..Z=26)
let config = default_config();
// 2. Instantiate a parser (e.g., DfsParser)
let parser = DfsParser::new(&config);
// 3. Parse a digit string
let results = parser.parse("1234").expect("Valid input");
// Output: ["ABCD", "AWD", "LCD"]
for word in results {
println!("{}", word);
}
}
To build the project in release mode:
cargo build --release
To run the test suite:
cargo test
This project uses divan for benchmarking. To run benchmarks:
cargo bench
Benchmarks were run on an ambiguous input ("1" repeated $N$ times) which triggers the worst-case exponential growth of solutions.
| Input Length (N) | NaiveParser | DfsParser | MemoizedParser | Speedup (Memo vs Naive) |
|---|---|---|---|---|
| 10 | 30.1 µs | 7.6 µs | 3.0 µs | 10x |
| 20 | 4.4 ms | 0.8 ms | 0.25 ms | 17x |
| 25 | 58.3 ms | 8.6 ms | 2.7 ms | 21x |
| 30 | N/A | 100.3 ms | 32.3 ms | - |
VecDeque allocations.This project was significantly improved with the assistance of Gemini, an AI by Google. Gemini was used to:
thiserror.DfsParser, MemoizedParser).Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.