-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add practice exercise rational-numbers
#248
Open
natanaelsirqueira
wants to merge
8
commits into
exercism:main
Choose a base branch
from
natanaelsirqueira:add-exercise-rational-numbers
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0d6b62c
Add practice exercise `rational-numbers`
natanaelsirqueira 58d200d
Make `base` argument of `power_of_real` function a float
natanaelsirqueira 083ef56
Add label to `base` argument of `power_of_integer` function
natanaelsirqueira a6b5f0c
Remove unnecessary conversion of denominator to its absolute value
natanaelsirqueira 15b9ad5
Use `assert` in `power_of_integer` function
natanaelsirqueira 64a8b8c
Merge branch 'exercism:main' into add-exercise-rational-numbers
natanaelsirqueira 3ee3ba5
Sync manifest config
natanaelsirqueira 41e5a22
Merge branch 'exercism:main' into add-exercise-rational-numbers
natanaelsirqueira 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 @@ | ||
# Instructions | ||
|
||
A rational number is defined as the quotient of two integers `a` and `b`, called the numerator and denominator, respectively, where `b != 0`. | ||
|
||
~~~~exercism/note | ||
Note that mathematically, the denominator can't be zero. | ||
However in many implementations of rational numbers, you will find that the denominator is allowed to be zero with behaviour similar to positive or negative infinity in floating point numbers. | ||
In those cases, the denominator and numerator generally still can't both be zero at once. | ||
~~~~ | ||
|
||
The absolute value `|r|` of the rational number `r = a/b` is equal to `|a|/|b|`. | ||
|
||
The sum of two rational numbers `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ + r₂ = a₁/b₁ + a₂/b₂ = (a₁ * b₂ + a₂ * b₁) / (b₁ * b₂)`. | ||
|
||
The difference of two rational numbers `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ - r₂ = a₁/b₁ - a₂/b₂ = (a₁ * b₂ - a₂ * b₁) / (b₁ * b₂)`. | ||
|
||
The product (multiplication) of two rational numbers `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ * r₂ = (a₁ * a₂) / (b₁ * b₂)`. | ||
|
||
Dividing a rational number `r₁ = a₁/b₁` by another `r₂ = a₂/b₂` is `r₁ / r₂ = (a₁ * b₂) / (a₂ * b₁)` if `a₂` is not zero. | ||
|
||
Exponentiation of a rational number `r = a/b` to a non-negative integer power `n` is `r^n = (a^n)/(b^n)`. | ||
|
||
Exponentiation of a rational number `r = a/b` to a negative integer power `n` is `r^n = (b^m)/(a^m)`, where `m = |n|`. | ||
|
||
Exponentiation of a rational number `r = a/b` to a real (floating-point) number `x` is the quotient `(a^x)/(b^x)`, which is a real number. | ||
|
||
Exponentiation of a real number `x` to a rational number `r = a/b` is `x^(a/b) = root(x^a, b)`, where `root(p, q)` is the `q`th root of `p`. | ||
|
||
Implement the following operations: | ||
|
||
- addition, subtraction, multiplication and division of two rational numbers, | ||
- absolute value, exponentiation of a given rational number to an integer power, exponentiation of a given rational number to a real (floating-point) power, exponentiation of a real number to a rational number. | ||
|
||
Your implementation of rational numbers should always be reduced to lowest terms. | ||
For example, `4/4` should reduce to `1/1`, `30/60` should reduce to `1/2`, `12/8` should reduce to `3/2`, etc. | ||
To reduce a rational number `r = a/b`, divide `a` and `b` by the greatest common divisor (gcd) of `a` and `b`. | ||
So, for example, `gcd(12, 8) = 4`, so `r = 12/8` can be reduced to `(12/4)/(8/4) = 3/2`. | ||
The reduced form of a rational number should be in "standard form" (the denominator should always be a positive integer). | ||
If a denominator with a negative integer is present, multiply both numerator and denominator by `-1` to ensure standard form is reached. | ||
For example, `3/-4` should be reduced to `-3/4` | ||
|
||
Assume that the programming language you are using does not have an implementation of rational numbers. |
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 @@ | ||
*.beam | ||
*.ez | ||
build | ||
erl_crash.dump |
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,23 @@ | ||
{ | ||
"authors": [ | ||
"natanaelsirqueira" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/rational_numbers.gleam" | ||
], | ||
"test": [ | ||
"test/rational_numbers_test.gleam" | ||
], | ||
"example": [ | ||
".meta/example.gleam" | ||
], | ||
"invalidator": [ | ||
"gleam.toml", | ||
"manifest.toml" | ||
] | ||
}, | ||
"blurb": "Implement rational numbers.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Rational_number" | ||
} |
110 changes: 110 additions & 0 deletions
110
exercises/practice/rational-numbers/.meta/example.gleam
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,110 @@ | ||
import gleam/int | ||
import gleam/float | ||
import gleam/result | ||
|
||
pub type RationalNumber { | ||
RationalNumber(numerator: Int, denominator: Int) | ||
} | ||
|
||
pub fn add(r1: RationalNumber, r2: RationalNumber) -> RationalNumber { | ||
let RationalNumber(a, b) = r1 | ||
let RationalNumber(c, d) = r2 | ||
|
||
reduce(RationalNumber(numerator: a * d + c * b, denominator: b * d)) | ||
} | ||
|
||
pub fn subtract(r1: RationalNumber, r2: RationalNumber) -> RationalNumber { | ||
let RationalNumber(a, b) = r1 | ||
let RationalNumber(c, d) = r2 | ||
|
||
reduce(RationalNumber(numerator: a * d - c * b, denominator: b * d)) | ||
} | ||
|
||
pub fn multiply(r1: RationalNumber, r2: RationalNumber) -> RationalNumber { | ||
let RationalNumber(a, b) = r1 | ||
let RationalNumber(c, d) = r2 | ||
|
||
reduce(RationalNumber(numerator: a * c, denominator: b * d)) | ||
} | ||
|
||
pub fn divide(r1: RationalNumber, r2: RationalNumber) -> RationalNumber { | ||
let RationalNumber(a, b) = r1 | ||
let RationalNumber(c, d) = r2 | ||
|
||
reduce(RationalNumber(numerator: a * d, denominator: c * b)) | ||
} | ||
|
||
pub fn absolute_value(r: RationalNumber) -> RationalNumber { | ||
let RationalNumber(numerator, denominator) = reduce(r) | ||
|
||
RationalNumber( | ||
numerator: int.absolute_value(numerator), | ||
denominator: denominator, | ||
) | ||
} | ||
|
||
pub fn power_of_rational( | ||
number base: RationalNumber, | ||
to exponent: Int, | ||
) -> RationalNumber { | ||
case base { | ||
RationalNumber(numerator, denominator) if exponent < 0 -> | ||
power_of_rational( | ||
RationalNumber(numerator: denominator, denominator: numerator), | ||
to: int.absolute_value(exponent), | ||
) | ||
|
||
RationalNumber(numerator, denominator) -> { | ||
let numerator = power_of_integer(numerator, to: exponent) | ||
let denominator = power_of_integer(denominator, to: exponent) | ||
|
||
reduce(RationalNumber(numerator, denominator)) | ||
} | ||
} | ||
} | ||
|
||
pub fn power_of_real( | ||
number base: Float, | ||
to exponent: RationalNumber, | ||
) -> Result(Float, Nil) { | ||
let RationalNumber(numerator, denominator) = exponent | ||
|
||
use power <- result.then(float.power(base, int.to_float(numerator))) | ||
|
||
nth_root(denominator, of: power) | ||
} | ||
|
||
pub fn reduce(r: RationalNumber) -> RationalNumber { | ||
let RationalNumber(numerator, denominator) = r | ||
|
||
let gcd = gcd(numerator, denominator) | ||
|
||
case numerator / gcd, denominator / gcd { | ||
numerator, denominator if denominator < 0 -> | ||
RationalNumber( | ||
numerator: int.negate(numerator), | ||
denominator: int.negate(denominator), | ||
) | ||
|
||
numerator, denominator -> RationalNumber(numerator, denominator) | ||
} | ||
} | ||
|
||
fn power_of_integer(number base: Int, to exponent: Int) -> Int { | ||
let assert Ok(power) = int.power(base, int.to_float(exponent)) | ||
|
||
float.round(power) | ||
} | ||
|
||
fn nth_root(n: Int, of p: Float) -> Result(Float, Nil) { | ||
let n = int.to_float(n) | ||
|
||
float.power(p, 1.0 /. n) | ||
} | ||
|
||
fn gcd(a: Int, b: Int) -> Int { | ||
case a, b { | ||
a, 0 -> a | ||
a, b -> gcd(b, a % b) | ||
} | ||
} |
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,139 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[0ba4d988-044c-4ed5-9215-4d0bb8d0ae9f] | ||
description = "Arithmetic -> Addition -> Add two positive rational numbers" | ||
|
||
[88ebc342-a2ac-4812-a656-7b664f718b6a] | ||
description = "Arithmetic -> Addition -> Add a positive rational number and a negative rational number" | ||
|
||
[92ed09c2-991e-4082-a602-13557080205c] | ||
description = "Arithmetic -> Addition -> Add two negative rational numbers" | ||
|
||
[6e58999e-3350-45fb-a104-aac7f4a9dd11] | ||
description = "Arithmetic -> Addition -> Add a rational number to its additive inverse" | ||
|
||
[47bba350-9db1-4ab9-b412-4a7e1f72a66e] | ||
description = "Arithmetic -> Subtraction -> Subtract two positive rational numbers" | ||
|
||
[93926e2a-3e82-4aee-98a7-fc33fb328e87] | ||
description = "Arithmetic -> Subtraction -> Subtract a positive rational number and a negative rational number" | ||
|
||
[a965ba45-9b26-442b-bdc7-7728e4b8d4cc] | ||
description = "Arithmetic -> Subtraction -> Subtract two negative rational numbers" | ||
|
||
[0df0e003-f68e-4209-8c6e-6a4e76af5058] | ||
description = "Arithmetic -> Subtraction -> Subtract a rational number from itself" | ||
|
||
[34fde77a-75f4-4204-8050-8d3a937958d3] | ||
description = "Arithmetic -> Multiplication -> Multiply two positive rational numbers" | ||
|
||
[6d015cf0-0ea3-41f1-93de-0b8e38e88bae] | ||
description = "Arithmetic -> Multiplication -> Multiply a negative rational number by a positive rational number" | ||
|
||
[d1bf1b55-954e-41b1-8c92-9fc6beeb76fa] | ||
description = "Arithmetic -> Multiplication -> Multiply two negative rational numbers" | ||
|
||
[a9b8f529-9ec7-4c79-a517-19365d779040] | ||
description = "Arithmetic -> Multiplication -> Multiply a rational number by its reciprocal" | ||
|
||
[d89d6429-22fa-4368-ab04-9e01a44d3b48] | ||
description = "Arithmetic -> Multiplication -> Multiply a rational number by 1" | ||
|
||
[0d95c8b9-1482-4ed7-bac9-b8694fa90145] | ||
description = "Arithmetic -> Multiplication -> Multiply a rational number by 0" | ||
|
||
[1de088f4-64be-4e6e-93fd-5997ae7c9798] | ||
description = "Arithmetic -> Division -> Divide two positive rational numbers" | ||
|
||
[7d7983db-652a-4e66-981a-e921fb38d9a9] | ||
description = "Arithmetic -> Division -> Divide a positive rational number by a negative rational number" | ||
|
||
[1b434d1b-5b38-4cee-aaf5-b9495c399e34] | ||
description = "Arithmetic -> Division -> Divide two negative rational numbers" | ||
|
||
[d81c2ebf-3612-45a6-b4e0-f0d47812bd59] | ||
description = "Arithmetic -> Division -> Divide a rational number by 1" | ||
|
||
[5fee0d8e-5955-4324-acbe-54cdca94ddaa] | ||
description = "Absolute value -> Absolute value of a positive rational number" | ||
|
||
[3cb570b6-c36a-4963-a380-c0834321bcaa] | ||
description = "Absolute value -> Absolute value of a positive rational number with negative numerator and denominator" | ||
|
||
[6a05f9a0-1f6b-470b-8ff7-41af81773f25] | ||
description = "Absolute value -> Absolute value of a negative rational number" | ||
|
||
[5d0f2336-3694-464f-8df9-f5852fda99dd] | ||
description = "Absolute value -> Absolute value of a negative rational number with negative denominator" | ||
|
||
[f8e1ed4b-9dca-47fb-a01e-5311457b3118] | ||
description = "Absolute value -> Absolute value of zero" | ||
|
||
[4a8c939f-f958-473b-9f88-6ad0f83bb4c4] | ||
description = "Absolute value -> Absolute value of a rational number is reduced to lowest terms" | ||
|
||
[ea2ad2af-3dab-41e7-bb9f-bd6819668a84] | ||
description = "Exponentiation of a rational number -> Raise a positive rational number to a positive integer power" | ||
|
||
[8168edd2-0af3-45b1-b03f-72c01332e10a] | ||
description = "Exponentiation of a rational number -> Raise a negative rational number to a positive integer power" | ||
|
||
[c291cfae-cfd8-44f5-aa6c-b175c148a492] | ||
description = "Exponentiation of a rational number -> Raise a positive rational number to a negative integer power" | ||
|
||
[45cb3288-4ae4-4465-9ae5-c129de4fac8e] | ||
description = "Exponentiation of a rational number -> Raise a negative rational number to an even negative integer power" | ||
|
||
[2d47f945-ffe1-4916-a399-c2e8c27d7f72] | ||
description = "Exponentiation of a rational number -> Raise a negative rational number to an odd negative integer power" | ||
|
||
[e2f25b1d-e4de-4102-abc3-c2bb7c4591e4] | ||
description = "Exponentiation of a rational number -> Raise zero to an integer power" | ||
|
||
[431cac50-ab8b-4d58-8e73-319d5404b762] | ||
description = "Exponentiation of a rational number -> Raise one to an integer power" | ||
|
||
[7d164739-d68a-4a9c-b99f-dd77ce5d55e6] | ||
description = "Exponentiation of a rational number -> Raise a positive rational number to the power of zero" | ||
|
||
[eb6bd5f5-f880-4bcd-8103-e736cb6e41d1] | ||
description = "Exponentiation of a rational number -> Raise a negative rational number to the power of zero" | ||
|
||
[30b467dd-c158-46f5-9ffb-c106de2fd6fa] | ||
description = "Exponentiation of a real number to a rational number -> Raise a real number to a positive rational number" | ||
|
||
[6e026bcc-be40-4b7b-ae22-eeaafc5a1789] | ||
description = "Exponentiation of a real number to a rational number -> Raise a real number to a negative rational number" | ||
|
||
[9f866da7-e893-407f-8cd2-ee85d496eec5] | ||
description = "Exponentiation of a real number to a rational number -> Raise a real number to a zero rational number" | ||
|
||
[0a63fbde-b59c-4c26-8237-1e0c73354d0a] | ||
description = "Reduction to lowest terms -> Reduce a positive rational number to lowest terms" | ||
|
||
[5ed6f248-ad8d-4d4e-a545-9146c6727f33] | ||
description = "Reduction to lowest terms -> Reduce places the minus sign on the numerator" | ||
|
||
[f87c2a4e-d29c-496e-a193-318c503e4402] | ||
description = "Reduction to lowest terms -> Reduce a negative rational number to lowest terms" | ||
|
||
[3b92ffc0-5b70-4a43-8885-8acee79cdaaf] | ||
description = "Reduction to lowest terms -> Reduce a rational number with a negative denominator to lowest terms" | ||
|
||
[c9dbd2e6-5ac0-4a41-84c1-48b645b4f663] | ||
description = "Reduction to lowest terms -> Reduce zero to lowest terms" | ||
|
||
[297b45ad-2054-4874-84d4-0358dc1b8887] | ||
description = "Reduction to lowest terms -> Reduce an integer to lowest terms" | ||
|
||
[a73a17fe-fe8c-4a1c-a63b-e7579e333d9e] | ||
description = "Reduction to lowest terms -> Reduce one to lowest terms" |
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,9 @@ | ||
name = "rational_numbers" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
gleam_stdlib = "~> 0.26" | ||
gleam_bitwise = "~> 1.2" | ||
|
||
[dev-dependencies] | ||
gleeunit = "~> 0.10" |
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,13 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "gleam_bitwise", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "6064699EFBABB1CA392DCB193D0E8B402FB042B4B46857B01E6875E643B57F54" }, | ||
{ name = "gleam_stdlib", version = "0.27.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "9DBDD21B48C654182CDD8AA15ACF85E8E74A0438583C68BD7EF08BE89F999C6F" }, | ||
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" }, | ||
] | ||
|
||
[requirements] | ||
gleam_bitwise = "~> 1.2" | ||
gleam_stdlib = "~> 0.26" | ||
gleeunit = "~> 0.10" |
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.
It feels strange to have a
Result
here but no test with an error case...What do you think?
Maybe we should add a test case with a negative base to the problem-specs?
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.
Sounds good. I'll work on a PR later today.
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.
Opened the PR and it got closed as expected. Are you able to reopen?
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.
Thank you. Let's wait for the PR to be merged before merging this one :)