You've already forked rust-tutor
first exercise!
- create exercise vec statistics calculator (shout out claude) - finish hacky solution (it works... but at what cost) - update current state
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,2 +1,7 @@
|
|||||||
|
/rust-book
|
||||||
context.md
|
context.md
|
||||||
ideas.md
|
ideas.md
|
||||||
|
**/target/
|
||||||
|
**/Cargo.lock
|
||||||
|
**/*.rs.bk
|
||||||
|
.DS_Store
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
# Current Learning State
|
# Current Learning State
|
||||||
|
|
||||||
## Position
|
## Position
|
||||||
**Book:** Rust Book (Brown University edition) - Chapter 7 (Modules)
|
**Book:** Rust Book (Brown University edition) - Chapter 8 (Collections) ✅
|
||||||
**Status:** Ready to start Module exercises
|
**Status:** Ready for Collections exercises or Chapter 9
|
||||||
**Last Updated:** 2026-01-12
|
**Last Updated:** 2026-01-14
|
||||||
|
|
||||||
## Recent Concepts Covered
|
## Recent Concepts Covered
|
||||||
- Variables and mutability
|
- Variables and mutability
|
||||||
@@ -12,6 +12,8 @@
|
|||||||
- Ownership and borrowing
|
- Ownership and borrowing
|
||||||
- Structs and enums
|
- Structs and enums
|
||||||
- Pattern matching
|
- Pattern matching
|
||||||
|
- Modules and privacy
|
||||||
|
- Collections (Vec, String, HashMap)
|
||||||
|
|
||||||
## Current Understanding Level
|
## Current Understanding Level
|
||||||
- **Ownership:** Ambivalent (normal at this stage) - needs more practice
|
- **Ownership:** Ambivalent (normal at this stage) - needs more practice
|
||||||
|
|||||||
10
MANUAL.md
10
MANUAL.md
@@ -2,15 +2,7 @@
|
|||||||
|
|
||||||
## Quick Start (First Time)
|
## Quick Start (First Time)
|
||||||
|
|
||||||
1. **Initialize Git (if not done)**
|
1. **Start Your First Exercise**
|
||||||
```bash
|
|
||||||
cd /Users/real/Lab/now/rust/rust-tutor
|
|
||||||
git init
|
|
||||||
git add .
|
|
||||||
git commit -m "Initial learning scaffolding setup"
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Start Your First Exercise**
|
|
||||||
- Open `NEW_CHAT_TEMPLATE.md`
|
- Open `NEW_CHAT_TEMPLATE.md`
|
||||||
- Copy the template
|
- Copy the template
|
||||||
- Start new Claude Code chat
|
- Start new Claude Code chat
|
||||||
|
|||||||
6
exercises/vec-stats/Cargo.toml
Normal file
6
exercises/vec-stats/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "vec-stats"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
71
exercises/vec-stats/src/main.rs
Normal file
71
exercises/vec-stats/src/main.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
fn main() {
|
||||||
|
let test_vec = vec![1, 2, 2, 2, 2, 3, 4, 5]; // should return (3.0, 3, 3)
|
||||||
|
|
||||||
|
println!("{:?}", calculate_stats(test_vec));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Stats {
|
||||||
|
mean: f64,
|
||||||
|
median: i32,
|
||||||
|
mode: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_stats(numbers: Vec<i32>) -> Stats {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
let mut tmp: Vec<i32> = numbers;
|
||||||
|
let len = tmp.len();
|
||||||
|
tmp.sort();
|
||||||
|
|
||||||
|
let mut median: i32 = 0;
|
||||||
|
|
||||||
|
// median
|
||||||
|
if len % 2 != 0 {
|
||||||
|
median = tmp[len / 2];
|
||||||
|
} else {
|
||||||
|
median = (tmp[len / 2] + tmp[(len / 2) - 1]) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mean
|
||||||
|
let mut sum: i32 = 0;
|
||||||
|
|
||||||
|
let tmp2 = tmp.clone();
|
||||||
|
for i in tmp2 {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mean = sum as f64 / len as f64;
|
||||||
|
|
||||||
|
//mode
|
||||||
|
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
|
||||||
|
let tmp3 = tmp.clone();
|
||||||
|
|
||||||
|
for num in tmp3 {
|
||||||
|
let count = map.entry(num).or_insert(0);
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tmp4 = tmp.clone();
|
||||||
|
|
||||||
|
let mut mode_calc = *map.get(&1).unwrap();
|
||||||
|
|
||||||
|
let mut mode: i32 = 0;
|
||||||
|
|
||||||
|
for (k, v) in map {
|
||||||
|
if v > mode_calc {
|
||||||
|
mode = k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?}", tmp4);
|
||||||
|
|
||||||
|
let return_value = Stats {
|
||||||
|
mean: mean,
|
||||||
|
median: median,
|
||||||
|
mode: mode,
|
||||||
|
};
|
||||||
|
|
||||||
|
return_value
|
||||||
|
}
|
||||||
26
exercises/vec-stats/vec-stats-exercise.md
Normal file
26
exercises/vec-stats/vec-stats-exercise.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Exercise: Vec Statistics Calculator
|
||||||
|
|
||||||
|
**Concepts practiced:** Vec operations, HashMap for counting, basic math functions
|
||||||
|
**Objective:** Build a simple stats calculator that takes a list of numbers and returns basic statistics
|
||||||
|
|
||||||
|
## Requirements:
|
||||||
|
- Create a function `calculate_stats(numbers: Vec<i32>) -> (f64, i32, i32)` that returns (mean, median, mode)
|
||||||
|
- Use Vec sorting for median calculation
|
||||||
|
- Use HashMap for mode calculation (most frequent number)
|
||||||
|
- Handle edge cases (empty vec should panic with clear message)
|
||||||
|
|
||||||
|
## Success criteria:
|
||||||
|
- Code compiles and runs
|
||||||
|
- Test with `vec![1, 2, 3, 3, 4, 5]` returns approximately `(3.0, 3, 3)`
|
||||||
|
|
||||||
|
## Stretch goal:
|
||||||
|
Return a struct instead of tuple for better readability
|
||||||
|
|
||||||
|
## Getting Started:
|
||||||
|
1. `cd exercises`
|
||||||
|
2. `cargo new vec-stats`
|
||||||
|
3. `cd vec-stats`
|
||||||
|
4. Implement the function in `src/main.rs` or `src/lib.rs`
|
||||||
|
5. Test with the example input
|
||||||
|
|
||||||
|
Expected time: 30-45 minutes
|
||||||
Reference in New Issue
Block a user