Skip to content

Commit 8fb941c

Browse files
committed
✨ (concepts) Add concept exercise interest-is-interesting
This is inspired by the same on csharp track. Provides introduction to while loops & floating point numbers.
1 parent 796f2ca commit 8fb941c

File tree

11 files changed

+499
-0
lines changed

11 files changed

+499
-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": "interest-is-interesting",
40+
"uuid": "8a81dfe7-e941-4f94-895d-2f2b8305153d",
41+
"name": "Interest is Interesting",
42+
"difficulty": 1,
43+
"concepts": [
44+
"floating-point-numbers",
45+
"while-loops"
46+
],
47+
"prerequisites": [
48+
"assignment",
49+
"if-statements"
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,42 @@
1+
# Hints
2+
3+
## General
4+
5+
- [Floating-point types][floating-point-types] section on Chapter 3.2 Data Types
6+
of the Rust Book.
7+
8+
## 1. Calculate the interest rate
9+
10+
- By default, any floating-point number defined in Rust code is treated as a
11+
[`f64`][f64].
12+
- To use [`f32`][f32] one can write numbers with a suffix of `_f32` or explicitly add
13+
the type to declaration.
14+
```rust
15+
let x = 2.0_f32;
16+
17+
let y: f32 = 3.0;
18+
```
19+
- [If statements][if-statements] can be used to return different values based on certain
20+
conditions.
21+
22+
23+
## 2. Calculate the interest
24+
25+
- When calculating interest, it might be helpful to notice that `interest_rate` returns a percentage.
26+
27+
## 3. Calculate the annual balance update
28+
29+
- When calculating the annual balance update, we can use methods we have defined in previous steps.
30+
31+
## 4. Calculate the years before reaching the desired balance
32+
33+
- To calculate the years, one can keep looping until the desired balance is
34+
reached. You can use the [while-loop].
35+
36+
[while-loop]: https://doc.rust-lang.org/rust-by-example/flow_control/while.html
37+
[f32]: https://doc.rust-lang.org/std/primitive.f32.html
38+
[f64]: https://doc.rust-lang.org/std/primitive.f64.html
39+
[if-statements]: https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions
40+
[floating-point-types]:
41+
https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=floating#floating-point-types
42+
[rust-book]: https://doc.rust-lang.org/book
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Instructions
2+
3+
In this exercise you'll be working with savings accounts. Each year, the balance of your savings account is updated based on its interest rate.
4+
The interest rate your bank gives you depends on the amount of money in your account (its balance):
5+
6+
- 3.213% for a negative balance (balance gets more negative).
7+
- 0.5% for a positive balance less than `1000` dollars.
8+
- 1.621% for a positive balance greater than or equal to `1000` dollars and less than `5000` dollars.
9+
- 2.475% for a positive balance greater than or equal to `5000` dollars.
10+
11+
You have four tasks, each of which will deal your balance and its interest rate.
12+
13+
## 1. Calculate the interest rate
14+
15+
Implement the `interest_rate()` method to calculate the interest rate based on the specified balance:
16+
17+
```rust
18+
interest_rate(200.75)
19+
// 0.5
20+
```
21+
22+
23+
## 2. Calculate the interest
24+
25+
Implement the `interest()` method to calculate the interest based on the specified balance:
26+
27+
```rust
28+
interest(200.75)
29+
// 1.00375
30+
```
31+
32+
33+
## 3. Calculate the annual balance update
34+
35+
Implement the `annual_balance_update()` method to calculate the annual balance update, taking into account the interest rate:
36+
37+
```rust
38+
annual_balance_update(200.75)
39+
// 201.75375
40+
```
41+
42+
43+
## 4. Calculate the years before reaching the desired balance
44+
45+
Implement the `years_before_desired_balance()` method to calculate the minimum number of years required to reach the desired balance given annually compounding interest:
46+
47+
```rust
48+
years_before_desired_balance(200.75, 214.88)
49+
// 14
50+
```
51+
52+
Note that the value returned is an integer.
53+
54+
~~~~exercism/note
55+
When applying simple interest to a principal balance, the balance is multiplied by the interest rate and the product of the two is the interest amount.
56+
57+
Compound interest on the other hand is done by applying interest on a recurring basis.
58+
On each application the interest amount is computed and added to the principal balance so that subsequent interest calculations are subject to a greater principal balance.
59+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Introduction
2+
3+
## Floating Point Numbers
4+
5+
A floating-point number is a number with zero or more digits behind the decimal
6+
separator.
7+
Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`.
8+
9+
Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision.
10+
11+
Rust implements the IEEE 754-2008 "binary32" and "binary64" floating-point types as `f32` and `f64`, respectively.
12+
The f32 type is a single-precision float, and f64 has double precision.
13+
14+
- `f32`: 32 bit floating point precision. Written as `2.45_f32`.
15+
- `f64`: 64 bit floating point precision. This is default in rust. Written as
16+
`2.45_f64`.
17+
18+
```rust
19+
fn main() {
20+
let x = 2.0; // f64
21+
22+
let y: f32 = 3.0; // f32
23+
}
24+
```
25+
26+
As can be seen, each type can store a different number of digits.
27+
This means that trying to store PI in a `float` will only store the first 6 to 9 digits (with the last digit being rounded).
28+
29+
## Conditional Loops with while
30+
31+
In this exercise you may also want to use a loop.
32+
There are several ways to write loops in Rust, but the `while` loop is most appropriate here:
33+
34+
```rust
35+
let mut x = 10;
36+
37+
while x > 0 {
38+
// Execute logic if x > 10
39+
x = x - 1;
40+
}
41+
```
42+
43+
In the above example, we define a `while` loop with the condition `x > 0`.
44+
Since the initial value of `x` was set to `10` and we decrement it by 1 every
45+
loop, this will run the code inside the curly braces 10 times.
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,21 @@
1+
{
2+
"authors": [
3+
"devkabiir"
4+
],
5+
"forked_from": [
6+
"csharp/interest-is-interesting"
7+
],
8+
"files": {
9+
"solution": [
10+
"src/lib.rs",
11+
"Cargo.toml"
12+
],
13+
"test": [
14+
"tests/interest-is-interesting.rs"
15+
],
16+
"exemplar": [
17+
".meta/exemplar.rs"
18+
]
19+
},
20+
"blurb": "Learn about floating point numbers by adding interest to savings accounts."
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- Know how number literals are represented in Rust
6+
- Know the different types of integer and floating point primitives
7+
- Know how to round floats
8+
- Know how to write while loops
9+
- Know how to use if statements
10+
11+
## Out of scope
12+
13+
- High precision floating point arithmetic.
14+
- Decimals and crates that provide such.
15+
16+
## Concepts
17+
18+
- Numbers
19+
- Integers
20+
- Floating point values (basic)
21+
- If statements
22+
- While loops
23+
24+
## Prerequisites
25+
26+
None
27+
28+
## Resources to refer to
29+
30+
### Hints
31+
32+
[Rust book - Scalar types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html?highlight=primitive#scalar-types)
33+
[cheats.rs - Basic Types](https://cheats.rs/#basic-types)
34+
[Rust reference - Integer literals](https://doc.rust-lang.org/stable/reference/tokens.html#integer-literals)
35+
[Rust reference - Floating point literals](https://doc.rust-lang.org/stable/reference/tokens.html#floating-point-literals)
36+
37+
### After
38+
39+
[Rust reference - Literals](https://doc.rust-lang.org/stable/reference/expressions/literal-expr.html)
40+
[Rust reference - Numeric types](https://doc.rust-lang.org/stable/reference/types/numeric.html)
41+
[Rust reference - Type cast expressions](https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#type-cast-expressions)
42+
43+
## Representer
44+
45+
This exercise does not require any specific representation logic to be added to the representer.
46+
47+
## Analyzer
48+
49+
This exercise does not require any specific logic to be added to the analyzer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub fn interest_rate(balance: f64) -> f64 {
2+
if balance < 0.0 {
3+
return 3.213;
4+
}
5+
6+
if balance < 1000.0 {
7+
return 0.5;
8+
}
9+
10+
if balance < 5000.0 {
11+
return 1.621;
12+
}
13+
14+
return 2.475;
15+
}
16+
17+
pub fn interest(balance: f64) -> f64 {
18+
return balance * interest_rate(balance) / 100.0;
19+
}
20+
21+
pub fn annual_balance_update(balance: f64) -> f64 {
22+
return balance + interest(balance);
23+
}
24+
25+
pub fn years_before_desired_balance(balance: f64, target_balance: f64) -> u8 {
26+
let mut accumulating_balance = balance;
27+
let mut years = 0;
28+
29+
while accumulating_balance < target_balance {
30+
accumulating_balance = annual_balance_update(accumulating_balance);
31+
years += 1;
32+
}
33+
34+
return years;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "interest_is_interesting"
3+
version = "0.1.0"
4+
edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub fn interest_rate(_balance: f64) -> f64 {
2+
unimplemented!("Implement interest_rate")
3+
}
4+
5+
pub fn interest(_balance: f64) -> f64 {
6+
unimplemented!("Implement interest")
7+
}
8+
9+
pub fn annual_balance_update(_balance: f64) -> f64 {
10+
unimplemented!("Implement annual_balance_update")
11+
}
12+
13+
pub fn years_before_desired_balance(_balance: f64, _target_balance: f64) -> u8 {
14+
unimplemented!("Implement years_before_desired_balance")
15+
}

0 commit comments

Comments
 (0)