Skip to content

Commit d22c6f9

Browse files
committed
✨ (concept) Add new concept exercise cars-assemble
This is inspired by the same in csharp track. Provides a gentle introduction to variable assignment, if-statements and numbers.
1 parent 796f2ca commit d22c6f9

File tree

11 files changed

+393
-0
lines changed

11 files changed

+393
-0
lines changed

config.json

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@
3535
},
3636
"exercises": {
3737
"concept": [
38+
{
39+
"slug": "cars-assemble",
40+
"uuid": "1d0e6e2c-c319-48bc-8626-7b3c98473f42",
41+
"name": "Cars Assemble",
42+
"difficulty": 1,
43+
"concepts": [
44+
"if-statements",
45+
"assignment",
46+
"numbers"
47+
],
48+
"prerequisites": [
49+
"booleans"
50+
],
51+
"status": "wip"
52+
},
3853
{
3954
"slug": "lucians-luscious-lasagna",
4055
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Hints
2+
3+
## General
4+
5+
- [Rust book][rust-book-data-types] on Integers and Floating point numbers.
6+
7+
## 1. Calculate the success rate
8+
9+
- Determining the success rate can be done through a [conditional statement][if-statement].
10+
11+
## 2. Calculate the production rate per second
12+
13+
- Use `as` to cast `i32` to `f64`
14+
- Use the `success_rate()` method you wrote earlier to determine the success rate.
15+
- Rust does not allow for multiplication to be applied to two different number
16+
types (such as an `i32` and a `f64`). Both must be of the same type.
17+
- Numbers can be compared using the built-in comparision and equality operators.
18+
Checkout [full list of operators][operators-list] supported by Rust
19+
## 3. Calculate the number of working items produced per second
20+
21+
- Use `as` to cast f64 to i32
22+
- Rounding a float to a certain precision (here 1 decimal places) can be done using:
23+
```rust
24+
let my_float = 3.166;
25+
let rounded = (my_float * 10.0).round() / 10.0;
26+
// => 3.2
27+
```
28+
Read more on this [StackOverflow answer][stackoverflow-rounding].
29+
30+
[if-statement]: https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html#ifelse
31+
[stackoverflow-rounding]: https://stackoverflow.com/a/28656825
32+
[operators-list]:https://doc.rust-lang.org/book/appendix-02-operators.html#operators
33+
[rust-book-data-types]:
34+
https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Instructions
2+
3+
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).
4+
5+
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.
6+
7+
You have three tasks.
8+
9+
## 1. Calculate the success rate
10+
11+
Implement the `success_rate()` function to calculate the ratio of an item being created without error for a given speed. The following table shows how speed influences the success rate:
12+
13+
- `0`: 0% success rate.
14+
- `1` to `4`: 100% success rate.
15+
- `5` to `8`: 90% success rate.
16+
- `9`: 80% success rate.
17+
- `10`: 77% success rate.
18+
19+
```csharp
20+
success_rate(10)
21+
// => 0.77
22+
```
23+
24+
## 2. Calculate the production rate per hour
25+
26+
Implement the `production_rate_per_hour()` function to calculate the assembly line's production rate per hour, taking into account its success rate:
27+
28+
```csharp
29+
production_rate_per_hour(6)
30+
// => 1193.4
31+
```
32+
33+
Note that the value returned is a `f32`.
34+
35+
## 3. Calculate the number of working items produced per minute
36+
37+
Implement the `working_items_per_minute()` function to calculate how many working cars are produced per minute:
38+
39+
```csharp
40+
working_items_per_minute(6)
41+
// => 19
42+
```
43+
44+
Note that the value returned is an `i32`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Introduction
2+
3+
## Numbers
4+
5+
There are two different types of numbers in Rust:
6+
7+
- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `500000`.
8+
- 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`.
9+
10+
The two default numeric types in Rust are `i32` and `f64`. An `i32` is a 32-bit integer and a `f64` is a 64-bit floating-point number.
11+
12+
Arithmetic is done using the standard arithmetic operators. Numbers can be
13+
compared using the standard numeric comparison operators and the equality (`==`)
14+
and inequality (`!=`) operators.
15+
16+
Read in-depth about integer and floating point numbers in the [Rust book][rust-book-data-types].
17+
18+
## Assignment
19+
20+
The following syntax can be used to define a variable and assign a value to it.
21+
```rust
22+
let my_variable = 5;
23+
```
24+
25+
The above defines a variable with value 5 which automatically makes its type as
26+
`i32`. The value of this variable cannot be changed. In Rust, `snake_case` is used
27+
to define the name of a variable
28+
29+
30+
To create a variable that can be assigned a different value one can use `mut`
31+
keyword:
32+
```rust
33+
let mut my_variable = 5;
34+
my_variable = 10;
35+
```
36+
37+
In Rust, integers and floats are different types hence you cannot assigne a
38+
float to and integer variable and vice-versa. Not even if the values seem equal:
39+
```rust
40+
let my_int = 5;
41+
let my_float = my_int; // => panic
42+
43+
let my_float2 = 5.0;
44+
let my_int2 = my_float2; // => panic
45+
```
46+
47+
48+
### Rust requires explicit conversion between types:
49+
50+
1. The `as` keyword can be used for casting:
51+
```rust
52+
let my_int = 50; // => i32
53+
let my_float = my_int as f64; // works as expected
54+
```
55+
1. Using the `.into()` method:
56+
```rust
57+
let my_int = 50;
58+
let my_float: f64 = my_int.into(); // works as expected
59+
```
60+
Note that this requires specifying the variable type explicitly.
61+
62+
63+
As an `i32` has less precision than a `f64`, converting from an `i32` to a `f64` is safe and lossless. However, converting from a `f64` to an `i32` could mean losing data.
64+
65+
## If Statements
66+
67+
In this exercise you must conditionally execute logic. The most common way to do this in Rust is by using an `if/else` statement:
68+
69+
```rust
70+
let x = 6;
71+
72+
if x == 5 {
73+
// Execute logic if x equals 5
74+
} else if x > 7 {
75+
// Execute logic if x greater than 7
76+
} else {
77+
// Execute logic in all other cases
78+
}
79+
```
80+
81+
The condition of an `if` statement must be of type `bool`. Rust has no concept of
82+
_truthy_ values.
83+
84+
[rust-book-data-types]:
85+
https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
**/*.rs.bk
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8+
Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"blurb": "Learn about numbers by analyzing the production of an assembly line.",
3+
"authors": [
4+
"devkabiir"
5+
],
6+
"contributors": [],
7+
"files": {
8+
"solution": [
9+
"src/lib.rs",
10+
"Cargo.toml"
11+
],
12+
"test": [
13+
"tests/cars-assemble.rs"
14+
],
15+
"exemplar": [
16+
".meta/exemplar.rs"
17+
]
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- Know of the existence of the two default number types, `i32` and `f64`.
6+
- Understand that an `i32` represents whole numbers, and a `f64` represents floating-point numbers.
7+
- Know of basic operators such as multiplication, comparison and equality.
8+
- Know how to convert from one numeric type to another.
9+
- Know how to conditionally execute code using an `if` statement.
10+
11+
## Out of scope
12+
13+
- Any other numeric types besides `i32` and `f64` (so no `float`, `byte`, etc.).
14+
- Parsing a `string` to an `i32` or `f64`.
15+
- Converting an `i32` or `f64` to a `string`.
16+
17+
## Concepts
18+
19+
- `integers`: know about `i32` and `f64` types
20+
- `if-statements`: know how to conditionally execute code using an `if` statement.
21+
- `math-operators`: know the math operators; know about precedence; know the role of parentheses
22+
- `assignment`: know the syntax and behavior of assignment in OO languages
23+
24+
## Prerequisites
25+
26+
- `booleans`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const PRODUCTION_RATE_PER_HOUR_FOR_DEFAULT_SPEED: i32 = 221;
2+
3+
pub fn production_rate_per_hour(speed: i32) -> f64 {
4+
let result = production_rate_per_hour_for_speed(speed) * success_rate(speed);
5+
6+
round_to_1_decimal(result)
7+
}
8+
9+
fn round_to_1_decimal(input: f64) -> f64 {
10+
return (input * 10.0).round() / 10.0;
11+
}
12+
13+
fn production_rate_per_hour_for_speed(speed: i32) -> f64 {
14+
(PRODUCTION_RATE_PER_HOUR_FOR_DEFAULT_SPEED * speed) as f64
15+
}
16+
17+
pub fn working_items_per_minute(speed: i32) -> i32 {
18+
(production_rate_per_hour(speed) / 60.0) as i32
19+
}
20+
21+
pub fn success_rate(speed: i32) -> f64 {
22+
if speed == 10 {
23+
return 0.77;
24+
}
25+
26+
if speed == 9 {
27+
return 0.8;
28+
}
29+
30+
if speed >= 5 {
31+
return 0.9;
32+
}
33+
34+
if speed <= 0 {
35+
return 0.0;
36+
}
37+
38+
return 1.0;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "cars_assemble"
4+
version = "1.0.0"
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub fn production_rate_per_hour(_speed: u8) -> f32 {
2+
unimplemented!("Implement production_rate_per_hour")
3+
}
4+
5+
pub fn working_items_per_minute(_speed: u8) -> u8 {
6+
unimplemented!("Implement working_items_per_minute")
7+
}
8+
9+
pub fn success_rate(_speed: u8) -> f32 {
10+
unimplemented!("Implement success_rate")
11+
}

0 commit comments

Comments
 (0)