Skip to content

Commit 7949f45

Browse files
authored
fix(rust-quest03): re-added removed subjects
1 parent 08c882d commit 7949f45

File tree

8 files changed

+215
-0
lines changed

8 files changed

+215
-0
lines changed

solutions/is_anagram/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "is_anagram"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

solutions/is_anagram/src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub fn is_anagram(s1: &str, s2: &str) -> bool {
2+
// Early return if the lengths of the strings are different
3+
if s1.len() != s2.len() {
4+
return false;
5+
}
6+
7+
let mut char_count = std::collections::HashMap::new();
8+
9+
// Count character occurrences in the first string
10+
for c in s1.to_lowercase().chars() {
11+
*char_count.entry(c).or_insert(0) += 1;
12+
}
13+
14+
// Decrement character occurrences for the second string
15+
for c in s2.to_lowercase().chars() {
16+
if let Some(count) = char_count.get_mut(&c) {
17+
*count -= 1;
18+
if *count < 0 {
19+
return false; // Character count mismatch
20+
}
21+
} else {
22+
return false; // Character not found in the first string
23+
}
24+
}
25+
26+
// Check if all character counts are zero (anagrams)
27+
char_count.values().all(|&count| count == 0)
28+
}

solutions/strings/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "strings"
3+
version = "0.1.0"
4+
authors = ["Augusto <[email protected]>"]
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

solutions/strings/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub fn char_length(s: &str) -> usize {
2+
let mut chars = 0;
3+
for _ in s.chars() {
4+
chars += 1;
5+
}
6+
chars
7+
}
8+
9+
#[cfg(test)]
10+
mod test {
11+
use super::*;
12+
13+
#[test]
14+
fn test_ascii() {
15+
let s = "ascii";
16+
assert_eq!(char_length(s), 5);
17+
}
18+
19+
#[test]
20+
fn test_emoji() {
21+
let s = "❤😍";
22+
assert_eq!(char_length(s), 2);
23+
}
24+
#[test]
25+
fn test_chinese_char() {
26+
let s = "形声字";
27+
assert_eq!(char_length(s), 3);
28+
}
29+
}

tests/is_anagram_test/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "is_anagram_test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
is_anagram = { path = "../../solutions/is_anagram" }

tests/is_anagram_test/src/main.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use is_anagram::*;
2+
3+
fn main() {
4+
println!("{}", is_anagram("listen", "silent"));
5+
println!("{}", is_anagram("RaceCar", "racecar"));
6+
println!("{}", is_anagram("hello world", "world hello"));
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use is_anagram::*;
12+
13+
#[test]
14+
fn basic_anagram() {
15+
let s1 = "listen";
16+
let s2 = "silent";
17+
assert!(is_anagram(s1, s2));
18+
}
19+
20+
#[test]
21+
fn anagram_with_different_case() {
22+
let s1 = "RaceCar";
23+
let s2 = "racecar";
24+
assert!(is_anagram(s1, s2));
25+
}
26+
27+
#[test]
28+
fn anagram_with_spaces() {
29+
let s1 = "hello world";
30+
let s2 = "world hello";
31+
assert!(is_anagram(s1, s2));
32+
}
33+
34+
#[test]
35+
fn non_anagram_with_different_lengths() {
36+
let s1 = "abc";
37+
let s2 = "abcd";
38+
assert!(!is_anagram(s1, s2));
39+
}
40+
41+
#[test]
42+
fn non_anagram_with_different_characters() {
43+
let s1 = "hello";
44+
let s2 = "world";
45+
assert!(!is_anagram(s1, s2));
46+
}
47+
48+
#[test]
49+
fn anagram_with_special_characters() {
50+
let s1 = "!#%@";
51+
let s2 = "@%#!";
52+
assert!(is_anagram(s1, s2));
53+
}
54+
55+
#[test]
56+
fn empty_strings() {
57+
let s1 = "";
58+
let s2 = "";
59+
assert!(is_anagram(s1, s2));
60+
}
61+
62+
#[test]
63+
fn test_anagram_repeating_characters() {
64+
let s1 = "aab";
65+
let s2 = "baa";
66+
assert!(is_anagram(s1, s2));
67+
}
68+
69+
#[test]
70+
fn non_anagram_with_one_characters() {
71+
let s1 = "a";
72+
let s2 = "b";
73+
assert!(!is_anagram(s1, s2));
74+
}
75+
76+
#[test]
77+
fn anagram_with_unicode_characters() {
78+
let s1 = "🙂😀";
79+
let s2 = "😀🙂";
80+
assert!(is_anagram(s1, s2));
81+
}
82+
}

tests/strings_test/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "strings_test"
3+
version = "0.1.0"
4+
authors = ["Augusto <[email protected]>"]
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
strings = { path = "../../solutions/strings" }

tests/strings_test/src/main.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Write a function that receives a string slice and returns the
2+
// length of character of the string
3+
4+
use strings::*;
5+
6+
fn main() {
7+
println!("length of {} = {}", "❤", "❤".len());
8+
println!("length of {} = {}", "❤", char_length("❤"));
9+
println!("length of {} = {}", "形声字", char_length("形聲字"));
10+
println!("length of {} = {}", "形声字", "形聲字".len());
11+
println!("length of {} = {}", "change", "change".len());
12+
println!("length of {} = {}", "change", char_length("change"));
13+
println!("char length of {} = {}", "😍", char_length("😍"));
14+
}
15+
16+
// fn char_length(s: &str) -> usize {
17+
// let mut chars = 0;
18+
// for _ in s.chars() {
19+
// chars += 1;
20+
// }
21+
// chars
22+
// }
23+
24+
#[test]
25+
fn test_ascii() {
26+
let s = "ascii";
27+
assert_eq!(char_length(s), 5);
28+
}
29+
30+
#[test]
31+
fn test_emoji() {
32+
let s = "❤😍";
33+
assert_eq!(char_length(s), 2);
34+
}
35+
36+
#[test]
37+
fn test_chinese_char() {
38+
let s = "形声字";
39+
assert_eq!(char_length(s), 3);
40+
}

0 commit comments

Comments
 (0)