Skip to content

Commit b04d705

Browse files
authored
fix(rust-quest03): applied proposed changes
1 parent b6e70f3 commit b04d705

File tree

36 files changed

+441
-1251
lines changed

36 files changed

+441
-1251
lines changed

solutions/arrays/src/lib.rs

+2-43
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,7 @@
1-
// Define a function call thirtytwo_tens that returns an array with 32
2-
// positions fill with only the value 10: [10, 10, 10, ... 10].len()
3-
// = 32
4-
5-
// Write a function that takes an array of i32 and returns the sum of
6-
// the elements (make it work with the main)
7-
// fn main() {
8-
// let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
9-
// let a1: Vec<i32> = (1..11).collect();
10-
// let b = [5; 10];
11-
12-
// println!("The Sum of the elements in {:?} = {}", a, sum(a));
13-
// println!("The Sum of the elements in {:?} = ", a1);
14-
// println!("The Sum of the elements in {:?} = {}", b, sum(b));
15-
// println!(
16-
// "Array size {} with only 10's in it {:?}",
17-
// thirtytwo_tens().len(),
18-
// thirtytwo_tens()
19-
// );
20-
// }
21-
221
pub fn sum(a: &[i32]) -> i32 {
23-
let mut result = 0;
24-
for e in a.iter() {
25-
result += e;
26-
}
27-
result
2+
a.iter().sum()
283
}
294

30-
pub fn thirtytwo_tens() -> [i32; 32] {
5+
pub const fn thirtytwo_tens() -> [i32; 32] {
316
[10; 32]
327
}
33-
34-
#[cfg(test)]
35-
mod test {
36-
use super::*;
37-
38-
#[test]
39-
fn test_thirtytwo_tens() {
40-
assert_eq!(thirtytwo_tens(), [10; 32]);
41-
}
42-
43-
#[test]
44-
fn test_sum() {
45-
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
46-
assert_eq!(sum(&a), a.iter().sum());
47-
}
48-
}

solutions/bigger/src/lib.rs

+1-79
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,5 @@
1-
/* ## bigger
2-
3-
### Instructions
4-
5-
Create the function `bigger` that gets the biggest positive number in the `HashMap`.
6-
7-
### Notions
8-
9-
- https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html
10-
11-
### Expected functions
12-
13-
```rust
14-
fn bigger(h: HashMap<&str, i32>) -> i32{}
15-
```
16-
17-
### Usage
18-
19-
Here is a program to test your function.
20-
21-
```rust
22-
fn main() {
23-
let mut hash = HashMap::new();
24-
hash.insert("Daniel", 122);
25-
hash.insert("Ashley", 333);
26-
hash.insert("Katie", 334);
27-
hash.insert("Robert", 14);
28-
29-
println!(
30-
"The biggest of the elements in the HashMap is {}",
31-
bigger(hash)
32-
);
33-
}
34-
```
35-
36-
And its output
37-
38-
```console
39-
student@ubuntu:~/[[ROOT]]/test$ cargo run
40-
The biggest of the elements in the HashMap is 334
41-
student@ubuntu:~/[[ROOT]]/test$
42-
```
43-
*/
44-
451
use std::collections::HashMap;
462

473
pub fn bigger(h: HashMap<&str, i32>) -> i32 {
48-
let mut max_val = 0;
49-
if max_val >= 0 {
50-
for (_, v) in h.iter() {
51-
if *v > max_val {
52-
max_val = *v;
53-
}
54-
}
55-
}
56-
return max_val;
57-
}
58-
59-
#[cfg(test)]
60-
mod tests {
61-
use super::*;
62-
63-
#[test]
64-
fn test_positive() {
65-
let mut f = HashMap::new();
66-
f.insert("Daniel", 12);
67-
f.insert("Ashley", 333);
68-
f.insert("Katie", 334);
69-
f.insert("Robert", 14);
70-
71-
assert_eq!(334, bigger(f));
72-
}
73-
#[test]
74-
fn test_negative() {
75-
let mut f = HashMap::new();
76-
f.insert("Daniel", 41758712);
77-
f.insert("Ashley", 54551444);
78-
f.insert("Katie", 575556334);
79-
f.insert("Robert", 574148);
80-
81-
assert_eq!(575556334, bigger(f));
82-
}
4+
h.into_values().max().unwrap()
835
}

solutions/capitalizing/src/lib.rs

+22-115
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,32 @@
1-
/*
2-
## capitalizing
3-
4-
### Instructions
5-
6-
Complete the `capitalize_first` function that turns the first letter of a string uppercase.
7-
Complete the `title_case` function that turns the first letter of each word in a string uppercase.
8-
Complete the `change_case` function that turns the uppercase letters of a string into lowercase and
9-
the lowercase letters into uppercase.
10-
11-
### Expected functions
12-
13-
```rust
14-
fn capitalize_first(input: &str) -> String {}
15-
16-
fn title_case(input: &str) -> String {}
17-
18-
fn change_case(input: &str) -> String {}
19-
20-
```
21-
22-
### Usage
23-
24-
Here is a program to test your function.
25-
26-
```rust
27-
fn main() {
28-
println!("{}", capitalize_first("joe is missing"));
29-
println!("{}", title_case("jill is leaving NOW"));
30-
println!("{}", change_case("heLLo THere"));
31-
}
32-
```
33-
34-
And its output
35-
36-
```console
37-
student@ubuntu:~/[[ROOT]]/test$ cargo run
38-
Joe is missing
39-
Jill is leaving now
40-
HEllO thERE
41-
student@ubuntu:~/[[ROOT]]/test$
42-
```
43-
*/
44-
451
pub fn capitalize_first(input: &str) -> String {
46-
input
47-
.chars()
48-
.take(1)
49-
.flat_map(char::to_uppercase)
50-
.chain(input.chars().skip(1))
51-
.collect::<String>()
52-
}
53-
54-
pub fn title_case(input: &str) -> String {
55-
let mut res = String::with_capacity(input.len());
56-
let v: Vec<&str> = input.split(' ').collect();
57-
582
if input.is_empty() {
59-
return String::new();
3+
String::new()
4+
} else {
5+
format!(
6+
"{}{}",
7+
input.chars().next().unwrap().to_ascii_uppercase(),
8+
&input[1..]
9+
)
6010
}
11+
}
6112

62-
for pat in v {
63-
let p = pat.to_lowercase();
64-
let slice: &str = &p[..];
65-
println!("-> {}", p);
66-
res += &capitalize_first(slice);
67-
res.push_str(" ");
68-
}
69-
let r = res.trim().to_string();
70-
return r;
13+
pub fn title_case(input: &str) -> String {
14+
input
15+
.split_inclusive(|c: char| c.is_ascii_whitespace())
16+
.map(capitalize_first)
17+
.collect::<Vec<_>>()
18+
.concat()
7119
}
7220

7321
pub fn change_case(input: &str) -> String {
74-
if input.is_empty() {
75-
return String::new();
76-
}
77-
78-
let mut result = String::with_capacity(input.len());
79-
80-
{
81-
for c in input.chars() {
82-
if c.is_uppercase() {
83-
let lower = c.to_lowercase().next().unwrap();
84-
result.push(lower);
85-
} else if c.is_lowercase() {
86-
let upper = c.to_uppercase().next().unwrap();
87-
result.push(upper);
22+
input
23+
.chars()
24+
.map(|c| {
25+
if c.is_ascii_uppercase() {
26+
c.to_ascii_lowercase()
8827
} else {
89-
result.push(c);
28+
c.to_ascii_uppercase()
9029
}
91-
}
92-
}
93-
94-
result
95-
}
96-
97-
#[cfg(test)]
98-
mod tests {
99-
use super::*;
100-
101-
#[test]
102-
fn test_success() {
103-
assert_eq!(capitalize_first("hello"), "Hello");
104-
assert_eq!(capitalize_first("this is working"), "This is working");
105-
}
106-
107-
#[test]
108-
fn test_titlle_case() {
109-
assert_eq!(title_case("this is a tittle"), "This Is A Tittle");
110-
assert_eq!(title_case("hello my name is carl"), "Hello My Name Is Carl");
111-
}
112-
113-
#[test]
114-
fn test_change_case() {
115-
assert_eq!(change_case("PROgraming"), "proGRAMING");
116-
assert_eq!(change_case("heLLo THere"), "HEllO thERE");
117-
}
118-
119-
#[test]
120-
fn test_empty() {
121-
assert_eq!(capitalize_first(""), "");
122-
assert_eq!(title_case(""), "");
123-
assert_eq!(change_case(""), "");
124-
}
30+
})
31+
.collect()
12532
}

solutions/card_deck/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ edition = "2021"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
11-
rand = "0.8.5"
10+
rand = "0.8.5"

0 commit comments

Comments
 (0)