Skip to content

Commit c858248

Browse files
pedrodesuPedro Ferreira
andauthored
CON-3602 Fix quest-08 (#161)
* fix(quest-08) * fix(lalgebra_scalar): add clone (for compat) (& copy [for perf measures]) --------- Co-authored-by: Pedro Ferreira <pedro.ferreira@01talent.com>
1 parent c07bd1e commit c858248

File tree

35 files changed

+777
-2053
lines changed

35 files changed

+777
-2053
lines changed

solutions/blood_types/src/lib.rs

Lines changed: 91 additions & 314 deletions
Large diffs are not rendered by default.

solutions/commits_stats/Cargo.toml

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

99
[dependencies]
10-
json = "0.12"
11-
chrono = "0.4"
10+
json = "0.12.4"
11+
chrono = "0.4.41"

solutions/commits_stats/src/lib.rs

Lines changed: 20 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,28 @@
1-
// # Instructions:
2-
3-
// In this exercise you will be provided with a json file with data
4-
// corresponding to git commits in github (extracted using the github
5-
// rest api) your job is to extract the relevant data and place it in
6-
// a struct called `CommitData` to get the following information:
7-
8-
// 1. Number of commits per author (identified by the github login)
9-
// 2. And the number of commits per author
10-
11-
// Create two functions:
12-
// fn commits_per_author(data: &Vec<CommitData>) -> HashMap<&str, u32>
13-
// fn commits_per_date(data: &Vec<CommitData>) -> HashMap<String, u32>
14-
// A week is represented by the a year followed by the number of the
15-
// week for example January 1, 2020 is in week 1 of 2020 an will be
16-
// represented by a String with the form "2020-W1"
17-
18-
// # Notions:
19-
// https://docs.rs/chrono/0.4.19/chrono/#modules
20-
// https://serde.rs/
21-
22-
use chrono::prelude::*;
23-
use chrono::IsoWeek;
24-
25-
#[derive(Debug)]
26-
struct Week(IsoWeek);
27-
28-
use std::fmt;
29-
30-
impl fmt::Display for Week {
31-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32-
write!(f, "{:?}", self.0)
33-
}
34-
}
35-
361
use std::collections::HashMap;
372

38-
pub fn commits_per_author(data: &json::JsonValue) -> HashMap<String, u32> {
39-
let mut commits_per_author: HashMap<String, u32> = HashMap::new();
40-
for commit in data.members() {
41-
let count = commits_per_author
42-
.entry(commit["author"]["login"].to_string())
43-
.or_insert(0);
44-
*count += 1;
45-
}
46-
commits_per_author
47-
}
3+
use chrono::{DateTime, Datelike};
484

495
pub fn commits_per_week(data: &json::JsonValue) -> HashMap<String, u32> {
50-
let mut commits_per_week: HashMap<String, u32> = HashMap::new();
51-
for commit in data.members() {
52-
let count = commits_per_week
53-
.entry(
54-
Week(
55-
DateTime::parse_from_rfc3339(&commit["commit"]["author"]["date"].to_string())
56-
.unwrap()
57-
.iso_week(),
58-
)
59-
.to_string(),
60-
)
61-
.or_insert(0);
62-
*count += 1;
63-
}
64-
commits_per_week
6+
data.members()
7+
.map(|l| l["commit"]["author"]["date"].to_string())
8+
.fold(HashMap::new(), |mut acc, x| {
9+
acc.entry(format!(
10+
"{:?}",
11+
DateTime::parse_from_rfc3339(x.as_str()).unwrap().iso_week()
12+
))
13+
.and_modify(|v| *v += 1)
14+
.or_insert(1);
15+
16+
acc
17+
})
6518
}
6619

67-
#[cfg(test)]
68-
mod tests {
69-
use super::*;
70-
use std::fs;
71-
72-
fn test_setup() -> json::JsonValue {
73-
let contents = fs::read_to_string("commits.json").unwrap();
74-
let serialized = json::parse(&contents).unwrap();
75-
serialized
76-
}
77-
78-
#[test]
79-
fn test_commits_per_week() {
80-
let serialized = test_setup();
81-
let commits_per_week = commits_per_week(&serialized);
82-
println!("{:#?}", &commits_per_week);
83-
let date = [
84-
"2020-W47".to_string(),
85-
"2020-W43".to_string(),
86-
"2020-W36".to_string(),
87-
"2020-W50".to_string(),
88-
"2020-W40".to_string(),
89-
"2020-W44".to_string(),
90-
"2020-W46".to_string(),
91-
"2020-W31".to_string(),
92-
"2020-W45".to_string(),
93-
"2020-W49".to_string(),
94-
];
95-
96-
let mut com_per_week = HashMap::new();
97-
let commits = [3, 1, 1, 2, 2, 5, 4, 1, 4, 7];
98-
99-
for i in 0..date.len() {
100-
com_per_week.insert(date[i].clone(), commits[i].clone());
101-
}
102-
103-
assert_eq!(com_per_week, commits_per_week);
104-
}
105-
106-
#[test]
107-
fn test_commits_per_author() {
108-
let serialized = test_setup();
109-
let logins = [
110-
"RPigott",
111-
"RedSoxFan",
112-
"Xyene",
113-
"paul-ri",
114-
"JayceFayne",
115-
"mwenzkowski",
116-
"psnszsn",
117-
"emersion",
118-
"tamirzb",
119-
"ifreund",
120-
"homembaixinho",
121-
];
122-
let commits = [1, 1, 7, 2, 1, 3, 1, 10, 1, 1, 2];
123-
let mut expected = HashMap::new();
124-
125-
for i in 0..logins.len() {
126-
expected.insert(logins[i].to_owned(), commits[i].to_owned());
127-
}
20+
pub fn commits_per_author(data: &json::JsonValue) -> HashMap<String, u32> {
21+
data.members()
22+
.map(|l| l["author"]["login"].to_string())
23+
.fold(HashMap::new(), |mut acc, x| {
24+
acc.entry(x).and_modify(|v| *v += 1).or_insert(1);
12825

129-
let commits_per_author = commits_per_author(&serialized);
130-
println!("{:#?}", &commits_per_author);
131-
assert_eq!(expected, commits_per_author);
132-
}
26+
acc
27+
})
13328
}

solutions/easy_traits/src/lib.rs

Lines changed: 17 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,30 @@
1-
/*
2-
## easy_traits
1+
pub trait AppendStrExt {
2+
fn append_str(&mut self, str_to_append: &str) -> &mut Self;
33

4-
### Instructions
4+
fn append_number(&mut self, nb_to_append: f64) -> &mut Self;
55

6-
Your task is to implement the trait `AppendStr` for the type StringValue.
7-
8-
The trait `AppendStr` has the following functions:
9-
10-
- `append_str`, that appends to the value of the structure a new_str of type String
11-
- `append_number`, that appends to the value of the structure a nb_to_append of type f64
12-
- `remove_punctuation_marks`, that removes from the value of the structure the following punctuation marks `. , ? !`
13-
14-
15-
### Expected Function
16-
17-
```rust
18-
#[derive(Clone)]
19-
struct StringValue {
20-
value: String,
21-
}
22-
23-
trait AppendStr {
24-
fn append_str(self, str_to_append: String) -> Self;
25-
26-
fn append_number(self, nb_to_append: f64) -> Self;
27-
28-
fn remove_punctuation_marks(self) -> Self;
6+
fn remove_punctuation_marks(&mut self) -> &mut Self;
297
}
308

31-
impl AppendStr for StringValue {
32-
}
33-
```
34-
*/
9+
impl AppendStrExt for String {
10+
#[inline]
11+
fn append_str(&mut self, str_to_append: &str) -> &mut Self {
12+
self.push_str(str_to_append);
3513

36-
#[derive(Clone, Debug, PartialEq)]
37-
pub struct StringValue {
38-
pub value: String,
39-
}
40-
41-
pub trait AppendStr {
42-
fn append_str(&mut self, str_to_append: String) -> Self;
43-
44-
fn append_number(&mut self, nb_to_append: f64) -> Self;
45-
46-
fn remove_punctuation_marks(&mut self) -> Self;
47-
}
48-
49-
impl AppendStr for StringValue {
50-
fn append_str(&mut self, str_to_append: String) -> Self {
51-
self.value.push_str(&str_to_append);
52-
self.clone()
14+
self
5315
}
5416

55-
fn append_number(&mut self, nb_to_append: f64) -> Self {
56-
self.value = format!("{}{nb_to_append}", self.value);
17+
#[inline]
18+
fn append_number(&mut self, nb_to_append: f64) -> &mut Self {
19+
self.push_str(nb_to_append.to_string().as_str());
5720

58-
self.clone()
21+
self
5922
}
6023

61-
fn remove_punctuation_marks(&mut self) -> Self {
62-
let mut str_to_append = String::from("");
63-
let chars: Vec<char> = self.value.chars().collect();
64-
for i in chars {
65-
if i != '!' && i != '.' && i != ',' && i != '?' {
66-
str_to_append.push(i);
67-
}
68-
}
69-
70-
self.value = str_to_append;
71-
72-
self.clone()
73-
}
74-
}
75-
76-
#[cfg(test)]
77-
mod tests {
78-
use super::*;
79-
80-
#[test]
81-
fn test_append_str() {
82-
let mut str_aux = StringValue {
83-
value: String::from("hello"),
84-
};
85-
86-
assert_eq!(
87-
String::from("hello there!"),
88-
str_aux.append_str(String::from(" there!")).value
89-
);
90-
91-
assert_eq!(
92-
String::from("hello there! How are You?"),
93-
str_aux.append_str(String::from(" How are You?")).value
94-
);
95-
96-
assert_eq!(
97-
String::from("hello there How are You"),
98-
str_aux.remove_punctuation_marks().value
99-
);
100-
}
101-
102-
#[test]
103-
fn test_remove_punctuation() {
104-
let mut str_aux = StringValue {
105-
value: String::from("!?.,!?.,"),
106-
};
107-
108-
assert_eq!(String::from(""), str_aux.remove_punctuation_marks().value);
109-
110-
assert_eq!(
111-
String::from("h!e!l?lo. the,.re!"),
112-
str_aux.append_str(String::from("h!e!l?lo. the,.re!")).value
113-
);
114-
assert_eq!(
115-
String::from("hello there"),
116-
str_aux.remove_punctuation_marks().value
117-
);
118-
}
119-
120-
#[test]
121-
fn test_append_number() {
122-
let mut str_aux = StringValue {
123-
value: String::from(""),
124-
};
125-
126-
assert_eq!(String::from("-1"), str_aux.append_number(-1.0).value);
127-
128-
assert_eq!(String::from("-15"), str_aux.append_number(5.0).value);
129-
130-
assert_eq!(String::from("-155.5"), str_aux.append_number(5.5).value);
24+
#[inline]
25+
fn remove_punctuation_marks(&mut self) -> &mut Self {
26+
self.retain(|c| !matches!(c, '.' | ',' | '?' | '!'));
13127

132-
assert_eq!(
133-
String::from("-1555"),
134-
str_aux.remove_punctuation_marks().value
135-
);
28+
self
13629
}
13730
}

solutions/easy_traits/src/main.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

solutions/generics/src/lib.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,4 @@
1-
// Write a functions called identity that calculates the identity of a
2-
// value (receives any data type and returns the same value)
3-
4-
// fn main() {
5-
// println!("Hello, world!");
6-
// println!("{}", identity(3));
7-
// }
8-
1+
#[inline]
92
pub fn identity<T>(v: T) -> T {
103
v
114
}
12-
13-
#[cfg(test)]
14-
mod test {
15-
use super::*;
16-
17-
#[derive(PartialEq, Debug)]
18-
struct Point {
19-
x: i32,
20-
y: i32,
21-
}
22-
23-
#[test]
24-
fn test_with_int() {
25-
assert_eq!(identity(3), 3);
26-
}
27-
28-
#[test]
29-
fn test_with_float() {
30-
assert_eq!(identity(1.0), 1.0);
31-
}
32-
33-
#[test]
34-
fn test_with_str() {
35-
assert_eq!(identity("you"), "you");
36-
}
37-
38-
#[test]
39-
fn test_with_struct() {
40-
let s = Point { x: 1, y: 2 };
41-
assert_eq!(identity(&s), &s);
42-
}
43-
}

0 commit comments

Comments
 (0)