-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
New practice exercise square-root
#797
Changes from 1 commit
36c4453
59ae72b
c96d3b0
cbc058b
e14d01f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). |
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}"] | ||
] |
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! |
||
} |
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 square_root(radicand :: pos_integer) :: pos_integer | ||
def square_root(1), do: 1 | ||
|
||
def square_root(radicand) do | ||
guess = div(radicand, 2) | ||
square_root(radicand, guess) | ||
end | ||
|
||
def square_root(radicand, guess) do | ||
new_guess = div(guess + div(radicand, guess), 2) | ||
|
||
if new_guess == guess do | ||
guess | ||
else | ||
square_root(radicand, new_guess) | ||
end | ||
end | ||
end |
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 square_root(radicand :: pos_integer) :: pos_integer | ||
def square_root(radicand) do | ||
end | ||
end |
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 |
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.square_root(radicand) | ||
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. "square root square root" :D sounds a bit silly. Maybe we can deviate from problem specs in this case and do, let's say 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. ☝🏻 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. It might look better in the tests, but it will look weird in the lib file. How about 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. My problem with Here's more proposals. They all look ok to me 🤷 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 defmodule Root do
@doc """
Calculate the integer square root of a positive integer
"""
@spec square(radicand :: pos_integer) :: pos_integer
def square(radicand) do
end
end defmodule Math do
@doc """
Calculate the integer square root of a positive integer
"""
@spec sqrt(radicand :: pos_integer) :: pos_integer
def sqrt(radicand) do
end
end defmodule Calculator do
@doc """
Calculate the integer square root of a positive integer
"""
@spec sqrt(radicand :: pos_integer) :: pos_integer
def sqrt(radicand) do
end
end defmodule PosInteger do
@doc """
Calculate the integer square root of a positive integer
"""
@spec sqrt(radicand :: pos_integer) :: pos_integer
def sqrt(radicand) do
end
end |
||
expected = 1 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 4" do | ||
radicand = 4 | ||
output = SquareRoot.square_root(radicand) | ||
expected = 2 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 25" do | ||
radicand = 25 | ||
output = SquareRoot.square_root(radicand) | ||
expected = 5 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 81" do | ||
radicand = 81 | ||
output = SquareRoot.square_root(radicand) | ||
expected = 9 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 196" do | ||
radicand = 196 | ||
output = SquareRoot.square_root(radicand) | ||
expected = 14 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "root of 65025" do | ||
radicand = 65025 | ||
output = SquareRoot.square_root(radicand) | ||
expected = 255 | ||
|
||
assert output == expected | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |
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 :)