summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Cargo.lock12
-rw-r--r--Cargo.toml4
-rw-r--r--libs/restaurant/Cargo.toml6
-rw-r--r--libs/restaurant/src/lib.rs57
-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
8 files changed, 237 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 264ca5d..91a9af9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -68,6 +68,10 @@ dependencies = [
]
[[package]]
+name = "hashmaps_01"
+version = "0.1.0"
+
+[[package]]
name = "hello_cargo"
version = "0.1.0"
@@ -86,6 +90,10 @@ name = "loops"
version = "0.1.0"
[[package]]
+name = "piglatin"
+version = "0.1.0"
+
+[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -113,6 +121,10 @@ name = "rectangles"
version = "0.1.0"
[[package]]
+name = "restaurant"
+version = "0.1.0"
+
+[[package]]
name = "shadowing"
version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
index d8bba20..089c7bb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,5 +12,9 @@ members = [
"src/fibonacci",
"src/christmas_song",
"src/rectangles",
+ "src/hashmaps_01",
+ "src/piglatin",
+
+ "libs/restaurant",
]
resolver = "2"
diff --git a/libs/restaurant/Cargo.toml b/libs/restaurant/Cargo.toml
new file mode 100644
index 0000000..8e47b7a
--- /dev/null
+++ b/libs/restaurant/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "restaurant"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/libs/restaurant/src/lib.rs b/libs/restaurant/src/lib.rs
new file mode 100644
index 0000000..ca35d43
--- /dev/null
+++ b/libs/restaurant/src/lib.rs
@@ -0,0 +1,57 @@
+mod front_of_house;
+
+mod back_of_house {
+ pub struct Breakfast {
+ pub toast: String,
+ seasonal_fruit: String,
+ }
+
+ impl Breakfast {
+ pub fn summer(toast: &str) -> Breakfast {
+ Breakfast {
+ toast: String::from(toast),
+ seasonal_fruit: String::from("peaches"),
+ }
+ }
+ // just to test reading capabilities and resolve
+ // "field never read" warning
+ pub fn components(&self) {
+ let v = vec![1, 2, 3, 4];
+
+ let does_not_exist = &v[100];
+ let does_not_exist = v.get(100);
+ println!("toast: {}, seasonal fruit: {}",
+ self.toast, self.seasonal_fruit)
+ }
+ }
+}
+
+pub fn add(left: u64, right: u64) -> u64 {
+ left + right
+}
+
+pub fn eat_at_restaurant() {
+ // Order a breakfast in the summer with Rye toast.
+ let mut meal = back_of_house::Breakfast::summer("Rye");
+ // Change our mind about what bread we'd like.
+ meal.toast = String::from("Wheat");
+ println!("I'd like {} toast please", meal.toast);
+
+ meal.components();
+ crate::front_of_house::hosting::add_to_waitlist();
+
+ // The next line won't compile if we uncomment it; we're not allowed
+ // to see or modify the seasonal fruit that comes with the meal.
+ // meal.seasonal_fruit = String::from("blueberries");
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn it_works() {
+ let result = add(2, 2);
+ assert_eq!(result, 4);
+ }
+}
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)
+}