Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/01_intro/01_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// partner in this course and it'll often guide you in the right direction!
//
// The input parameters should have the same type of the return type.
fn compute(a, b) -> u32 {
fn compute(a:u32, b: u32) -> u32 {
// Don't touch the function body.
a + b * 2
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/00_intro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn intro() -> &'static str {
// TODO: fix me 👇
"I'm ready to __!"
"I'm ready to build a calculator in Rust!"
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/01_integers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn compute(a: u32, b: u32) -> u32 {
// TODO: change the line below to fix the compiler error and make the tests pass.
let multiplier: u8 = 4;
let multiplier: u32 = 4;
a + b * multiplier
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/02_variables/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
// TODO: define a variable named `distance` with the right value to get tests to pass
// Do you need to annotate the type of `distance`? Why or why not?

let distance: u32 = end - start;
// Don't change the line below
distance / time_elapsed
}
Expand Down
14 changes: 13 additions & 1 deletion exercises/02_basic_calculator/03_if_else/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
/// Return `12` if `n` is even,
/// `13` if `n` is divisible by `3`,
/// `17` otherwise.



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)]
Expand Down
11 changes: 9 additions & 2 deletions exercises/02_basic_calculator/04_panics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
/// Given the start and end points of a journey, and the time it took to complete 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

if time_elapsed == 0{

panic!("The journey took no time at all. That's impossible!");
}
else{
(end - start) / time_elapsed
}

(end - start) / time_elapsed
}

#[cfg(test)]
Expand Down
23 changes: 23 additions & 0 deletions exercises/02_basic_calculator/05_factorial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
//
// Use only what you learned! No loops yet, so you'll have to use recursion!

// factorial
// 5! = 5 x (5-1) x (5-2) x (5-3) x (5-4) x (5-5)
// n! = n x (n-1) x (n-2) x (n-3) x (n-4) x (n-n)
// 0! = 1
// 1! = 1 X (1-1)
// 2! = 2 x (2-1) X (2-2)
// 2! = 3 x (3-1) x (3-2) x (3-3)
// 4! = 4 x (4-1) x (4-2) x (4-3) x (4-4)
// 5! = 5 x (5-1) x (5-2) x (5-3) X (5-4) x (5x5)
// n! = n x (n-1)!


fn factorial(n: u32) -> u32{

if n > 0 {
n * factorial(n-1)
}
else{
return 1
}

}

#[cfg(test)]
mod tests {
use crate::factorial;
Expand Down
12 changes: 10 additions & 2 deletions exercises/02_basic_calculator/06_while/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// Rewrite the factorial function using a `while` loop.
pub fn factorial(n: u32) -> u32 {
pub fn factorial(mut n: u32) -> u32 {
// The `todo!()` macro is a placeholder that the compiler
// interprets as "I'll get back to this later", thus
// suppressing type errors.
// It panics at runtime.
todo!()

let mut i =1;

while n>0 {
i = i *n;
n = n-1
}

return i
}

#[cfg(test)]
Expand Down
12 changes: 11 additions & 1 deletion exercises/02_basic_calculator/07_for/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Rewrite the factorial function using a `for` loop.
pub fn factorial(n: u32) -> u32 {
todo!()

let mut product = 1;

for i in 1..=n {

product = product * i

}

return product

}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/09_saturating/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub fn factorial(n: u32) -> u32 {
for i in 1..=n {
// Use saturating multiplication to stop at the maximum value of u32
// rather than overflowing and wrapping around
result *= i;
result = i.saturating_mul(result);
}
result
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/02_basic_calculator/10_as_casting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {

#[test]
fn u16_to_u32() {
let v: u32 = todo!();
let v: u32 = 47u16 as u32;
assert_eq!(47u16 as u32, v);
}

Expand All @@ -24,14 +24,14 @@ mod tests {
// 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
// `i8` value that is equivalent to `255` when converted to `u8`.
let y: i8 = todo!();
let y: i8 = x as i8;

assert_eq!(x, y);
}

#[test]
fn bool_to_u8() {
let v: u8 = todo!();
let v: u8 = true as u8;
assert_eq!(true as u8, v);
}
}