Skip to content

Commit d805196

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 d805196

File tree

11 files changed

+487
-0
lines changed

11 files changed

+487
-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-statemetns"
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,38 @@
1+
# Hints
2+
3+
## General
4+
5+
- [Floating-point numeric types introduction][docs.microsoft.com-floating_point_numeric_types].
6+
7+
## 1. Calculate the interest rate
8+
9+
- By default, any floating-point number defined in Rust code is treated as a
10+
`f64`.
11+
- To use `f32` one can write numbers with a suffix of `_f32` or explicitly add
12+
the type to declaration.
13+
```rust
14+
let x = 2.0_f32;
15+
16+
let y: f32 = 3.0;
17+
```
18+
- [If statements][if-statements] can be used to return different values based on certain
19+
conditions.
20+
21+
22+
## 2. Calculate the interest
23+
24+
- When calculating interest, it might be helpful to notice that `interest_rate` returns a percentage.
25+
26+
## 3. Calculate the annual balance update
27+
28+
- When calculating the annual balance update, we can use methods we have defined in previous steps.
29+
30+
## 4. Calculate the years before reaching the desired balance
31+
32+
- To calculate the years, one can keep looping until the desired balance is
33+
reached. You can use the [while-loop].
34+
35+
[while-loop]: https://doc.rust-lang.org/rust-by-example/flow_control/while.html
36+
[f32]: https://doc.rust-lang.org/std/primitive.f32.html
37+
[f64]: https://doc.rust-lang.org/std/primitive.f64.html
38+
[if-statements]: https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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. The interest rate your bank gives you depends on the amount of money in your account (its balance):
4+
5+
- 3.213% for a negative balance (balance gets more negative).
6+
- 0.5% for a positive balance less than `1000` dollars.
7+
- 1.621% for a positive balance greater than or equal to `1000` dollars and less than `5000` dollars.
8+
- 2.475% for a positive balance greater than or equal to `5000` dollars.
9+
10+
You have four tasks, each of which will deal your balance and its interest rate.
11+
12+
## 1. Calculate the interest rate
13+
14+
Implement the `interest_rate()` method to calculate the interest rate based on the specified balance:
15+
16+
```rust
17+
interest_is_interesting::interest_rate(200.75)
18+
// 0.5
19+
```
20+
21+
22+
## 2. Calculate the interest
23+
24+
Implement the `interest()` method to calculate the interest based on the specified balance:
25+
26+
```rust
27+
interest_is_interesting::interest(200.75)
28+
// 1.00375
29+
```
30+
31+
32+
## 3. Calculate the annual balance update
33+
34+
Implement the `annual_balance_update()` method to calculate the annual balance update, taking into account the interest rate:
35+
36+
```rust
37+
interest_is_interesting::annual_balance_update(200.75)
38+
// 201.75375
39+
```
40+
41+
42+
## 4. Calculate the years before reaching the desired balance
43+
44+
Implement the `years_before_desired_balance()` method to calculate the minimum number of years required to reach the desired balance given annually compounding interest:
45+
46+
```rust
47+
interest_is_interesting::years_before_desired_balance(200.75, 214.88)
48+
// 14
49+
```
50+
51+
Note that the value returned is an integer.
52+
53+
~~~~exercism/note
54+
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.
55+
56+
Compound interest on the other hand is done by applying interest on a recurring basis.
57+
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.
58+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Introduction
2+
3+
## Floating Point Numbers
4+
5+
A floating-point number is a number with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`.
6+
7+
Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision.
8+
9+
Rust implements the IEEE 754-2008 "binary32" and "binary64" floating-point types
10+
as `f32` and `f64`, respectively.
11+
The f32 type is a single-precision float, and f64 has double precision.
12+
13+
- `f32`: 32 bit floating point precision. Written as `2.45_f32`.
14+
- `f64`: 64 bit floating point precision. This is default in rust. Written as
15+
`2.45_f64`.
16+
17+
```rust
18+
fn main() {
19+
let x = 2.0; // f64
20+
21+
let y: f32 = 3.0; // f32
22+
}
23+
```
24+
25+
As can be seen, each type can store a different number of digits. 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).
26+
27+
## While Loops
28+
29+
In this exercise you may also want to use a loop. There are several ways to write loops in Rust, but the `while` loop is most appropriate here:
30+
31+
```rust
32+
let mut x = 23;
33+
34+
while x > 10 {
35+
// Execute logic if x > 10
36+
x = x - 2;
37+
}
38+
```
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)