Skip to content

Commit

Permalink
[Exercise]: added square-root (#3279)
Browse files Browse the repository at this point in the history
* added square-root

* Instruction Append

Co-authored-by: BethanyG <[email protected]>
  • Loading branch information
meatball133 and BethanyG authored Dec 11, 2022
1 parent c5da497 commit ee4d9e4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,19 @@
],
"difficulty": 2
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "c32f994a-1080-4f05-bf88-051975a75d64",
"practices": ["number"],
"prerequisites": [
"basics",
"number",
"conditionals",
"loops"
],
"difficulty": 2
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
Expand Down
16 changes: 16 additions & 0 deletions exercises/practice/square-root/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Instructions append

## How this Exercise is Structured in Python


Python offers a wealth of mathematical functions in the form of the [math module][math-module] and built-ins such as [`pow()`][pow] and [`sum()`][sum].
However, we'd like you to consider the challenge of solving this exercise without those built-ins or modules.

While there is a mathematical formula that will find the square root of _any_ number, we have gone the route of only testing [natural numbers][nautral-number] (positive integers).


[math-module]: https://docs.python.org/3/library/math.html
[pow]: https://docs.python.org/3/library/functions.html#pow
[sum]: https://docs.python.org/3/library/functions.html#sum
[nautral-number]: https://en.wikipedia.org/wiki/Natural_number

13 changes: 13 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

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][square-root] and [methods of computing square roots][computing-square-roots].

Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).

[square-root]: https://en.wikipedia.org/wiki/Square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
21 changes: 21 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"meatball133",
"Bethanyg"
],
"contributors": [],
"files": {
"solution": [
"square_root.py"
],
"test": [
"square_root_test.py"
],
"example": [
".meta/example.py"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
5 changes: 5 additions & 0 deletions exercises/practice/square-root/.meta/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def square_root(number):
n = 0
while n ** 2 != number:
n += 1
return n
15 changes: 15 additions & 0 deletions exercises/practice/square-root/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{%- import "generator_macros.j2" as macros with context -%}
{% macro test_case(case) -%}
{%- set input = case["input"] -%}
def test_{{ case["description"] | to_snake }}(self):
self.assertEqual(
{{ case["property"] | to_snake }}({{ case["input"]["radicand"] }}),
{{ case["expected"] }}
)
{%- endmacro %}
{{ macros.header()}}

class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for case in cases -%}
{{ test_case(case) }}
{% endfor %}
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"
2 changes: 2 additions & 0 deletions exercises/practice/square-root/square_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def square_root(number):
pass
27 changes: 27 additions & 0 deletions exercises/practice/square-root/square_root_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest

from square_root import (
square_root,
)

# Tests adapted from `problem-specifications//canonical-data.json`


class SquareRootTest(unittest.TestCase):
def test_root_of_1(self):
self.assertEqual(square_root(1), 1)

def test_root_of_4(self):
self.assertEqual(square_root(4), 2)

def test_root_of_25(self):
self.assertEqual(square_root(25), 5)

def test_root_of_81(self):
self.assertEqual(square_root(81), 9)

def test_root_of_196(self):
self.assertEqual(square_root(196), 14)

def test_root_of_65025(self):
self.assertEqual(square_root(65025), 255)

0 comments on commit ee4d9e4

Please sign in to comment.