Skip to content
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "3fa9b650-df9f-457f-bfa0-51b62696f998",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "swift-scheduling",
"name": "Swift Scheduling",
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Instructions

Your task is to calculate the square root of a given number.

- Try to avoid using the pre-existing math libraries of your language.
- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4…
- You are only required to handle cases where the result is a positive whole number.

Some potential approaches:

- Linear or binary search for a number that gives the input number when squared.
- Successive approximation using Newton's or Heron's method.
- Calculating one digit at a time or one bit at a time.

You can check out the Wikipedia pages on [integer square root][integer-square-root] and [methods of computing square roots][computing-square-roots] to help with choosing a method of calculation.

[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
10 changes: 10 additions & 0 deletions exercises/practice/square-root/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Introduction

We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target.

As the first step in our calculation, we take a target number and find its square root (that is, the number that when multiplied by itself equals the target number).

The journey will be very long.
To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient.
Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power.
Instead we want to implement our own square root calculation.
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jimmytty"
],
"files": {
"solution": [
"square-root.sql"
],
"test": [
"square-root_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
16 changes: 16 additions & 0 deletions exercises/practice/square-root/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
UPDATE "square-root"
SET result = (
WITH RECURSIVE isqrt (number, approx) AS (
VALUES (radicand, radicand)
UNION ALL
SELECT number, (number / approx + approx) / 2
FROM isqrt
WHERE approx * approx != NUMBER
AND approx > 1
)
SELECT approx AS sqrt
FROM isqrt
ORDER BY approx ASC
LIMIT 1
)
;
28 changes: 28 additions & 0 deletions exercises/practice/square-root/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

[9b748478-7b0a-490c-b87a-609dacf631fd]
description = "root of 1"

[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
description = "root of 4"

[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
description = "root of 25"

[93beac69-265e-4429-abb1-94506b431f81]
description = "root of 81"

[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
description = "root of 196"

[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
description = "root of 65025"
10 changes: 10 additions & 0 deletions exercises/practice/square-root/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS "square-root";
CREATE TABLE "square-root" (
radicand INTEGER NOT NULL,
result INTEGER
);

.mode csv
.import ./data.csv "square-root"

UPDATE "square-root" SET result = NULL;
24 changes: 24 additions & 0 deletions exercises/practice/square-root/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
radicand INTEGER NOT NULL,
expected INTEGER NOT NULL
);

INSERT INTO tests (uuid, description, radicand, expected)
VALUES
('9b748478-7b0a-490c-b87a-609dacf631fd', 'root of 1', 1, 1),
('7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb', 'root of 4', 4, 2),
('6624aabf-3659-4ae0-a1c8-25ae7f33c6ef', 'root of 25', 25, 5),
('93beac69-265e-4429-abb1-94506b431f81', 'root of 81', 81, 9),
('fbddfeda-8c4f-4bc4-87ca-6991af35360e', 'root of 196', 196, 14),
('c03d0532-8368-4734-a8e0-f96a9eb7fc1d', 'root of 65025', 65025, 255);
6 changes: 6 additions & 0 deletions exercises/practice/square-root/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1,
4,
25,
81,
196,
65025,
7 changes: 7 additions & 0 deletions exercises/practice/square-root/square-root.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Schema:
-- CREATE TABLE "square-root" (
-- radicand INTEGER NOT NULL,
-- result INTEGER
-- );
--
-- Task: update the square-root table and set the result based on the radicand.
40 changes: 40 additions & 0 deletions exercises/practice/square-root/square-root_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./square-root.sql
.output

-- Create a clean testing environment:
.read ./create_test_table.sql

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT radicand, result FROM "square-root") AS actual
WHERE (actual.radicand, actual.result) = (tests.radicand, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| tests.radicand
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT radicand, result FROM "square-root") AS actual
WHERE actual.radicand = tests.radicand AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT description, status, message, output, test_code, task_id
FROM tests;

-- Display test results in readable form for the student:
.mode table
SELECT description, status, message
FROM tests;