Skip to content

Commit 5c626e9

Browse files
committed
init commit
0 parents  commit 5c626e9

File tree

11 files changed

+107
-0
lines changed

11 files changed

+107
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rust-coding-challenges"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[workspace]
7+
members = [ "challenges/simple-parser" ]
8+
9+
[dependencies]

challenges/simple-parser/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "simple-parser"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rust-coding-challenges = { path = "../../" }
8+

challenges/simple-parser/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::{BTreeMap, BTreeSet, HashSet};
2+
use std::fs;
3+
use std::io;
4+
5+
pub fn read_file(file_path: &str) -> io::Result<String> {
6+
fs::read_to_string(file_path)
7+
}
8+
9+
pub fn get_lines_lower_case(text: &str) -> Vec<String> {
10+
text.to_lowercase().lines().map(|line| line.to_string()).collect()
11+
}
12+
13+
14+
pub fn solve(content: &str) -> BTreeMap<String, BTreeSet<usize>> {
15+
let mut word_map: BTreeMap<String, BTreeSet<usize>> = BTreeMap::new();
16+
let lines = get_lines_lower_case(content);
17+
18+
for(line_number, line) in lines.iter().enumerate() {
19+
let words: HashSet<&str> = line.split_whitespace().collect();
20+
for word in words {
21+
let word = word.to_string();
22+
word_map.entry(word).or_insert_with(BTreeSet::new).insert(line_number + 1);
23+
}
24+
}
25+
26+
word_map
27+
}

challenges/simple-parser/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::io;
2+
use simple_parser::solve;
3+
use rust_coding_challenges::utils::read_text_file_from_args;
4+
fn main() -> io::Result<()> {
5+
let content = read_text_file_from_args()?;
6+
let word_map = solve(&content);
7+
8+
for (word, lines) in word_map {
9+
println!("Word: '{}', Lines: {:?}", word, lines);
10+
}
11+
Ok(())
12+
}

challenges/simple-parser/tests/test_simple-parser.rs

Whitespace-only changes.

scripts/create_challenge.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Create the challenge folder structure
4+
CHALLENGE_NAME=$1
5+
if [ -z "$CHALLENGE_NAME" ]; then
6+
echo "Please provide the challenge name."
7+
exit 1
8+
fi
9+
10+
# Create a new Cargo project for the challenge
11+
cargo new "challenges/$CHALLENGE_NAME"
12+
13+
# Create the tests folder
14+
mkdir "challenges/$CHALLENGE_NAME/tests"
15+
16+
# Create a test file in the tests folder
17+
touch "challenges/$CHALLENGE_NAME/tests/test_${CHALLENGE_NAME}.rs"
18+
19+
echo "Challenge structure created for $CHALLENGE_NAME."

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod utils;

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
fn main() {
3+
println!("Hello, world!");
4+
}

src/utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::env;
2+
use std::fs;
3+
use std::io;
4+
5+
pub fn read_text_file_from_args() -> io::Result<String> {
6+
let args: Vec<String> = env::args().collect();
7+
if args.len() < 2 {
8+
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Usage: <file_path>"));
9+
}
10+
let file_path = &args[1];
11+
fs::read_to_string(file_path)
12+
}

0 commit comments

Comments
 (0)