Skip to content

Commit ebba91e

Browse files
Limm-jkdaunjeong824Yabby1997OMEGA-Ywnstj2007
authored andcommitted
Add new concept exercise : numbers (exercism#2875)
* First * Add : config.json authors & editor * Add : exchange file exchange_test / exchange * Fix : typo Line 2 from .exchange import * -> from exchange import * * Fix : Forder Name * Fix : Folder Name * Added example and instructions. (#6) added : currency-exchange/.meta/example.py added : currency-exchange/.docs/instructions.md * Added design.md (#7) * added design.md added : currency-exchange/.meta/design.md * Added hint.md (#8) Added hint.md (#8) * Add : hint.md (#10) * Delete .DS_Store * Add : hint.md (#9) Add : hint.md Solving Conflict * Add introduction.md (#8) * Fix: typo (#11) fix exchangable to exchangeable fix "Chandler want" to "Chandler wants" in instructions.md * Change File_name & Prettier (#12) * Fix : Prettier (#13) * Add : slug to config.json (#14) * Fix config (#15) * Fix introduction.md (#16) * Fix instructions.md (#17) * Fixed instructions.md Fixed grammatical errors in instructions.md based on BethanyG's review. Co-authored-by: Limm-jk <201602057@cs-cnu.org> Co-authored-by: Junkyu Lim <57378834+Limm-jk@users.noreply.github.com> * add testcase * Added problem "6. Calculate unexchangeable value" (#19) * Added new problem "Calculate unexchangeable value" Added modular arithmetic problem Calculate unexchangeable value * Added new problem "Calculate unexchangeable value" (example.py, exchange.py) Modified example.py, exchange.py * Modified Calculated unexchangeable value Modified `unexchangeable_value()` function's expected return type to be `int` * added new lines at the end of file * applied prettier on instructions.md Co-authored-by: Limm-jk <201602057@cs-cnu.org> Co-authored-by: Junkyu Lim <57378834+Limm-jk@users.noreply.github.com> * Add : Problem Design (#20) * Update languages/config.json we changed the concept name, so needed to update file. * Update languages/exercises/concept/currency-exchange/.docs/introduction.md * Update languages/exercises/concept/currency-exchange/.docs/introduction.md * Update languages/exercises/concept/currency-exchange/.docs/introduction.md * Update languages/exercises/concept/currency-exchange/.docs/introduction.md * Update languages/exercises/concept/currency-exchange/.meta/config.json we've decided to name the stub and test files for the concept and not the story. * Update languages/exercises/concept/currency-exchange/.meta/design.md minor typo * Update languages/exercises/concept/currency-exchange/exchange_test.py * Update languages/exercises/concept/currency-exchange/exchange_test.py * Update languages/exercises/concept/currency-exchange/.docs/introduction.md Co-authored-by: Yunseon Choi <science_co@naver.com> * Update languages/config.json removed arithmetic as a concept until we get the about.md file made. * Update languages/exercises/concept/currency-exchange/.meta/config.json reverting file name change and deferring to different PR. This is currently breaking a CI test. Co-authored-by: Yoggy <wjdek88@naver.com> Co-authored-by: Seunghun Yang <Yabby1997@gmail.com> Co-authored-by: Ticktakto <50654571+Ticktakto@users.noreply.github.com> Co-authored-by: Yunseon Choi <science_co@naver.com> Co-authored-by: wnstj2007 <43427152+wnstj2007@users.noreply.github.com> Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
1 parent 276f5b0 commit ebba91e

File tree

8 files changed

+373
-0
lines changed

8 files changed

+373
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# General
2+
3+
- [The Python Numbers Tutorial](https://docs.python.org/3/tutorial/introduction.html#numbers) and [Python numeric types](https://docs.python.org/3.9/library/stdtypes.html#numeric-types-int-float-complex) can be a great introduction.
4+
5+
## 1. Estimating exchangeable value
6+
7+
- You can use the [division operator](https://docs.python.org/3/tutorial/introduction.html#numbers) to get the value of exchanged currency.
8+
9+
## 2. Changes after exchanging
10+
11+
- You can use the [substraction operator](https://docs.python.org/3/tutorial/introduction.html#numbers) to get the amount of changes.
12+
13+
## 3. Calculate value of bills
14+
15+
- You can use the [multiplication operator](https://docs.python.org/3/tutorial/introduction.html#numbers) to get the value of bills.
16+
17+
## 4. Calculate number of bills
18+
19+
- You need to divide `budget` into `denomination`.
20+
- You need to use type casting _int_ to get exact number of bill.
21+
- To remove decimal places from a `float`, you can convert it to `int`.
22+
23+
**Note:** The `//` operator also does floor division. But, if the operand has `float`, the result is still `float`.
24+
25+
## 5. Calculate exchangeable value
26+
27+
- You need to calculate `spread` percent of `exchange_rate` using multiplication operator and add it to `exchange_rate` to get the exchanged currency.
28+
- Actual rate need to be computed! add exchange rate and exchange fee.
29+
- You can get exchanged money affected by commission by using divide operation and type casting _int_.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Your friend Chandler plans to visit exotic countries all around the world. Sadly, Chandler's math skills aren't good. He's pretty worried about being scammed with currency exchange during his trip - and he wants you to make a currency calculator for him. Here's his specification for the app:
2+
3+
## 1. Estimate value after exchange
4+
5+
Create the `estimate_value()` function, where `budget` & `exchange_rate` are the two required parameters:
6+
7+
1. `budget` : the amount of money you are planning to exchange.
8+
2. `exchange_rate` : unit value of the foreign currency.
9+
10+
This function should return the estimated value of the foreign currency you can receive based on your `budget` and the current `exchange rate`.
11+
12+
**Note:** If your currency is USD and you want to exchange USD for EUR with an exchange rate of `1.20`, then `1.20 USD` == `1 EUR`.
13+
14+
```python
15+
>>> estimate_value(127.5, 1.2)
16+
106.25
17+
```
18+
19+
## 2. Calculate currency left after an exchange
20+
21+
Create the `get_change()` function, where `budget` & `exchanging_value` are the two required parameters:
22+
23+
1. `budget` : amount of money you own.
24+
2. `exchanging_value` : amount of your money you want to exchange now.
25+
26+
This function should return the amount left of your starting currency after exchanging `exchanging_value`.
27+
28+
```python
29+
>>> get_change(127.5, 120)
30+
7.5
31+
```
32+
33+
## 3. Calculate value of bills
34+
35+
Create the `get_value()` function, with parameters `denomination` & `number_of_bills`
36+
37+
1. `denomination` : the value of a single bill.
38+
2. `number_of_bills` : amount of bills you received.
39+
40+
This function should return the total value of bills you now have.
41+
42+
```python
43+
>>> get_value(5, 128)
44+
640
45+
```
46+
47+
## 4. Calculate number of bills
48+
49+
Create the `get_number_of_bills()` function, with parameters `budget` & `denomination`
50+
51+
1. `budget` : amount of money you are planning to exchange.
52+
2. `denomination` : the value of a single bill.
53+
54+
This function should return the number of bills after exchanging all your money.
55+
56+
```python
57+
>>> get_number_of_bills(127.5, 5)
58+
25
59+
```
60+
61+
## 5. Calculate value after exchange
62+
63+
Create the `exchangeable_value()` function, with parameter `budget`, `exchange_rate`, `spread`, & `denomination`.
64+
65+
1. `budget` : amount of your money you are planning to exchange.
66+
2. `exchange_rate` : unit value of the foreign currency.
67+
3. `spread` : percentage taken as exchange fee.
68+
4. `denomination` : the value of a single bill.
69+
70+
This function should return the maximum value you can get considering the `budget`, `exchange_rate`, `spread`, & `denomination`.
71+
72+
**Note:** If `1 EUR` == `1.20 USD` and the spread is `10%`, the _actual exchange rate_ becomes `1 EUR` == `1.32 USD`.
73+
74+
```python
75+
>>> exchangeable_value(127.25, 1.20, 10, 20)
76+
80
77+
>>> exchangeable_value(127.25, 1.20, 10, 5)
78+
95
79+
```
80+
81+
## 6. Calculate unexchangeable value
82+
83+
Create the `unexchangeable_value()` function, with parameter `budget`, `exchange_rate`, `spread`, & `denomination`.
84+
85+
1. `budget` : amount of your money you are planning to exchange.
86+
2. `exchange_rate` : unit value of the foreign currency.
87+
3. `spread` : percentage taken as exchange fee.
88+
4. `denomination` : the value of a single bill.
89+
90+
This function should return the unexchangeable value considering the `budget`, `exchange_rate`, `spread`, & `denomination`.
91+
92+
**Note:** Returned value should be `int` type.
93+
94+
```python
95+
>>> unexchangeable_value(127.25, 1.20, 10, 20)
96+
16
97+
>>> unexchangeable_value(127.25, 1.20, 10, 5)
98+
1
99+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Numbers
2+
3+
There are three different kinds of built-in numbers in Python : `ints`, `floats`, and `complex`. However, in this exercise you'll be dealing only with `ints` and `floats`.
4+
5+
### ints
6+
7+
`ints` are whole numbers. e.g. `1234`, `-10`, `20201278`.
8+
9+
### floats
10+
11+
`floats` are numbers containing a decimal point. e.g. `0.0`,`3.14`,`-9.01`. Floats in Python are of [_arbitrary precision_][arbitrary-precision].
12+
13+
You can see more details in the following resources:
14+
15+
- [Python numeric type documentation][numeric-type-docs]
16+
- [Documentation for `int()` built in][`int()` built in]
17+
- [Documentation for `float()` built in][`float()` built in]
18+
19+
## Arithmetic
20+
21+
Python fully supports arithmetic between `ints` and `floats`. It will convert narrower numbers to match their less narrow counterparts when used with the binary arithmetic operators (`+`, `-`, `*`, `/`, `//`, and `%`). When division with `/`, `//` returns the quotient and `%` returns the remainder.
22+
23+
Python considers `ints` narrower than `floats`. So, using a float in an expression ensures the result will be a float too. However, when doing division, the result will always be a float, even if only integers are used.
24+
25+
```python
26+
# The int is widened to a float here, and a float type is returned.
27+
>>> 3 + 4.0
28+
7.0
29+
>>> 3 * 4.0
30+
12.0
31+
>>> 3 - 2.0
32+
1.0
33+
# Division always returns a float.
34+
>>> 6 / 2
35+
3.0
36+
>>> 7 / 4
37+
1.75
38+
# Calculating remainders.
39+
>>> 7 % 4
40+
3
41+
>>> 2 % 4
42+
2
43+
>>> 12.75 % 3
44+
0.75
45+
```
46+
47+
If an int result is needed, you can use `//` to truncate the result.
48+
49+
```python
50+
>>> 6 // 2
51+
3
52+
>>> 7 // 4
53+
1
54+
```
55+
56+
To convert a float to an integer, you can use `int()`. Also, to convert an integer to a float, you can use `float()`.
57+
58+
```python
59+
>>> int(6 / 2)
60+
3
61+
>>> float(1 + 2)
62+
3.0
63+
```
64+
65+
[arbitrary-precision]: https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#:~:text=In%20computer%20science%2C%20arbitrary%2Dprecision,memory%20of%20the%20host%20system.
66+
[numeric-type-docs]: https://docs.python.org/3/library/stdtypes.html#typesnumeric
67+
[`int()` built in]: https://docs.python.org/3/library/functions.html#int
68+
[`float()` built in]: https://docs.python.org/3/library/functions.html#float
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "Ticktakto",
5+
"exercism_username": "Ticktakto"
6+
},
7+
{
8+
"github_username": "Yabby1997",
9+
"exercism_username": "Yabby1997"
10+
},
11+
{
12+
"github_username": "limm-jk",
13+
"exercism_username": "limm-jk"
14+
},
15+
{
16+
"github_username": "OMEGA-Y",
17+
"exercism_username": "OMEGA-Y"
18+
},
19+
{
20+
"github_username": "wnstj2007",
21+
"exercism_username": "wnstj2007"
22+
}
23+
],
24+
"editor": {
25+
"solution_files": ["exchange.py"],
26+
"test_files": ["exchange_test.py"]
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Goal
2+
3+
The goal of this exercise is to teach the student how to use arithmetic operators and type casting between `int` and `float` in Python
4+
5+
## Learning objectives
6+
7+
- use `+`, `-`, `*`, `/` to add, subtract, multiply, divide numbers(`int` and `float`).
8+
- use `int()` to cast `float` to `int`.
9+
- use `%` to calculate remainders.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def estimate_value(budget, exchange_rate):
2+
return budget / exchange_rate
3+
4+
5+
def get_changes(budget, exchanging_value):
6+
return budget - exchanging_value
7+
8+
9+
def get_value(denomination, number_of_bills):
10+
return number_of_bills * denomination
11+
12+
13+
def get_number_of_bills(budget, denomination):
14+
return int(budget / denomination)
15+
16+
17+
def exchangeable_value(budget, exchange_rate, spread, denomination):
18+
exchange_fee = (exchange_rate / 100) * spread
19+
actual_rate = exchange_rate + exchange_fee
20+
exchangeable_amount = int((budget / actual_rate) / denomination)
21+
return exchangeable_amount * denomination
22+
23+
def unexchangeable_value(budget, exchange_rate, spread, denomination):
24+
exchange_fee = (exchange_rate / 100) * spread
25+
actual_rate = exchange_rate + exchange_fee
26+
unexchangeable_amount = int((budget / actual_rate) % denomination)
27+
return unexchangeable_amount
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def estimate_value(budget, exchange_rate):
2+
pass
3+
4+
5+
def get_change(budget, exchanging_value):
6+
pass
7+
8+
9+
def get_value(denomination_value, number_of_bills):
10+
pass
11+
12+
13+
def get_number_of_bills(budget, denomination):
14+
pass
15+
16+
17+
def exchangeable_value(budget, exchange_rate, spread, minimum_denomination):
18+
pass
19+
20+
21+
def unexchangeable_value(budget, exchange_rate, spread, denomination):
22+
pass
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import unittest
2+
from numbers import *
3+
4+
5+
class TestNumbers(unittest.TestCase):
6+
7+
# Problem 1
8+
def test_estimate_value(self):
9+
input_data = [
10+
# input : budget, exchange_rate
11+
(100000, 0.84),
12+
(700000, 10.1)
13+
]
14+
output_data = [119047, 69306]
15+
for input, output in zip(input_data, output_data):
16+
with self.subTest(input=input, output=output):
17+
self.assertEqual(int(estimate_value(input[0], input[1])), output)
18+
19+
# Problem 2
20+
def test_get_change(self):
21+
input_data = [
22+
# input : budget, exchanging_value
23+
(463000, 5000),
24+
(1250,120),
25+
(15000,1380)
26+
]
27+
output_data = [458000, 1130, 13620]
28+
for input, output in zip(input_data, output_data):
29+
with self.subTest(input=input, output=output):
30+
self.assertEqual(get_change(input[0], input[1]), output)
31+
32+
# Problem 3
33+
def test_get_value(self):
34+
input_data = [
35+
# input : denomination_value, number_of_bills
36+
(10000, 128),
37+
(50, 360),
38+
(200, 200)
39+
]
40+
output_data = [1280000, 18000, 40000]
41+
for input, output in zip(input_data, output_data):
42+
with self.subTest(input=input, output=output):
43+
self.assertEqual(get_value(input[0], input[1]), output)
44+
45+
# Problem 4
46+
def test_get_number_of_bills(self):
47+
input_data = [
48+
# input : budget, denomination
49+
(163270, 50000),
50+
(54361, 1000)
51+
]
52+
output_data = [3,54]
53+
for input, output in zip(input_data, output_data):
54+
with self.subTest(input=input, output=output):
55+
self.assertEqual(get_number_of_bills(input[0], input[1]), output)
56+
57+
# Problem 5
58+
def test_exchangeable_value(self):
59+
input_data = [
60+
# input : budget, exchange_rate, spread, minimum_denomination
61+
(100000, 10.61, 10, 1),
62+
(1500, 0.84, 25, 40),
63+
(470000, 1050, 30, 10000000000),
64+
(470000, 0.00000009, 30, 700),
65+
(425.33, 0.0009, 30, 700)
66+
]
67+
output_data = [8568, 1400, 0, 4017094016600, 363300]
68+
for input, output in zip(input_data, output_data):
69+
with self.subTest(input=input, output=output):
70+
self.assertEqual(exchangeable_value(input[0], input[1], input[2], input[3]), output)
71+
72+
# Problem 6
73+
def test_unexchangeable_value(self):
74+
75+
#exchangeable : 80, actual_rate : 1.3199999999999998, unexchangeable : 16.40151515151517
76+
#input : budget, exchange_rate, spread, denomination
77+
input_data = [
78+
79+
(100000, 10.61, 10, 1),
80+
(1500, 0.84, 25, 40),
81+
(425.33, 0.0009, 30, 700),
82+
(12000, 0.0096, 10, 50)
83+
]
84+
85+
output_data = [0, 28, 229, 13]
86+
87+
for input, output in zip(input_data, output_data):
88+
with self.subTest(input=input, output=output):
89+
self.assertEqual(unexchangeable_value(input[0], input[1], input[2], input[3]), output)
90+
91+

0 commit comments

Comments
 (0)