Browse Source

Exercism: Assembly Line

master
Julio Biason 3 years ago
parent
commit
5c70e36631
  1. 18
      rust/assembly-line/.exercism/config.json
  2. 1
      rust/assembly-line/.exercism/metadata.json
  3. 7
      rust/assembly-line/Cargo.lock
  4. 4
      rust/assembly-line/Cargo.toml
  5. 85
      rust/assembly-line/HELP.md
  6. 13
      rust/assembly-line/HINTS.md
  7. 86
      rust/assembly-line/README.md
  8. 17
      rust/assembly-line/src/lib.rs
  9. 60
      rust/assembly-line/tests/assembly-line.rs

18
rust/assembly-line/.exercism/config.json

@ -0,0 +1,18 @@
{
"blurb": "Learn about numbers while working on an assembly line for cars.",
"authors": [
"LewisClement",
"efx"
],
"files": {
"solution": [
"src/lib.rs"
],
"test": [
"tests/assembly-line.rs"
],
"exemplar": [
".meta/exemplar.rs"
]
}
}

1
rust/assembly-line/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"rust","exercise":"assembly-line","id":"954f6954d0b447a3a35ddb02681b0cbc","url":"https://exercism.org/tracks/rust/exercises/assembly-line","handle":"JBiason","is_requester":true,"auto_approve":false}

7
rust/assembly-line/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "assembly-line"
version = "0.1.0"

4
rust/assembly-line/Cargo.toml

@ -0,0 +1,4 @@
[package]
name = "assembly-line"
version = "0.1.0"
edition = "2018"

85
rust/assembly-line/HELP.md

@ -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` 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.io/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.io/docs/community/contributors

13
rust/assembly-line/HINTS.md

@ -0,0 +1,13 @@
# Hints
## General
## 1. Calculate the production rate per hour
- Determining the success rate can be done through a [conditional statement](https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#if-expressions) or with [pattern matching](https://doc.rust-lang.org/stable/book/ch18-01-all-the-places-for-patterns.html#match-arms).
- As Rust only allows multiplication between values of the same type, some [type casting](https://doc.rust-lang.org/rust-by-example/types/cast.html) will have to be done.
## 2. Calculate the number of working items produced per minute
- Just like multiplication, division is only possible between numbers of the same type. By writing a number with a decimal point (e.g. `1.0` instead of `1`) we can write inline constants with a floating point type.

86
rust/assembly-line/README.md

@ -0,0 +1,86 @@
# Assembly Line
Welcome to Assembly Line on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## numbers
There are two different categories of numbers in Rust.
The name of a numeric type consists of two parts:
- A letter to specify whether it's a floating-point number (f), unsigned integer (u) or signed integer (i)
- A number to specify the numbers size in bits. Larger types have a greater range between minimum and maximum values.
For floating points it will also allow for more numbers behind the decimal separator.
The following combinations are possible:
- 8 bits: `u8`, `i8`
- 16 bits: `u16`, `i16`
- 32 bits: `u32`, `i32`, `f32`
- 64 bits: `u64`, `i64`, `f64`
- 128 bits: `u128`, `i128`
Note that there are only 32-bits and 64-bits variants for floating-point numbers.
## Integers
- Integers: numbers with no digits behind the decimal separator (whole numbers).
Integer types can either store only positive numbers (unsigned) or store either positive and negative numbers (signed).
Examples are -6, 0, 1, 25, 976 and 500000.
## Floating Point Numbers
- Floating-point numbers: numbers with zero or more digits behind the decimal separator.
Examples are -2.4, 0.1, 3.14, 16.984025 and 1024.0.
## Converting between number types
Rust doesn't do any implicit type conversion.
This means that if you need to turn one numeric type into another, you have to do so explicitly.
When converting from a larger type to a smaller one (for instance `u64` to `u32`) you could lose data.
Converting from a floating point to an integer **will** lose everything behind the decimal point, effectively rounding down.
## Instructions
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum).
At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
- `1` to `4`: 100% success rate.
- `5` to `8`: 90% success rate.
- `9` and `10`: 77% success rate.
You have two tasks.
## 1. Calculate the production rate per hour
Implement a method to calculate the assembly line's production rate per hour, taking into account its success rate:
```rust
numbers::production_rate_per_hour(6)
// Returns: 1193.4
```
Note that the value returned is an `f64`.
## 2. Calculate the number of working items produced per minute
Implement a method to calculate how many working cars are produced per minute:
```rust
numbers::working_items_per_minute(6)
// Returns: 19
```
Note that the value returned is an `u32`.
## Source
### Created by
- @LewisClement
- @efx

17
rust/assembly-line/src/lib.rs

@ -0,0 +1,17 @@
// This stub file contains items which aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]
pub fn production_rate_per_hour(speed: u8) -> f64 {
speed as f64
* 221.0
* match speed {
x if x >= 5 && x < 9 => 0.9,
x if x >= 9 => 0.77,
_ => 1.0,
}
}
pub fn working_items_per_minute(speed: u8) -> u32 {
production_rate_per_hour(speed) as u32 / 60
}

60
rust/assembly-line/tests/assembly-line.rs

@ -0,0 +1,60 @@
fn process_rate_per_hour(speed: u8, expected_rate: f64) {
assert!((assembly_line::production_rate_per_hour(speed) - expected_rate).abs() < f64::EPSILON);
}
fn process_rate_per_minute(speed: u8, expected_rate: u32) {
assert_eq!(
assembly_line::working_items_per_minute(speed),
expected_rate
);
}
#[test]
fn production_rate_per_hour_at_speed_zero() {
process_rate_per_hour(0, 0.0);
}
#[test]
fn production_rate_per_hour_at_speed_one() {
process_rate_per_hour(1, 221.0);
}
#[test]
fn production_rate_per_hour_at_speed_four() {
process_rate_per_hour(4, 884.0);
}
#[test]
fn production_rate_per_hour_at_speed_seven() {
process_rate_per_hour(7, 1392.3);
}
#[test]
fn production_rate_per_hour_at_speed_nine() {
process_rate_per_hour(9, 1531.53);
}
#[test]
fn production_rate_per_minute_at_speed_zero() {
process_rate_per_minute(0, 0);
}
#[test]
fn production_rate_per_minute_at_speed_one() {
process_rate_per_minute(1, 3);
}
#[test]
fn production_rate_per_minute_at_speed_five() {
process_rate_per_minute(5, 16);
}
#[test]
fn production_rate_per_minute_at_speed_eight() {
process_rate_per_minute(8, 26);
}
#[test]
fn production_rate_per_minute_at_speed_ten() {
process_rate_per_minute(10, 28);
}
Loading…
Cancel
Save