Skip to content

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 5 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,23 @@
],
"difficulty": 8
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "bf9e6251-105b-4fd7-b388-c6f5a652d174",
"prerequisites": [
"if",
"cond",
"integer",
"multiple-clause-functions",
"pattern-matching",
"recursion"
],
"practices": [
"integer"
],
"difficulty": 5
},
{
"slug": "sgf-parsing",
"name": "SGF Parsing",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/square-root/.docs/instructions.append.md
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`.
9 changes: 9 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
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.
Copy link

@papey papey Jun 29, 2021

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 ?

Copy link
Member

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:

  1. Adding an instructions.append.md file to tell students that they need to implement an algorithm on their own and not use :math.sqrt() or Float.pow
  2. Adding an issue to the analyzer repo so that somebody works on it at some point, an issue similar to Write a basic leap analysis elixir-analyzer#42 for example

Copy link
Contributor Author

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.

That will be apparent looking at the tests, I think. It's not uncommon for tests to have more information than the description :)


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).
4 changes: 4 additions & 0 deletions exercises/practice/square-root/.formatter.exs
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}"]
]
18 changes: 18 additions & 0 deletions exercises/practice/square-root/.meta/config.json
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"
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha I didn't notice that!

}
22 changes: 22 additions & 0 deletions exercises/practice/square-root/.meta/example.ex
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
8 changes: 8 additions & 0 deletions exercises/practice/square-root/lib/square_root.ex
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
28 changes: 28 additions & 0 deletions exercises/practice/square-root/mix.exs
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
57 changes: 57 additions & 0 deletions exercises/practice/square-root/test/square_root_test.exs
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
2 changes: 2 additions & 0 deletions exercises/practice/square-root/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)