From 10e45e94a4fa815ce83487eeb3cecd743277cddb Mon Sep 17 00:00:00 2001 From: Isaac Good Date: Sat, 26 Sep 2026 21:56:18 -0700 Subject: [PATCH] [/home/goodi/.bash/stuff] Add new practice exercise --- config.json | 8 ++++ exercises/practice/bob/.meta/config.json | 2 +- .../collatz-conjecture/.docs/instructions.md | 3 ++ .../collatz-conjecture/.docs/introduction.md | 28 ++++++++++++++ .../collatz-conjecture/.meta/config.json | 19 ++++++++++ .../collatz-conjecture/.meta/example.gd | 11 ++++++ .../collatz-conjecture/.meta/template.j2 | 12 ++++++ .../collatz-conjecture/.meta/tests.toml | 38 +++++++++++++++++++ .../collatz-conjecture/collatz_conjecture.gd | 2 + .../collatz_conjecture_test.gd | 34 +++++++++++++++++ 10 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/collatz-conjecture/.docs/instructions.md create mode 100644 exercises/practice/collatz-conjecture/.docs/introduction.md create mode 100644 exercises/practice/collatz-conjecture/.meta/config.json create mode 100644 exercises/practice/collatz-conjecture/.meta/example.gd create mode 100644 exercises/practice/collatz-conjecture/.meta/template.j2 create mode 100644 exercises/practice/collatz-conjecture/.meta/tests.toml create mode 100644 exercises/practice/collatz-conjecture/collatz_conjecture.gd create mode 100644 exercises/practice/collatz-conjecture/collatz_conjecture_test.gd diff --git a/config.json b/config.json index 66ac4ff..8a65a12 100644 --- a/config.json +++ b/config.json @@ -242,6 +242,14 @@ "practices": [], "prerequisites": [], "difficulty": 2 + }, + { + "slug": "collatz-conjecture", + "name": "Collatz Conjecture", + "uuid": "00561d93-d636-4e44-9590-88de0e3eccc9", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/bob/.meta/config.json b/exercises/practice/bob/.meta/config.json index 198b7a4..8d08075 100644 --- a/exercises/practice/bob/.meta/config.json +++ b/exercises/practice/bob/.meta/config.json @@ -1,6 +1,6 @@ { "authors": [ - "IsaaG" + "IsaacG" ], "files": { "solution": [ diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md new file mode 100644 index 0000000..af332a8 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -0,0 +1,3 @@ +# Instructions + +Given a positive integer, return the number of steps it takes to reach 1 according to the rules of the Collatz Conjecture. diff --git a/exercises/practice/collatz-conjecture/.docs/introduction.md b/exercises/practice/collatz-conjecture/.docs/introduction.md new file mode 100644 index 0000000..c35bdeb --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/introduction.md @@ -0,0 +1,28 @@ +# Introduction + +One evening, you stumbled upon an old notebook filled with cryptic scribbles, as though someone had been obsessively chasing an idea. +On one page, a single question stood out: **Can every number find its way to 1?** +It was tied to something called the **Collatz Conjecture**, a puzzle that has baffled thinkers for decades. + +The rules were deceptively simple. +Pick any positive integer. + +- If it's even, divide it by 2. +- If it's odd, multiply it by 3 and add 1. + +Then, repeat these steps with the result, continuing indefinitely. + +Curious, you picked number 12 to test and began the journey: + +12 ➜ 6 ➜ 3 ➜ 10 ➜ 5 ➜ 16 ➜ 8 ➜ 4 ➜ 2 ➜ 1 + +Counting from the second number (6), it took 9 steps to reach 1, and each time the rules repeated, the number kept changing. +At first, the sequence seemed unpredictable — jumping up, down, and all over. +Yet, the conjecture claims that no matter the starting number, we'll always end at 1. + +It was fascinating, but also puzzling. +Why does this always seem to work? +Could there be a number where the process breaks down, looping forever or escaping into infinity? +The notebook suggested solving this could reveal something profound — and with it, fame, [fortune][collatz-prize], and a place in history awaits whoever could unlock its secrets. + +[collatz-prize]: https://mathprize.net/posts/collatz-conjecture/ diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json new file mode 100644 index 0000000..1d40633 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "IsaacG" + ], + "files": { + "solution": [ + "collatz_conjecture.gd" + ], + "test": [ + "collatz_conjecture_test.gd" + ], + "example": [ + ".meta/example.gd" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture" +} diff --git a/exercises/practice/collatz-conjecture/.meta/example.gd b/exercises/practice/collatz-conjecture/.meta/example.gd new file mode 100644 index 0000000..8ffec7e --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/example.gd @@ -0,0 +1,11 @@ +func steps(number: int) -> int: + if number < 1: + return ERR_INVALID_PARAMETER + var result: int = 0 + while number > 1: + if number % 2 == 1: + number = number * 3 + 1 + else: + number /= 2 + result += 1 + return result diff --git a/exercises/practice/collatz-conjecture/.meta/template.j2 b/exercises/practice/collatz-conjecture/.meta/template.j2 new file mode 100644 index 0000000..511de19 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/template.j2 @@ -0,0 +1,12 @@ +{% for case in cases %} +func test_{{ case.func_name }}(solution_script): + var got = solution_script.{{ case.property }}({{ case.input.number }}) +{%- if not case.expect_error %} + var want = {{ case.expected }} +{%- else %} + var want = ERR_INVALID_PARAMETER +{%- endif %} + return [got, want] + +{% endfor %} + diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml new file mode 100644 index 0000000..cc34e16 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -0,0 +1,38 @@ +# 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. + +[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd] +description = "zero steps for one" + +[3d76a0a6-ea84-444a-821a-f7857c2c1859] +description = "divide if even" + +[754dea81-123c-429e-b8bc-db20b05a87b9] +description = "even and odd steps" + +[ecfd0210-6f85-44f6-8280-f65534892ff6] +description = "large number of even and odd steps" + +[7d4750e6-def9-4b86-aec7-9f7eb44f95a3] +description = "zero is an error" +include = false + +[2187673d-77d6-4543-975e-66df6c50e2da] +description = "zero is an error" +reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" + +[c6c795bf-a288-45e9-86a1-841359ad426d] +description = "negative value is an error" +include = false + +[ec11f479-56bc-47fd-a434-bcd7a31a7a2e] +description = "negative value is an error" +reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d" diff --git a/exercises/practice/collatz-conjecture/collatz_conjecture.gd b/exercises/practice/collatz-conjecture/collatz_conjecture.gd new file mode 100644 index 0000000..131ca81 --- /dev/null +++ b/exercises/practice/collatz-conjecture/collatz_conjecture.gd @@ -0,0 +1,2 @@ +func steps(number: int): + pass diff --git a/exercises/practice/collatz-conjecture/collatz_conjecture_test.gd b/exercises/practice/collatz-conjecture/collatz_conjecture_test.gd new file mode 100644 index 0000000..d075680 --- /dev/null +++ b/exercises/practice/collatz-conjecture/collatz_conjecture_test.gd @@ -0,0 +1,34 @@ +func test_zero_steps_for_one(solution_script): + var got = solution_script.steps(1) + var want = 0 + return [got, want] + + +func test_divide_if_even(solution_script): + var got = solution_script.steps(16) + var want = 4 + return [got, want] + + +func test_even_and_odd_steps(solution_script): + var got = solution_script.steps(12) + var want = 9 + return [got, want] + + +func test_large_number_of_even_and_odd_steps(solution_script): + var got = solution_script.steps(1000000) + var want = 152 + return [got, want] + + +func test_zero_is_an_error(solution_script): + var got = solution_script.steps(0) + var want = ERR_INVALID_PARAMETER + return [got, want] + + +func test_negative_value_is_an_error(solution_script): + var got = solution_script.steps(-15) + var want = ERR_INVALID_PARAMETER + return [got, want]