Skip to content

Commit

Permalink
solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanoz committed Apr 9, 2024
1 parent 46cec40 commit e47fcf7
Show file tree
Hide file tree
Showing 31 changed files with 144 additions and 44 deletions.
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///

fn exercise() -> i32 {
let output = 7;
let mut output = 7;

// Do not change below this line __________________________________________
println!("The value of output is: {}", output);
Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn exercise() -> i32 {
let output = 7; // Do not change this line

println!("The value of output is: {}", output);
output = 5;
let output = 5;
println!("The value of output is: {}", output);
output // Do not change this line
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn exercise() -> i32 {
let mut output = "seven"; // Do not change this line

println!("The value of output is: {}", output);
output = 5;
let output = 5;
println!("The value of output is: {}", output);
output // Do not change this line
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn exercise() -> i32 {
let mut output = 7; // Do not change this line
{
println!("The value of output is: {}", output);
output = 5; // <-- how to tell the compiler that this is a different variable?
let output = 5; // <-- how to tell the compiler that this is a different variable?
println!("The value of output is: {}", output);
}
output // Do not change this line
Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn exercise() -> i32 {
}

fn square(value: i32) -> i32 {
value * value;
value * value
}

fn main() {
Expand Down
5 changes: 3 additions & 2 deletions exercises/src/bin/exercise_06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ fn exercise() -> i32 {
divide(value)
}

fn divide(value: i32) {
#[allow(clippy::eq_op)]
fn divide(value: i32) -> i32 {
if value == 0 {
0
} else {
value / value
};
}
}

fn main() {
Expand Down
10 changes: 6 additions & 4 deletions exercises/src/bin/exercise_07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ fn exercise() -> String {
let c = String::from("five");
let d = c;

// format!("{a}={c}")
// format!("{b}={c}")
// format!("{a}={d}")
// format!("{b}={d}")
// format!("{a}={c}") // Invalid: borrow of moved value `c`
// format!("{b}={c}") // Invalid: borrow of moved value `c`
format!("{a}={d}") // Valid: as a is a primitive type, it has a copy trait,
// meaning that b is not taking ownership but is a copy of a
// format!("{b}={d}") // Valid: as a is a primitive type, it has a copy trait,
// meaning that b is not taking ownership but is a copy of a
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[allow(unused)]
fn exercise() -> String {
let value = String::from("Hello world");
read_string(value);
let value = read_string(value);
value
}

Expand Down
6 changes: 3 additions & 3 deletions exercises/src/bin/exercise_09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
fn exercise() -> String {
// Do not change this function
let value = String::from("Hello world");
read_string(value);
read_string(value);
read_string(&value);
read_string(&value);
value
}

fn read_string(value: String) {
fn read_string(value: &String) {
println!("{}", value);
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn exercise() -> String {
value
}

fn read_first_word() {
fn read_first_word(value: &str) -> &str {
// Adjust the signature of this function to make the example compile
value.split_once(' ').unwrap().0
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/src/bin/exercise_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
/// value by 2? Hint: refer to exercise_01 to see how to add mutability to a variable.
///
fn exercise() -> i32 {
let value = 5;
times_two(value);
let mut value = 5;
times_two(&mut value);
value
}

fn times_two(value: i32) {
fn times_two(value: &mut i32) {
*value *= 2; // This line does not require any changes!
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/src/bin/exercise_12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
fn exercise() -> i32 {
let tuple = (1, 2, 3, 5, 13, 21, 34);

todo!("return the correct entry from the tuple")
tuple.3
}

fn main() {
Expand Down
3 changes: 2 additions & 1 deletion exercises/src/bin/exercise_13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
///

fn exercise() -> (i32, String) {
todo!("Call get_input and return the sum of all i32 and the concatenation of all Strings")
let (a, b, c, d, e) = get_input();
(a + c + e, format!("{}{}", b, d))
}

fn get_input() -> (i32, String, i32, String, i32) {
Expand Down
5 changes: 5 additions & 0 deletions exercises/src/bin/exercise_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct Order {
shipping: i32,
tax: i32,
}
impl Order {
fn total_cost(&self) -> i32 {
self.cost + self.shipping + self.tax
}
}

fn main() {
exercise();
Expand Down
5 changes: 5 additions & 0 deletions exercises/src/bin/exercise_15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct Order {
shipping: i32,
tax: i32,
}
impl Order {
fn apply_discount_to_cost(&mut self, discount: i32) {
self.cost -= discount;
}
}

fn main() {
exercise();
Expand Down
4 changes: 4 additions & 0 deletions exercises/src/bin/exercise_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl Order {
fn cost_without_tax(&self) -> i32 {
self.cost + self.shipping
}

fn have_equal_cost_without_tax(order1: &Order, order2: &Order) -> bool {
order1.cost_without_tax() == order2.cost_without_tax()
}
}

fn main() {
Expand Down
3 changes: 2 additions & 1 deletion exercises/src/bin/exercise_17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fn exercise() -> i32 {
let shipping = 10;
let tax = 5;

todo!("Create an order using the builder pattern");
let order = OrderBuilder::new(cost).shipping(shipping).tax(tax).build();
order.total_cost()
}

#[allow(unused)]
Expand Down
10 changes: 8 additions & 2 deletions exercises/src/bin/exercise_18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// is positive, -1 if the number is negative, and 0 if the number is zero.
///

fn exercise() -> () {
fn exercise() {
// Do not change this function
println!("Sign for 5 is {}", sign(5));
println!("Sign for 25 is {}", sign(25));
Expand All @@ -13,7 +13,13 @@ fn exercise() -> () {
}

fn sign(value: i32) -> i32 {
todo!("implement sign function here")
if value > 0 {
1
} else if value < 0 {
-1
} else {
0
}
}

fn main() {
Expand Down
4 changes: 3 additions & 1 deletion exercises/src/bin/exercise_19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
fn exercise() -> i32 {
let mut sum = 0;

todo!("Sum the numbers from 1 to 100 and store the result in the variable `sum` here");
for i in 1..=100 {
sum += i;
}

sum
}
Expand Down
13 changes: 13 additions & 0 deletions exercises/src/bin/exercise_20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ enum TrafficLight {
Yellow,
Green,
}
impl TrafficLight {
fn switch_to_next(&self) -> TrafficLight {
if self == &TrafficLight::Green {
TrafficLight::Yellow
} else if self == &TrafficLight::Yellow {
TrafficLight::Red
} else if self == &TrafficLight::Red {
TrafficLight::Green
} else {
unreachable!()
}
}
}

fn main() {
exercise();
Expand Down
11 changes: 10 additions & 1 deletion exercises/src/bin/exercise_21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// Try to use pattern matching to implement the method.
///

fn exercise() -> () {
fn exercise() {
// Do not change this function
println!("Dog sound: {}", Animal::Dog.make_sound());
println!("Cat sound: {}", Animal::Cat.make_sound());
Expand All @@ -17,6 +17,15 @@ enum Animal {
Cat,
Cow,
}
impl Animal {
fn make_sound(&self) -> &str {
match self {
Animal::Dog => "Woof",
Animal::Cat => "Meow",
Animal::Cow => "Moo",
}
}
}

fn main() {
exercise();
Expand Down
5 changes: 3 additions & 2 deletions exercises/src/bin/exercise_22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ fn exercise() -> i32 {
let mut sum = 0;

for i in 1..=100 {
let divided = integer_divided_by_two(i);
todo!("If the `integer_divided_by_two` function returns Some value, add its output to the sum");
if let Some(value) = integer_divided_by_two(i) {
sum += value;
}
}

sum
Expand Down
6 changes: 5 additions & 1 deletion exercises/src/bin/exercise_23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use std::num::ParseIntError;

fn exercise(input: &str) -> i32 {
todo!();
if let Ok(value) = int_value(input) {
value
} else {
0
}
}

#[allow(dead_code)]
Expand Down
11 changes: 11 additions & 0 deletions exercises/src/bin/exercise_24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ struct Square {
struct Circle {
value: f32,
}
impl Area for Square {
fn area(&self) -> f32 {
self.value * self.value
}
}
impl Area for Circle {
fn area(&self) -> f32 {
let radius = self.value / 2.0;
core::f32::consts::PI * radius * radius
}
}

trait Area {
fn area(&self) -> f32;
Expand Down
4 changes: 4 additions & 0 deletions exercises/src/bin/exercise_25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn exercise() -> String {
)
}

fn get_first<T>(pair: (T, T)) -> T {
pair.0
}

fn main() {
println!("{}", exercise());
}
Expand Down
16 changes: 13 additions & 3 deletions exercises/src/bin/exercise_26.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@ use std::collections::{HashMap, HashSet};

fn vector_sum(vector: Vec<i32>) -> i32 {
let mut total = 0;
todo!("Return the sum of the vector");
for i in vector {
total += i;
}
total
}

fn hashset_product(hashset: HashSet<i32>) -> i32 {
let mut total = 1;
todo!("Return the product of the hashset");
for i in hashset {
total *= i;
}
total
}

fn hashmap_sum(hashmap: HashMap<&str, Vec<i32>>) -> HashMap<&str, i32> {
let mut total = HashMap::new();
todo!("Return the sum of the hashmap values per key");
for (key, value) in hashmap {
let mut sum = 0;
for i in value {
sum += i;
}
total.insert(key, sum);
}
total
}

Expand Down
3 changes: 2 additions & 1 deletion exercises/src/bin/exercise_27.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
///

fn exercise(a: i32, b: i32) -> i32 {
todo!("Call the apply function twice with a closure defined above");
let sum = apply(|a, b| a + b, a, b);
apply(|a, b| a * b, sum, b)
}

fn apply<F: Fn(i32, i32) -> i32>(func: F, a: i32, b: i32) -> i32 {
Expand Down
9 changes: 6 additions & 3 deletions exercises/src/bin/exercise_28.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ use std::collections::{HashMap, HashSet};
///

fn vector_sum(vector: Vec<i32>) -> i32 {
todo!("Return the sum of the vector");
vector.into_iter().sum()
}

fn hashset_product(hashset: HashSet<i32>) -> i32 {
todo!("Return the product of the hashset");
hashset.into_iter().product()
}

fn hashmap_sum(hashmap: HashMap<&str, Vec<i32>>) -> HashMap<&str, i32> {
todo!("Return the sum of the hashmap values per key");
hashmap
.into_iter()
.map(|(key, value)| (key, value.into_iter().sum()))
.collect()
}

fn main() {
Expand Down
Loading

0 comments on commit e47fcf7

Please sign in to comment.