feat: Solve 1.1
This commit is contained in:
parent
f450e6c5f0
commit
a47f7e1197
7 changed files with 493 additions and 6 deletions
3
src/chapter_1.rs
Normal file
3
src/chapter_1.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mod error;
|
||||
mod problem_1;
|
||||
pub use problem_1::is_permutation;
|
||||
9
src/chapter_1/error.rs
Normal file
9
src/chapter_1/error.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use thiserror::Error;
|
||||
|
||||
pub type Chapter1Result<T> = Result<T, Chapter1Error>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Chapter1Error {
|
||||
#[error("Failed to find a match")]
|
||||
Problem1Error,
|
||||
}
|
||||
53
src/chapter_1/problem_1.rs
Normal file
53
src/chapter_1/problem_1.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use tracing::debug;
|
||||
|
||||
use super::error::{Chapter1Error::Problem1Error, Chapter1Result};
|
||||
|
||||
/// Check Permutation: Given two strings, write a method to decide if one is a permutation of the other
|
||||
pub fn is_permutation(string: &str, string2: &str) -> Chapter1Result<()> {
|
||||
let string = String::from(string);
|
||||
let mut string2 = String::from(string2);
|
||||
for char1 in string.chars() {
|
||||
debug!("char 1: {char1}");
|
||||
eliminate(char1, &mut string2)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn eliminate(char1: char, string2: &mut String) -> Chapter1Result<()> {
|
||||
for (idx, char2) in string2.chars().enumerate() {
|
||||
debug!("char 2: {char2}");
|
||||
if char1 == char2 {
|
||||
debug!("match remove");
|
||||
string2.remove(idx);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(Problem1Error)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_problem_1 {
|
||||
use super::*;
|
||||
use tracing_test::traced_test;
|
||||
|
||||
#[traced_test]
|
||||
#[test]
|
||||
fn test_guh() {
|
||||
assert!(is_permutation("guh", "hug").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_whale() {
|
||||
assert!(is_permutation("whale", "lewah").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zeb() {
|
||||
assert!(is_permutation("zeb", "bera").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bez() {
|
||||
assert!(is_permutation("szeb", "bez").is_err());
|
||||
}
|
||||
}
|
||||
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
mod chapter_1;
|
||||
pub use chapter_1::is_permutation;
|
||||
25
src/main.rs
25
src/main.rs
|
|
@ -1,8 +1,21 @@
|
|||
fn main() {
|
||||
use ctci::is_permutation;
|
||||
use tracing_error::ErrorLayer;
|
||||
use tracing_subscriber::{EnvFilter, prelude::*};
|
||||
|
||||
|
||||
use color_eyre::eyre::{WrapErr, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.with(ErrorLayer::default())
|
||||
.init();
|
||||
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
// Is Unique: Implement an algorithm to determine if a string has all unique characters.
|
||||
// What if you cannot use additional data structures?
|
||||
|
||||
// Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
|
||||
is_permutation("hello", "olleh")
|
||||
.wrap_err("Failed permutation")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue