Skip to content
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

Merged
merged 5 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2593,6 +2593,23 @@
"errors"
],
"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
}
],
"foregone": [
Expand Down
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
Contributor

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
Contributor

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 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
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 square_root(radicand :: pos_integer) :: pos_integer
def square_root(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.square_root(radicand)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 SquareRoot.calculate?

Copy link
Contributor

Choose a reason for hiding this comment

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

☝🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 sqrt?

Copy link
Contributor

Choose a reason for hiding this comment

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

My problem with SquareRoot.square_root and SquareRoot.sqrt is that in both cases both the module and the function name are exactly the same noun, shortened or not. I don't really know why, but it feels more correct to me if function name is either an action that gets executed on the "subject" indicated by the module name, or if the function name is a noun that's a specialization of the module name. I wonder if there's an article about this 🤔 I would like to know where my feeling comes from, maybe it's completely unjustified?

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
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)