mirror of
https://github.com/SinTan1729/unscrambler-rust.git
synced 2025-04-11 05:46:05 -05:00
Initial commit
This commit is contained in:
commit
d5fafe20bd
6 changed files with 86 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "unscrambler"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "unscrambler"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1
src/data/wordlist.txt
Normal file
1
src/data/wordlist.txt
Normal file
File diff suppressed because one or more lines are too long
1
src/data/wordlist_sorted.txt
Normal file
1
src/data/wordlist_sorted.txt
Normal file
File diff suppressed because one or more lines are too long
68
src/main.rs
Normal file
68
src/main.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use std::io::{self, Write};
|
||||
|
||||
fn main() {
|
||||
// read the dictionary files
|
||||
let wordlist = include_str!("data/wordlist.txt");
|
||||
let wordlist_sorted = include_str!("data/wordlist_sorted.txt");
|
||||
|
||||
// get into a loop so that we can run it multiple times
|
||||
loop {
|
||||
// get input
|
||||
let mut input = String::new();
|
||||
print!("Please enter the scrambled word: ");
|
||||
io::stdout().flush().unwrap();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
input = input.trim().to_string().to_lowercase();
|
||||
match input.find(" ") {
|
||||
None => (),
|
||||
_ => {
|
||||
println!("Please enter only one scrambled word.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// process input and sort it
|
||||
let input = &input[..];
|
||||
let mut broken_input: Vec<char> = input.chars().collect();
|
||||
broken_input.sort_by(|a, b| a.cmp(b));
|
||||
let input = &[" ", &String::from_iter(broken_input)[..], " "].join("")[..];
|
||||
|
||||
// find in dictionary
|
||||
let indices: Vec<usize> = wordlist_sorted
|
||||
.match_indices(input)
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
|
||||
// print output
|
||||
if indices.len() == 0 {
|
||||
println!("No matches found!");
|
||||
std::process::exit(1);
|
||||
} else {
|
||||
println!("The matched words are:");
|
||||
for index in indices {
|
||||
println!(
|
||||
"{}",
|
||||
sentence_case(&wordlist[index + 1..index + input.len() - 1])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ask if we want to go again
|
||||
print!("Would you like to do it again? (y/N)");
|
||||
io::stdout().flush().unwrap();
|
||||
let mut responce = String::new();
|
||||
io::stdin().read_line(&mut responce).unwrap();
|
||||
if !(responce.contains("Y") || responce.contains("y")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// function for conversion into sentence case
|
||||
fn sentence_case(s: &str) -> String {
|
||||
let mut c = s.chars();
|
||||
match c.next() {
|
||||
None => String::new(),
|
||||
Some(f) => f.to_uppercase().collect::<String>() + &c.as_str()[..].to_lowercase(),
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue