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,43 @@
{
"blurb": "Given students' names along with the grade that they are in, create a roster for the school",
"authors": [
"EduardoBautista"
],
"contributors": [
"andrewclarkson",
"ashleygwilliams",
"coriolinus",
"cwhakes",
"EduardoBautista",
"efx",
"ErikSchierboom",
"ffflorian",
"IanWhitney",
"kytrinyx",
"lpil",
"lutostag",
"navossoc",
"nfiles",
"petertseng",
"pminten",
"rofrol",
"stevejb71",
"stringparser",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/grade-school.rs"
],
"example": [
".meta/example.rs"
]
},
"source": "A pairing session with Phil Battos at gSchool",
"source_url": "http://gschool.it"
}

View File

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

8
grade-school/.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
grade-school/Cargo.toml Normal file
View File

@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "grade-school"
version = "0.0.0"

85
grade-school/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

77
grade-school/README.md Normal file
View File

@@ -0,0 +1,77 @@
# Grade School
Welcome to Grade School on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given students' names along with the grade that they are in, create a roster
for the school.
In the end, you should be able to:
- Add a student's name to the roster for a grade
- "Add Jim to grade 2."
- "OK."
- Get a list of all students enrolled in a grade
- "Which students are in grade 2?"
- "We've only got Jim just now."
- Get a sorted list of all students in all grades. Grades should sort
as 1, 2, 3, etc., and students within a grade should be sorted
alphabetically by name.
- "Who all is enrolled in school right now?"
- "Let me think. We have
Anna, Barb, and Charlie in grade 1,
Alex, Peter, and Zoe in grade 2
and Jim in grade 5.
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim"
Note that all our students only have one name. (It's a small town, what
do you want?)
## For bonus points
Did you get the tests passing and the code clean? If you want to, these
are some additional things you could try:
- If you're working in a language with mutable data structures and your
implementation allows outside code to mutate the school's internal DB
directly, see if you can prevent this. Feel free to introduce additional
tests.
Then please share your thoughts in a comment on the submission. Did this
experiment make the code better? Worse? Did you learn anything from it?
## Source
### Created by
- @EduardoBautista
### Contributed to by
- @andrewclarkson
- @ashleygwilliams
- @coriolinus
- @cwhakes
- @EduardoBautista
- @efx
- @ErikSchierboom
- @ffflorian
- @IanWhitney
- @kytrinyx
- @lpil
- @lutostag
- @navossoc
- @nfiles
- @petertseng
- @pminten
- @rofrol
- @stevejb71
- @stringparser
- @xakon
- @ZapAnton
### Based on
A pairing session with Phil Battos at gSchool - http://gschool.it

46
grade-school/src/lib.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::collections::HashMap;
// This annotation prevents Clippy from warning us that `School` has a
// `fn new()` with no arguments, but doesn't implement the `Default` trait.
//
// Normally, it's good practice to just do what Clippy tells you, but in this
// case, we want to keep things relatively simple. The `Default` trait is not the point
// of this exercise.
#[allow(clippy::new_without_default)]
pub struct School {
student_grades: HashMap<u32, Vec<String>>
}
impl School {
pub fn new() -> Self {
Self {
student_grades: HashMap::new()
}
}
pub fn add(&mut self, grade: u32, student: &str) {
self.student_grades
.entry(grade)
.or_insert(Vec::new())
.push(student.to_owned())
}
pub fn grades(&self) -> Vec<u32> {
let mut grades = self.student_grades.keys().cloned().collect::<Vec<u32>>();
grades.sort();
grades
}
// If `grade` returned a reference, `School` would be forced to keep a `Vec<String>`
// internally to lend out. By returning an owned vector of owned `String`s instead,
// the internal structure can be completely arbitrary. The tradeoff is that some data
// must be copied each time `grade` is called.
pub fn grade(&self, grade: u32) -> Vec<String> {
let mut students_for_grade = match self.student_grades.get(&grade) {
Some(students_grade) => students_grade.to_vec(),
None => vec![]
};
students_for_grade.sort();
students_for_grade
}
}

View File

@@ -0,0 +1,83 @@
use grade_school as school;
fn to_owned(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
#[test]
fn test_grades_for_empty_school() {
let s = school::School::new();
assert_eq!(s.grades(), vec![]);
}
#[test]
#[ignore]
fn test_grades_for_one_student() {
let mut s = school::School::new();
s.add(2, "Aimee");
assert_eq!(s.grades(), vec![2]);
}
#[test]
#[ignore]
fn test_grades_for_several_students_are_sorted() {
let mut s = school::School::new();
s.add(2, "Aimee");
s.add(7, "Logan");
s.add(4, "Blair");
assert_eq!(s.grades(), vec![2, 4, 7]);
}
#[test]
#[ignore]
fn test_grades_when_several_students_have_the_same_grade() {
let mut s = school::School::new();
s.add(2, "Aimee");
s.add(2, "Logan");
s.add(2, "Blair");
assert_eq!(s.grades(), vec![2]);
}
#[test]
#[ignore]
fn test_grade_for_empty_school() {
let s = school::School::new();
assert_eq!(s.grade(1), Vec::<String>::new());
}
#[test]
#[ignore]
fn test_grade_when_no_students_have_that_grade() {
let mut s = school::School::new();
s.add(7, "Logan");
assert_eq!(s.grade(1), Vec::<String>::new());
}
#[test]
#[ignore]
fn test_grade_for_one_student() {
let mut s = school::School::new();
s.add(2, "Aimee");
assert_eq!(s.grade(2), to_owned(&["Aimee"]));
}
#[test]
#[ignore]
fn test_grade_returns_students_sorted_by_name() {
let mut s = school::School::new();
s.add(2, "James");
s.add(2, "Blair");
s.add(2, "Paul");
assert_eq!(s.grade(2), to_owned(&["Blair", "James", "Paul"]));
}
#[test]
#[ignore]
fn test_add_students_to_different_grades() {
let mut s = school::School::new();
s.add(3, "Chelsea");
s.add(7, "Logan");
assert_eq!(s.grades(), vec![3, 7]);
assert_eq!(s.grade(3), to_owned(&["Chelsea"]));
assert_eq!(s.grade(7), to_owned(&["Logan"]));
}