Initial commit

This commit is contained in:
2022-08-04 23:58:25 +02:00
commit ff1c36fc24
447 changed files with 9869 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
{
"blurb": "Tally the results of a small football competition.",
"authors": [
"EduardoBautista"
],
"contributors": [
"andrewclarkson",
"ashleygwilliams",
"ClashTheBunny",
"coriolinus",
"cwhakes",
"EduardoBautista",
"efx",
"ErikSchierboom",
"etrepum",
"IanWhitney",
"kytrinyx",
"lutostag",
"mkantor",
"navossoc",
"nfiles",
"petertseng",
"rofrol",
"samcday",
"stringparser",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/tournament.rs"
],
"example": [
".meta/example.rs"
]
}
}

View File

@@ -0,0 +1 @@
{"track":"rust","exercise":"tournament","id":"aafb92c191a349b0bafd436bc2dfed05","url":"https://exercism.org/tracks/rust/exercises/tournament","handle":"ArthurDanjou","is_requester":true,"auto_approve":false}

8
tournament/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

4
tournament/Cargo.toml Normal file
View File

@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "tournament"
version = "1.4.0"

85
tournament/HELP.md Normal file
View File

@@ -0,0 +1,85 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

98
tournament/README.md Normal file
View File

@@ -0,0 +1,98 @@
# Tournament
Welcome to Tournament on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Tally the results of a small football competition.
Based on an input file containing which team played against which and what the
outcome was, create a file with a table like this:
```text
Team | MP | W | D | L | P
Devastating Donkeys | 3 | 2 | 1 | 0 | 7
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
Blithering Badgers | 3 | 1 | 0 | 2 | 3
Courageous Californians | 3 | 0 | 1 | 2 | 1
```
What do those abbreviations mean?
- MP: Matches Played
- W: Matches Won
- D: Matches Drawn (Tied)
- L: Matches Lost
- P: Points
A win earns a team 3 points. A draw earns 1. A loss earns 0.
The outcome should be ordered by points, descending. In case of a tie, teams are ordered alphabetically.
## Input
Your tallying program will receive input that looks like:
```text
Allegoric Alaskans;Blithering Badgers;win
Devastating Donkeys;Courageous Californians;draw
Devastating Donkeys;Allegoric Alaskans;win
Courageous Californians;Blithering Badgers;loss
Blithering Badgers;Devastating Donkeys;loss
Allegoric Alaskans;Courageous Californians;win
```
The result of the match refers to the first team listed. So this line:
```text
Allegoric Alaskans;Blithering Badgers;win
```
means that the Allegoric Alaskans beat the Blithering Badgers.
This line:
```text
Courageous Californians;Blithering Badgers;loss
```
means that the Blithering Badgers beat the Courageous Californians.
And this line:
```text
Devastating Donkeys;Courageous Californians;draw
```
means that the Devastating Donkeys and Courageous Californians tied.
## Source
### Created by
- @EduardoBautista
### Contributed to by
- @andrewclarkson
- @ashleygwilliams
- @ClashTheBunny
- @coriolinus
- @cwhakes
- @EduardoBautista
- @efx
- @ErikSchierboom
- @etrepum
- @IanWhitney
- @kytrinyx
- @lutostag
- @mkantor
- @navossoc
- @nfiles
- @petertseng
- @rofrol
- @samcday
- @stringparser
- @xakon
- @ZapAnton

112
tournament/src/lib.rs Normal file
View File

@@ -0,0 +1,112 @@
extern crate core;
use std::collections::HashMap;
const HEADER: &str = "Team | MP | W | D | L | P";
#[derive(Default, Eq, PartialEq)]
struct Team {
name: String,
matches: u8,
wins: u8,
draws: u8,
losses: u8,
points: u8
}
impl Team {
fn new(name: String) -> Self {
Self {
name,
..Default::default()
}
}
fn win(&mut self) {
self.wins += 1;
self.matches += 1;
self.points += 3;
}
fn draw(&mut self) {
self.draws += 1;
self.matches += 1;
self.points += 1;
}
fn lose(&mut self) {
self.losses += 1;
self.matches += 1;
}
fn add_match(&mut self, result: &MatchResult) {
match result {
MatchResult::Win => self.win(),
MatchResult::Draw => self.draw(),
MatchResult::Loss => self.lose()
}
}
}
impl From<&Team> for String {
fn from(team: &Team) -> String {
format!(
"{:<30} | {:>2} | {:>2} | {:>2} | {:>2} | {:>2}",
team.name, team.matches, team.wins, team.draws, team.losses, team.points
)
}
}
enum MatchResult {
Win,
Draw,
Loss
}
impl From<&str> for MatchResult {
fn from(result: &str) -> Self {
match result {
"win" => MatchResult::Win,
"draw" => MatchResult::Draw,
"loss" => MatchResult::Loss,
_ => panic!()
}
}
}
impl MatchResult {
fn reverse(&self) -> Self {
match self {
MatchResult::Win => MatchResult::Loss,
MatchResult::Draw => MatchResult::Draw,
MatchResult::Loss => MatchResult::Win
}
}
}
pub fn tally(match_results: &str) -> String {
let mut scores: HashMap<String, Team> = HashMap::new();
match_results.lines().for_each(|line| {
let frags: Vec<&str> = line.split(";").collect();
let home = frags[0];
let away = frags[1];
let result = frags[2].into();
scores
.entry(home.into())
.or_insert(Team::new(home.into()))
.add_match(&result);
scores
.entry(away.into())
.or_insert(Team::new(away.into()))
.add_match(&result.reverse());
});
let mut score_values: Vec<&Team> = scores.values().collect();
score_values.sort_by(|a, b| b.points.cmp(&a.points).then_with(|| a.name.cmp(&b.name)));
vec![String::from(HEADER)]
.into_iter()
.chain(score_values.into_iter().map(|t| t.into()))
.collect::<Vec<String>>()
.join("\n")
}

View File

@@ -0,0 +1,152 @@
#[test]
fn just_the_header_if_no_input() {
let input = "";
let expected = "Team | MP | W | D | L | P";
assert_eq!(tournament::tally(input), expected);
}
#[test]
#[ignore]
fn a_win_is_three_points_a_loss_is_zero_points() {
let input = "Allegoric Alaskans;Blithering Badgers;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n"
+ "Blithering Badgers | 1 | 0 | 0 | 1 | 0";
assert_eq!(tournament::tally(input), expected);
}
#[test]
#[ignore]
fn a_win_can_also_be_expressed_as_a_loss() {
let input = "Blithering Badgers;Allegoric Alaskans;loss";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n"
+ "Blithering Badgers | 1 | 0 | 0 | 1 | 0";
assert_eq!(tournament::tally(input), expected);
}
#[test]
#[ignore]
fn a_different_team_can_win() {
let input = "Blithering Badgers;Allegoric Alaskans;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Blithering Badgers | 1 | 1 | 0 | 0 | 3\n"
+ "Allegoric Alaskans | 1 | 0 | 0 | 1 | 0";
assert_eq!(tournament::tally(input), expected);
}
#[test]
#[ignore]
fn there_can_be_more_than_one_match() {
let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string()
+ "Allegoric Alaskans;Blithering Badgers;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n"
+ "Blithering Badgers | 2 | 0 | 0 | 2 | 0";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn a_draw_is_one_point_each() {
let input = "Allegoric Alaskans;Blithering Badgers;draw\n".to_string()
+ "Allegoric Alaskans;Blithering Badgers;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 2 | 1 | 1 | 0 | 4\n"
+ "Blithering Badgers | 2 | 0 | 1 | 1 | 1";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn there_can_be_more_than_one_winner() {
let input = "Allegoric Alaskans;Blithering Badgers;loss\n".to_string()
+ "Allegoric Alaskans;Blithering Badgers;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 2 | 1 | 0 | 1 | 3\n"
+ "Blithering Badgers | 2 | 1 | 0 | 1 | 3";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn there_can_be_more_than_two_teams() {
let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string()
+ "Blithering Badgers;Courageous Californians;win\n"
+ "Courageous Californians;Allegoric Alaskans;loss";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n"
+ "Blithering Badgers | 2 | 1 | 0 | 1 | 3\n"
+ "Courageous Californians | 2 | 0 | 0 | 2 | 0";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn typical_input() {
let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string()
+ "Devastating Donkeys;Courageous Californians;draw\n"
+ "Devastating Donkeys;Allegoric Alaskans;win\n"
+ "Courageous Californians;Blithering Badgers;loss\n"
+ "Blithering Badgers;Devastating Donkeys;loss\n"
+ "Allegoric Alaskans;Courageous Californians;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n"
+ "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n"
+ "Blithering Badgers | 3 | 1 | 0 | 2 | 3\n"
+ "Courageous Californians | 3 | 0 | 1 | 2 | 1";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn incomplete_competition_not_all_pairs_have_played() {
let input = "Allegoric Alaskans;Blithering Badgers;loss\n".to_string()
+ "Devastating Donkeys;Allegoric Alaskans;loss\n"
+ "Courageous Californians;Blithering Badgers;draw\n"
+ "Allegoric Alaskans;Courageous Californians;win";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n"
+ "Blithering Badgers | 2 | 1 | 1 | 0 | 4\n"
+ "Courageous Californians | 2 | 0 | 1 | 1 | 1\n"
+ "Devastating Donkeys | 1 | 0 | 0 | 1 | 0";
assert_eq!(tournament::tally(&input), expected);
}
#[test]
#[ignore]
fn ties_broken_alphabetically() {
let input = "Courageous Californians;Devastating Donkeys;win\n".to_string()
+ "Allegoric Alaskans;Blithering Badgers;win\n"
+ "Devastating Donkeys;Allegoric Alaskans;loss\n"
+ "Courageous Californians;Blithering Badgers;win\n"
+ "Blithering Badgers;Devastating Donkeys;draw\n"
+ "Allegoric Alaskans;Courageous Californians;draw";
let expected = "".to_string()
+ "Team | MP | W | D | L | P\n"
+ "Allegoric Alaskans | 3 | 2 | 1 | 0 | 7\n"
+ "Courageous Californians | 3 | 2 | 1 | 0 | 7\n"
+ "Blithering Badgers | 3 | 0 | 1 | 2 | 1\n"
+ "Devastating Donkeys | 3 | 0 | 1 | 2 | 1";
assert_eq!(tournament::tally(&input), expected);
}