-
-
Notifications
You must be signed in to change notification settings - Fork 402
New practice exercise square-root
#797
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
36c4453
New practice exercise
jiegillet 59ae72b
Add instructions not to use built-in functions
jiegillet c96d3b0
Rename square_root to sqrt
jiegillet cbc058b
sqrt -> calculate
jiegillet e14d01f
Merge branch 'main' into jie-square-root
jiegillet 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 hidden or 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 hidden or 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,3 @@ | ||
# Introduction append | ||
|
||
Make sure that you implement an algorithm that doesn't rely on built-in function such as `:math.sqrt()` or `Float.pow`. |
This file contains hidden or 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 @@ | ||
# Description | ||
|
||
Given a natural radicand, return its square root. | ||
|
||
Note that the term "radicand" refers to the number for which the root is to be determined. That is, it is the number under the root symbol. | ||
|
||
Check out the Wikipedia pages on [square root](https://en.wikipedia.org/wiki/Square_root) and [methods of computing square roots](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots). | ||
|
||
Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up). |
This file contains hidden or 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 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains hidden or 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,18 @@ | ||
{ | ||
"authors": ["jiegillet"], | ||
"contributors": [], | ||
"files": { | ||
"example": [ | ||
".meta/example.ex" | ||
], | ||
"solution": [ | ||
"lib/square_root.ex" | ||
], | ||
"test": [ | ||
"test/square_root_test.exs" | ||
] | ||
}, | ||
"blurb": "Given a natural radicand, return its square root.", | ||
"source": "wolf99", | ||
"source_url": "https://github.com/exercism/problem-specifications/pull/1582" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How meta 😂 I suspect this isn't really a "good" value for source URL but meh 🤷 we're just copying problem specifications. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hahaha I didn't notice that! |
||
} |
This file contains hidden or 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,22 @@ | ||
defmodule SquareRoot do | ||
@doc """ | ||
Calculate the integer square root of a positive integer | ||
""" | ||
@spec calculate(radicand :: pos_integer) :: pos_integer | ||
def calculate(1), do: 1 | ||
|
||
def calculate(radicand) do | ||
guess = div(radicand, 2) | ||
calculate(radicand, guess) | ||
end | ||
|
||
def calculate(radicand, guess) do | ||
new_guess = div(guess + div(radicand, guess), 2) | ||
|
||
if new_guess == guess do | ||
guess | ||
else | ||
calculate(radicand, new_guess) | ||
end | ||
end | ||
end |
This file contains hidden or 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 @@ | ||
defmodule SquareRoot do | ||
@doc """ | ||
Calculate the integer square root of a positive integer | ||
""" | ||
@spec calculate(radicand :: pos_integer) :: pos_integer | ||
def calculate(radicand) do | ||
end | ||
end |
This file contains hidden or 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,28 @@ | ||
defmodule SquareRoot.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :square_root, | ||
version: "0.1.0", | ||
# elixir: "~> 1.8", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
# {:dep_from_hexpm, "~> 0.3.0"}, | ||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
] | ||
end | ||
end |
This file contains hidden or 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,57 @@ | ||
defmodule SquareRootTest do | ||
use ExUnit.Case | ||
|
||
# @tag :pending | ||
test "root of 1" do | ||
radicand = 1 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 1 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 4" do | ||
radicand = 4 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 2 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 25" do | ||
radicand = 25 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 5 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 81" do | ||
radicand = 81 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 9 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 196" do | ||
radicand = 196 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 14 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 65025" do | ||
radicand = 65025 | ||
output = SquareRoot.calculate(radicand) | ||
expected = 255 | ||
|
||
assert output == expected | ||
end | ||
end |
This file contains hidden or 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,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
We may have to add that this is a simplified exercise where only perfect squares are tested.
This way, we may avoid questions where the student asks what happens if function takes, for example,
3
.We may also add an anylizer ensuring that
:math.sqrt(x)
is not used. Note that maybe just a hint is ok here, what do you think @angelikatyborska ?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.
Yes, we definitely need to tell students not to use
:math.sqrt()
. I would recommend:instructions.append.md
file to tell students that they need to implement an algorithm on their own and not use:math.sqrt()
orFloat.pow
leap
analysis elixir-analyzer#42 for exampleThere 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.
That will be apparent looking at the tests, I think. It's not uncommon for tests to have more information than the description :)