finish access part

This commit is contained in:
lilymonade 2025-03-12 00:15:24 +01:00
parent c52caca844
commit 1b850fc73e
Signed by: lilymonade
GPG Key ID: F8967EC454DBDCB6
5 changed files with 122 additions and 20 deletions

View File

@ -44,3 +44,8 @@ exercises:
tests:
- "add_last_two_not_enough"
- "add_last_two_enough"
- "dup_top_empty"
- "dup_top_has_value"
- "median_already_sorted"
- "median_shuffled"
- "median_empty"

View File

@ -1,10 +1,37 @@
/// Add the last two numbers of the input slice.
///
/// If the slice is not large enough, return `None`
/// If it is, return the computed value in a `Some`
/// # Return value
/// `None` if the slice is not large enough
/// `Some(result)` if the slice has at least 2 elements
pub fn add_last_two(v: &[f32]) -> Option<f32> {
match v.last_chunk() {
Some([a, b]) => Some(a + b),
None => None,
}
}
/// Duplicate the top element from the stack if it exist
/// (the stack is represented as a Vec with top == last)
///
/// # Return value
/// `Some(())` if the operation succeeded
/// `None` if not
pub fn dup_top(v: &mut Vec<f32>) -> Option<()> {
match v.last() {
Some(last) => {
v.push(*last);
return Some(());
}
_ => return None,
}
}
/// Compute the median of a slice in place (if the slice was sorted, it would be the middle element)
pub fn median(v: &[i32]) -> Option<i32> {
let mut tmp = v.to_vec();
tmp.sort();
match tmp.get(tmp.len() / 2) {
Some(&r) => Some(r),
None => None,
}
}

View File

@ -9,3 +9,31 @@ pub fn add_last_two_not_enough() {
pub fn add_last_two_enough() {
assert_eq!(access::add_last_two(&[1.0, 2.0, 3.0]), Some(5.0));
}
#[test]
pub fn dup_top_empty() {
let mut empty = vec![];
assert!(access::dup_top(&mut empty).is_none());
}
#[test]
pub fn dup_top_has_values() {
let mut empty = vec![1.0, 2.0];
assert!(access::dup_top(&mut empty).is_some());
assert_eq!(empty, &[1.0, 2.0, 2.0]);
}
#[test]
pub fn median_already_sorted() {
assert_eq!(access::median(&[1, 2, 3]), Some(2));
}
#[test]
pub fn median_shuffled() {
assert_eq!(access::median(&[420, 69, 128]), Some(128));
}
#[test]
pub fn median_empty() {
assert_eq!(access::median(&[]), None);
}

View File

@ -0,0 +1,60 @@
---
name = "Accessing values"
file = "src/vec/access.rs"
---
Instead of using the good old C-style bound checking:
```rust
if vec.len() < 1 {
return None;
} else {
// compiler still thinks this line can panic
return vec[0];
}
```
Try to implement these functions using non-panicking methods like [`last`](https://doc.rust-lang.org/std/primitive.slice.html#method.last), [`last_chunk`](https://doc.rust-lang.org/std/primitive.slice.html#method.last_chunk), or [`get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get).
```prototype
/// Add the last two numbers of the input slice.
///
/// # Return value
/// `None` if the slice is not large enough
/// `Some(result)` if the slice has at least 2 elements
pub fn add_last_two(v: &[f32]) -> Option<f32> {
unimplemented!()
}
/// Duplicate the top element from the stack if it exist
/// (the stack is represented as a Vec with top == last)
///
/// # Return value
/// `Some(())` if the operation succeeded
/// `None` if not
pub fn dup_top(v: &mut Vec<f32>) -> Option<()> {
unimplemented!()
}
/// Compute the median of a slice in place (if the slice was sorted, it would be the middle element)
pub fn median(v: &[i32]) -> Option<i32> {
unimplemented!()
}
```
```example
fn main() {
assert_eq!(add_last_two(&[]), None);
assert_eq!(add_last_two(&[10.0]), None);
assert_eq!(add_last_two(&[1.0, 2.0, 3.0]), Some(5.0));
let mut stack = vec![1.0];
assert!(dup_top(&mut stack).is_some());
assert_eq!(&stack, &[1.0, 1.0]);
stack.clear();
assert!(dup_top(&mut stack).is_none());
assert_eq!(median(&[2, 1, 3]), Some(2));
}
```

View File

@ -14,21 +14,3 @@ Slices (`[T]`) represent some memory space containing an arbitrary number of ele
`Vec<T>` can be seen as [owned](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html) `[T]`, it means that every function working on a `&[T]` can work on a `&Vec<T>`.
```
```prototype
/// Add the last two numbers of the input slice.
///
/// If the slice is not large enough, return `None`
/// If it is, return the computed value in a `Some`
pub fn add_last_two(v: &[f32]) -> Option<f32> {
unimplemented!()
}
```
```example
fn main() {
assert_eq!(add_last_two(&[]), None);
assert_eq!(add_last_two(&[10.0]), None);
assert_eq!(add_last_two(&[1.0, 2.0, 3.0]), Some(5.0));
}
```