done with basic calc

This commit is contained in:
2025-02-20 09:59:05 +05:30
parent 57475122a9
commit 7910a25928
9 changed files with 49 additions and 14 deletions

View File

@@ -2,7 +2,13 @@
/// `13` if `n` is divisible by `3`, /// `13` if `n` is divisible by `3`,
/// `17` otherwise. /// `17` otherwise.
fn magic_number(n: u32) -> u32 { fn magic_number(n: u32) -> u32 {
todo!() if n % 2 == 0 {
return 12;
} else if n % 3 == 0 {
return 13;
} else {
return 17;
}
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -2,7 +2,9 @@
/// calculate the average speed of the journey. /// calculate the average speed of the journey.
fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
// TODO: Panic with a custom message if `time_elapsed` is 0 // TODO: Panic with a custom message if `time_elapsed` is 0
if time_elapsed == 0 {
panic!("The journey took no time at all. That's impossible!");
}
(end - start) / time_elapsed (end - start) / time_elapsed
} }

View File

@@ -10,6 +10,14 @@
// //
// Use only what you learned! No loops yet, so you'll have to use recursion! // Use only what you learned! No loops yet, so you'll have to use recursion!
fn factorial(n: u32) -> u32 {
if n == 0 || n == 1 {
return 1;
} else {
return n * factorial(n - 1);
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::factorial; use crate::factorial;

View File

@@ -4,7 +4,16 @@ pub fn factorial(n: u32) -> u32 {
// interprets as "I'll get back to this later", thus // interprets as "I'll get back to this later", thus
// suppressing type errors. // suppressing type errors.
// It panics at runtime. // It panics at runtime.
todo!() if n == 0 || n == 1 {
return 1;
}
let mut i = 1;
let mut prod = 1;
while i <= n {
prod = prod * i;
i = i + 1;
}
return prod;
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -1,6 +1,13 @@
// Rewrite the factorial function using a `for` loop. // Rewrite the factorial function using a `for` loop.
pub fn factorial(n: u32) -> u32 { pub fn factorial(n: u32) -> u32 {
todo!() if n == 0 || n == 1 {
return 1;
}
let mut prod = 1;
for i in 2..=n {
prod = prod * i;
}
prod
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -8,3 +8,5 @@ edition = "2021"
# compiler noise. # compiler noise.
# We'll re-enable them again once we explain how visibility works in Rust. # We'll re-enable them again once we explain how visibility works in Rust.
dead_code = "allow" dead_code = "allow"
[profile.dev.package.copy]
overflow-checks = false

View File

@@ -1,9 +1,10 @@
pub fn factorial(n: u32) -> u32 { pub fn factorial(n: u32) -> u32 {
let mut result = 1; let mut result: u32 = 1;
for i in 1..=n { for i in 1..=n {
// Use saturating multiplication to stop at the maximum value of u32 // Use saturating multiplication to stop at the maximum value of u32
// rather than overflowing and wrapping around // rather than overflowing and wrapping around
result *= i; // result *= i;
result = result.saturating_mul(i);
} }
result result
} }

View File

@@ -6,7 +6,7 @@ mod tests {
#[test] #[test]
fn u16_to_u32() { fn u16_to_u32() {
let v: u32 = todo!(); let v: u32 = 47;
assert_eq!(47u16 as u32, v); assert_eq!(47u16 as u32, v);
} }
@@ -24,14 +24,14 @@ mod tests {
// You could solve this by using exactly the same expression as above, // You could solve this by using exactly the same expression as above,
// but that would defeat the purpose of the exercise. Instead, use a genuine // but that would defeat the purpose of the exercise. Instead, use a genuine
// `i8` value that is equivalent to `255` when converted to `u8`. // `i8` value that is equivalent to `255` when converted to `u8`.
let y: i8 = todo!(); let y: i8 = 255 as _;
assert_eq!(x, y); assert_eq!(x, y);
} }
#[test] #[test]
fn bool_to_u8() { fn bool_to_u8() {
let v: u8 = todo!(); let v: u8 = 1 as _;
assert_eq!(true as u8, v); assert_eq!(true as u8, v);
} }
} }