-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ (concepts) Add concept exercise interest-is-interesting
#1670
Open
devkabiir
wants to merge
1
commit into
exercism:main
Choose a base branch
from
devkabiir:concept-while-loops-if-statements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- [Floating-point types][floating-point-types] section on Chapter 3.2 Data Types | ||
of the Rust Book. | ||
|
||
## 1. Calculate the interest rate | ||
|
||
- By default, any floating-point number defined in Rust code is treated as a | ||
[`f64`][f64]. | ||
- To use [`f32`][f32] one can write numbers with a suffix of `_f32` or explicitly add | ||
the type to declaration. | ||
```rust | ||
let x = 2.0_f32; | ||
|
||
let y: f32 = 3.0; | ||
``` | ||
- [If statements][if-statements] can be used to return different values based on certain | ||
conditions. | ||
|
||
|
||
## 2. Calculate the interest | ||
|
||
- When calculating interest, it might be helpful to notice that `interest_rate` returns a percentage. | ||
|
||
## 3. Calculate the annual balance update | ||
|
||
- When calculating the annual balance update, we can use methods we have defined in previous steps. | ||
|
||
## 4. Calculate the years before reaching the desired balance | ||
|
||
- To calculate the years, one can keep looping until the desired balance is | ||
reached. You can use the [while-loop]. | ||
|
||
[while-loop]: https://doc.rust-lang.org/rust-by-example/flow_control/while.html | ||
[f32]: https://doc.rust-lang.org/std/primitive.f32.html | ||
[f64]: https://doc.rust-lang.org/std/primitive.f64.html | ||
[if-statements]: https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions | ||
[floating-point-types]: | ||
https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=floating#floating-point-types | ||
[rust-book]: https://doc.rust-lang.org/book |
59 changes: 59 additions & 0 deletions
59
exercises/concept/interest-is-interesting/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Instructions | ||
|
||
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): | ||
|
||
- 3.213% for a negative balance (balance gets more negative). | ||
- 0.5% for a positive balance less than `1000` dollars. | ||
- 1.621% for a positive balance greater than or equal to `1000` dollars and less than `5000` dollars. | ||
- 2.475% for a positive balance greater than or equal to `5000` dollars. | ||
|
||
You have four tasks, each of which will deal your balance and its interest rate. | ||
|
||
## 1. Calculate the interest rate | ||
|
||
Implement the `interest_rate()` method to calculate the interest rate based on the specified balance: | ||
|
||
```rust | ||
interest_rate(200.75) | ||
// 0.5 | ||
``` | ||
|
||
|
||
## 2. Calculate the interest | ||
|
||
Implement the `interest()` method to calculate the interest based on the specified balance: | ||
|
||
```rust | ||
interest(200.75) | ||
// 1.00375 | ||
``` | ||
|
||
|
||
## 3. Calculate the annual balance update | ||
|
||
Implement the `annual_balance_update()` method to calculate the annual balance update, taking into account the interest rate: | ||
|
||
```rust | ||
annual_balance_update(200.75) | ||
// 201.75375 | ||
``` | ||
|
||
|
||
## 4. Calculate the years before reaching the desired balance | ||
|
||
Implement the `years_before_desired_balance()` method to calculate the minimum number of years required to reach the desired balance given annually compounding interest: | ||
|
||
```rust | ||
years_before_desired_balance(200.75, 214.88) | ||
// 14 | ||
``` | ||
|
||
Note that the value returned is an integer. | ||
|
||
~~~~exercism/note | ||
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. | ||
|
||
Compound interest on the other hand is done by applying interest on a recurring basis. | ||
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. | ||
~~~~ |
45 changes: 45 additions & 0 deletions
45
exercises/concept/interest-is-interesting/.docs/introduction.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Introduction | ||
|
||
## Floating Point Numbers | ||
|
||
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`. | ||
|
||
Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision. | ||
|
||
Rust implements the IEEE 754-2008 "binary32" and "binary64" floating-point types as `f32` and `f64`, respectively. | ||
The f32 type is a single-precision float, and f64 has double precision. | ||
|
||
- `f32`: 32 bit floating point precision. Written as `2.45_f32`. | ||
- `f64`: 64 bit floating point precision. This is default in rust. Written as | ||
`2.45_f64`. | ||
|
||
```rust | ||
fn main() { | ||
let x = 2.0; // f64 | ||
|
||
let y: f32 = 3.0; // f32 | ||
} | ||
``` | ||
|
||
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). | ||
|
||
## Conditional Loops with while | ||
|
||
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: | ||
|
||
```rust | ||
let mut x = 10; | ||
|
||
while x > 0 { | ||
// Execute logic if x > 10 | ||
x = x - 1; | ||
} | ||
``` | ||
|
||
In the above example, we define a `while` loop with the condition `x > 0`. | ||
Since the initial value of `x` was set to `10` and we decrement it by 1 every | ||
loop, this will run the code inside the curly braces 10 times. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
21 changes: 21 additions & 0 deletions
21
exercises/concept/interest-is-interesting/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"authors": [ | ||
"devkabiir" | ||
], | ||
"forked_from": [ | ||
"csharp/interest-is-interesting" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/lib.rs", | ||
"Cargo.toml" | ||
], | ||
"test": [ | ||
"tests/interest-is-interesting.rs" | ||
], | ||
"exemplar": [ | ||
".meta/exemplar.rs" | ||
] | ||
}, | ||
"blurb": "Learn about floating point numbers by adding interest to savings accounts." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Design | ||
|
||
## Learning objectives | ||
|
||
- Know how number literals are represented in Rust | ||
- Know the different types of integer and floating point primitives | ||
- Know how to round floats | ||
- Know how to write while loops | ||
- Know how to use if statements | ||
|
||
## Out of scope | ||
|
||
- High precision floating point arithmetic. | ||
- Decimals and crates that provide such. | ||
|
||
## Concepts | ||
|
||
- Numbers | ||
- Integers | ||
- Floating point values (basic) | ||
- If statements | ||
- While loops | ||
|
||
## Prerequisites | ||
|
||
None | ||
|
||
## Resources to refer to | ||
|
||
### Hints | ||
|
||
[Rust book - Scalar types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html?highlight=primitive#scalar-types) | ||
[cheats.rs - Basic Types](https://cheats.rs/#basic-types) | ||
[Rust reference - Integer literals](https://doc.rust-lang.org/stable/reference/tokens.html#integer-literals) | ||
[Rust reference - Floating point literals](https://doc.rust-lang.org/stable/reference/tokens.html#floating-point-literals) | ||
|
||
### After | ||
|
||
[Rust reference - Literals](https://doc.rust-lang.org/stable/reference/expressions/literal-expr.html) | ||
[Rust reference - Numeric types](https://doc.rust-lang.org/stable/reference/types/numeric.html) | ||
[Rust reference - Type cast expressions](https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#type-cast-expressions) | ||
|
||
## Representer | ||
|
||
This exercise does not require any specific representation logic to be added to the representer. | ||
|
||
## Analyzer | ||
|
||
This exercise does not require any specific logic to be added to the analyzer. |
31 changes: 31 additions & 0 deletions
31
exercises/concept/interest-is-interesting/.meta/exemplar.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
pub fn interest_rate(balance: f64) -> f64 { | ||
if balance < 0.0 { | ||
3.213 | ||
} else if balance < 1000.0 { | ||
0.5 | ||
} else if balance < 5000.0 { | ||
1.621 | ||
} else { | ||
2.475 | ||
} | ||
} | ||
|
||
pub fn interest(balance: f64) -> f64 { | ||
balance * interest_rate(balance) / 100.0 | ||
} | ||
|
||
pub fn annual_balance_update(balance: f64) -> f64 { | ||
balance + interest(balance) | ||
} | ||
|
||
pub fn years_before_desired_balance(balance: f64, target_balance: f64) -> u8 { | ||
let mut accumulating_balance = balance; | ||
let mut years = 0; | ||
|
||
while accumulating_balance < target_balance { | ||
accumulating_balance = annual_balance_update(accumulating_balance); | ||
years += 1; | ||
} | ||
|
||
years | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "interest_is_interesting" | ||
version = "0.1.0" | ||
edition = "2021" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pub fn interest_rate(_balance: f64) -> f64 { | ||
unimplemented!("Implement interest_rate") | ||
} | ||
|
||
pub fn interest(_balance: f64) -> f64 { | ||
unimplemented!("Implement interest") | ||
} | ||
|
||
pub fn annual_balance_update(_balance: f64) -> f64 { | ||
unimplemented!("Implement annual_balance_update") | ||
} | ||
|
||
pub fn years_before_desired_balance(_balance: f64, _target_balance: f64) -> u8 { | ||
unimplemented!("Implement years_before_desired_balance") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked to see if students know about the
mut
keyword. It is mentioned in the introduction of the lasagna exercise, but I think adding a hints makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
assignment
as a prerequisites for this concept and, incars-assemble
#1675 I do exactly that. Should I still include an intro here?Based on my understanding of the state of rust syllabus & the issues around it. I decided not to rely on existing concept exercise implementations. All of my implementations are either completely independent or only build up on what I have added (& will add soon). My focus currently is to split them into much smaller chunks so later we can implement Rust specific concepts without hesitation.