Skip to content
Merged
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
405 changes: 91 additions & 314 deletions solutions/blood_types/src/lib.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions solutions/commits_stats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
json = "0.12"
chrono = "0.4"
json = "0.12.4"
chrono = "0.4.41"
145 changes: 20 additions & 125 deletions solutions/commits_stats/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,133 +1,28 @@
// # Instructions:

// In this exercise you will be provided with a json file with data
// corresponding to git commits in github (extracted using the github
// rest api) your job is to extract the relevant data and place it in
// a struct called `CommitData` to get the following information:

// 1. Number of commits per author (identified by the github login)
// 2. And the number of commits per author

// Create two functions:
// fn commits_per_author(data: &Vec<CommitData>) -> HashMap<&str, u32>
// fn commits_per_date(data: &Vec<CommitData>) -> HashMap<String, u32>
// A week is represented by the a year followed by the number of the
// week for example January 1, 2020 is in week 1 of 2020 an will be
// represented by a String with the form "2020-W1"

// # Notions:
// https://docs.rs/chrono/0.4.19/chrono/#modules
// https://serde.rs/

use chrono::prelude::*;
use chrono::IsoWeek;

#[derive(Debug)]
struct Week(IsoWeek);

use std::fmt;

impl fmt::Display for Week {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}

use std::collections::HashMap;

pub fn commits_per_author(data: &json::JsonValue) -> HashMap<String, u32> {
let mut commits_per_author: HashMap<String, u32> = HashMap::new();
for commit in data.members() {
let count = commits_per_author
.entry(commit["author"]["login"].to_string())
.or_insert(0);
*count += 1;
}
commits_per_author
}
use chrono::{DateTime, Datelike};

pub fn commits_per_week(data: &json::JsonValue) -> HashMap<String, u32> {
let mut commits_per_week: HashMap<String, u32> = HashMap::new();
for commit in data.members() {
let count = commits_per_week
.entry(
Week(
DateTime::parse_from_rfc3339(&commit["commit"]["author"]["date"].to_string())
.unwrap()
.iso_week(),
)
.to_string(),
)
.or_insert(0);
*count += 1;
}
commits_per_week
data.members()
.map(|l| l["commit"]["author"]["date"].to_string())
.fold(HashMap::new(), |mut acc, x| {
acc.entry(format!(
"{:?}",
DateTime::parse_from_rfc3339(x.as_str()).unwrap().iso_week()
))
.and_modify(|v| *v += 1)
.or_insert(1);

acc
})
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;

fn test_setup() -> json::JsonValue {
let contents = fs::read_to_string("commits.json").unwrap();
let serialized = json::parse(&contents).unwrap();
serialized
}

#[test]
fn test_commits_per_week() {
let serialized = test_setup();
let commits_per_week = commits_per_week(&serialized);
println!("{:#?}", &commits_per_week);
let date = [
"2020-W47".to_string(),
"2020-W43".to_string(),
"2020-W36".to_string(),
"2020-W50".to_string(),
"2020-W40".to_string(),
"2020-W44".to_string(),
"2020-W46".to_string(),
"2020-W31".to_string(),
"2020-W45".to_string(),
"2020-W49".to_string(),
];

let mut com_per_week = HashMap::new();
let commits = [3, 1, 1, 2, 2, 5, 4, 1, 4, 7];

for i in 0..date.len() {
com_per_week.insert(date[i].clone(), commits[i].clone());
}

assert_eq!(com_per_week, commits_per_week);
}

#[test]
fn test_commits_per_author() {
let serialized = test_setup();
let logins = [
"RPigott",
"RedSoxFan",
"Xyene",
"paul-ri",
"JayceFayne",
"mwenzkowski",
"psnszsn",
"emersion",
"tamirzb",
"ifreund",
"homembaixinho",
];
let commits = [1, 1, 7, 2, 1, 3, 1, 10, 1, 1, 2];
let mut expected = HashMap::new();

for i in 0..logins.len() {
expected.insert(logins[i].to_owned(), commits[i].to_owned());
}
pub fn commits_per_author(data: &json::JsonValue) -> HashMap<String, u32> {
data.members()
.map(|l| l["author"]["login"].to_string())
.fold(HashMap::new(), |mut acc, x| {
acc.entry(x).and_modify(|v| *v += 1).or_insert(1);

let commits_per_author = commits_per_author(&serialized);
println!("{:#?}", &commits_per_author);
assert_eq!(expected, commits_per_author);
}
acc
})
}
141 changes: 17 additions & 124 deletions solutions/easy_traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,137 +1,30 @@
/*
## easy_traits
pub trait AppendStrExt {
fn append_str(&mut self, str_to_append: &str) -> &mut Self;

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

Your task is to implement the trait `AppendStr` for the type StringValue.

The trait `AppendStr` has the following functions:

- `append_str`, that appends to the value of the structure a new_str of type String
- `append_number`, that appends to the value of the structure a nb_to_append of type f64
- `remove_punctuation_marks`, that removes from the value of the structure the following punctuation marks `. , ? !`


### Expected Function

```rust
#[derive(Clone)]
struct StringValue {
value: String,
}

trait AppendStr {
fn append_str(self, str_to_append: String) -> Self;

fn append_number(self, nb_to_append: f64) -> Self;

fn remove_punctuation_marks(self) -> Self;
fn remove_punctuation_marks(&mut self) -> &mut Self;
}

impl AppendStr for StringValue {
}
```
*/
impl AppendStrExt for String {
#[inline]
fn append_str(&mut self, str_to_append: &str) -> &mut Self {
self.push_str(str_to_append);

#[derive(Clone, Debug, PartialEq)]
pub struct StringValue {
pub value: String,
}

pub trait AppendStr {
fn append_str(&mut self, str_to_append: String) -> Self;

fn append_number(&mut self, nb_to_append: f64) -> Self;

fn remove_punctuation_marks(&mut self) -> Self;
}

impl AppendStr for StringValue {
fn append_str(&mut self, str_to_append: String) -> Self {
self.value.push_str(&str_to_append);
self.clone()
self
}

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

self.clone()
self
}

fn remove_punctuation_marks(&mut self) -> Self {
let mut str_to_append = String::from("");
let chars: Vec<char> = self.value.chars().collect();
for i in chars {
if i != '!' && i != '.' && i != ',' && i != '?' {
str_to_append.push(i);
}
}

self.value = str_to_append;

self.clone()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_append_str() {
let mut str_aux = StringValue {
value: String::from("hello"),
};

assert_eq!(
String::from("hello there!"),
str_aux.append_str(String::from(" there!")).value
);

assert_eq!(
String::from("hello there! How are You?"),
str_aux.append_str(String::from(" How are You?")).value
);

assert_eq!(
String::from("hello there How are You"),
str_aux.remove_punctuation_marks().value
);
}

#[test]
fn test_remove_punctuation() {
let mut str_aux = StringValue {
value: String::from("!?.,!?.,"),
};

assert_eq!(String::from(""), str_aux.remove_punctuation_marks().value);

assert_eq!(
String::from("h!e!l?lo. the,.re!"),
str_aux.append_str(String::from("h!e!l?lo. the,.re!")).value
);
assert_eq!(
String::from("hello there"),
str_aux.remove_punctuation_marks().value
);
}

#[test]
fn test_append_number() {
let mut str_aux = StringValue {
value: String::from(""),
};

assert_eq!(String::from("-1"), str_aux.append_number(-1.0).value);

assert_eq!(String::from("-15"), str_aux.append_number(5.0).value);

assert_eq!(String::from("-155.5"), str_aux.append_number(5.5).value);
#[inline]
fn remove_punctuation_marks(&mut self) -> &mut Self {
self.retain(|c| !matches!(c, '.' | ',' | '?' | '!'));

assert_eq!(
String::from("-1555"),
str_aux.remove_punctuation_marks().value
);
self
}
}
1 change: 0 additions & 1 deletion solutions/easy_traits/src/main.rs

This file was deleted.

41 changes: 1 addition & 40 deletions solutions/generics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,4 @@
// Write a functions called identity that calculates the identity of a
// value (receives any data type and returns the same value)

// fn main() {
// println!("Hello, world!");
// println!("{}", identity(3));
// }

#[inline]
pub fn identity<T>(v: T) -> T {
v
}

#[cfg(test)]
mod test {
use super::*;

#[derive(PartialEq, Debug)]
struct Point {
x: i32,
y: i32,
}

#[test]
fn test_with_int() {
assert_eq!(identity(3), 3);
}

#[test]
fn test_with_float() {
assert_eq!(identity(1.0), 1.0);
}

#[test]
fn test_with_str() {
assert_eq!(identity("you"), "you");
}

#[test]
fn test_with_struct() {
let s = Point { x: 1, y: 2 };
assert_eq!(identity(&s), &s);
}
}
Loading
Loading