summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorhybrid <hybrid@hybridlabs.cc>2026-10-14 21:33:16 +0300
committerhybrid <hybrid@hybridlabs.cc>2026-10-14 21:33:16 +0300
commit43ddbb814e679bf2adde3a8602624c207641dca2 (patch)
treeb2cc74f6827381bc29608a3143f6131b19477535 /src
parent84fe604d3492420821e7afc0772c6d2fb4c2e046 (diff)
downloadrust-book-43ddbb814e679bf2adde3a8602624c207641dca2.tar.gz
rust-book-43ddbb814e679bf2adde3a8602624c207641dca2.tar.bz2
rust-book-43ddbb814e679bf2adde3a8602624c207641dca2.zip
add: libs/restaurant, src/{hashmaps_01, piglatin}
Diffstat (limited to 'src')
-rw-r--r--src/hashmaps_01/Cargo.toml6
-rw-r--r--src/hashmaps_01/src/main.rs79
-rw-r--r--src/piglatin/Cargo.toml6
-rw-r--r--src/piglatin/src/main.rs67
4 files changed, 158 insertions, 0 deletions
diff --git a/src/hashmaps_01/Cargo.toml b/src/hashmaps_01/Cargo.toml
new file mode 100644
index 0000000..d02c5c5
--- /dev/null
+++ b/src/hashmaps_01/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "hashmaps_01"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/hashmaps_01/src/main.rs b/src/hashmaps_01/src/main.rs
new file mode 100644
index 0000000..00f1dea
--- /dev/null
+++ b/src/hashmaps_01/src/main.rs
@@ -0,0 +1,79 @@
+/*
+ * Chapter:
+ * 8.3 Storing Keys with assosiated Values in Hash Maps
+ *
+ * Link:
+ * https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#summary
+ *
+ * Task:
+ * Given a list of integers, use a vector and return the average, the median
+ * (when sorted, the value in the middle position) and mode
+ * (the value that occurs most often; a hash map will be helpful here)
+ * of the list.
+ *
+ * Remark:
+ * - Average task is from russian translated version of the book.
+ */
+
+fn main() {
+ let v = vec![
+ 10, 10, 10, 9, 8,
+ 7, 6, 5, 4, 3, 2,
+ 1, 0, 1, 2, 3, 4,
+ 5, 6, 7, 8, 9, 10,
+ ];
+ // sorted: 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
+ // 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 10
+
+ println!("avg: {:.2}", avg(&v));
+ println!("median: {:.2}", median(&v));
+ println!("mode: {}", mode(&v));
+}
+
+fn avg(v: &[i32]) -> f64 {
+ if v.is_empty() {
+ return 0.0;
+ }
+ let mut sum: f64 = 0.0;
+
+ for i in 0..v.len() {
+ sum += v[i] as f64;
+ }
+
+ sum / v.len() as f64
+}
+
+fn median(v: &[i32]) -> i32 {
+ let mut sorted = v.to_vec();
+ sorted.sort();
+
+ let n = sorted.len();
+
+ if n % 2 == 1 {
+ sorted[n / 2]
+ } else {
+ ((sorted[n / 2 - 1] as i64 + sorted[n / 2] as i64) / 2) as i32
+ }
+}
+
+fn mode(v: &[i32]) -> i32 {
+ use std::collections::HashMap;
+
+ let mut counts = HashMap::new();
+
+ for &i in v {
+ let _ = *counts.entry(i).or_insert(0) += 1;
+ }
+
+ let mut max_key = v[0];
+ let mut max_count = 0;
+
+ for (&key, &count) in &counts {
+ if count > max_count {
+ max_key = key;
+ max_count = count;
+ }
+ }
+
+ max_key
+}
diff --git a/src/piglatin/Cargo.toml b/src/piglatin/Cargo.toml
new file mode 100644
index 0000000..725b8b5
--- /dev/null
+++ b/src/piglatin/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "piglatin"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/piglatin/src/main.rs b/src/piglatin/src/main.rs
new file mode 100644
index 0000000..c993c52
--- /dev/null
+++ b/src/piglatin/src/main.rs
@@ -0,0 +1,67 @@
+/*
+ * Chapter:
+ * 8.3 Storing Keys with assosiated Values in Hash Maps
+ *
+ * Link:
+ * https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#summary
+ *
+ * Task:
+ * Convert strings to Pig Latin. The first consonant of each word is moved
+ * to the end of the word and ay is added, so first becomes irst-fay.
+ * Words that start with a vowel have hay added to the end instead
+ * (apple becomes apple-hay). Keep in mind the details about UTF-8 encoding!
+ *
+ * Example input/output:
+ * I: "pig"
+ * O: "igpay"
+ *
+ * I: "latin"
+ * O: "atinlay"
+ *
+ * I: "banana"
+ * O: "ananabay"
+ *
+ */
+
+fn main() {
+ println!("{}", str2pig_latin("1bcdege"));
+ println!("{}", str2pig_latin("pig"));
+ println!("{}", str2pig_latin("world"));
+ println!("{}", str2pig_latin("world of warcraft"));
+}
+
+fn str2pig_latin(input: &str) -> String {
+ let mut output = String::new();
+
+ for word in input.split_whitespace() {
+ if !output.is_empty() {
+ output.push(' ');
+ }
+
+ let end_byte = word
+ .char_indices()
+ .find(|&(_, c)| !is_consonant(c))
+ .map_or(word.len(), |(i, _)| i);
+
+ let (head, tail) = word.split_at(end_byte);
+
+ if head.is_empty() {
+ output.push_str(word);
+ output.push_str("-hay");
+ } else {
+ output.push_str(tail);
+ output.push_str(head);
+ output.push_str("ay");
+ }
+ }
+
+ output
+}
+
+fn is_vowel(c: char) -> bool {
+ matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u')
+}
+
+fn is_consonant(c: char) -> bool {
+ c.is_ascii_alphabetic() && !is_vowel(c)
+}