From 2f5fd2f8e420ca5c07bd91728287e7524117a6bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Thu, 16 Nov 2023 06:13:04 -0800 Subject: [PATCH] Add collatz-conjecture --- config.json | 8 ++++ .../collatz-conjecture/.docs/instructions.md | 29 ++++++++++++++ .../collatz-conjecture/.meta/config.json | 19 ++++++++++ .../collatz-conjecture/.meta/example.vim | 19 ++++++++++ .../collatz-conjecture/.meta/tests.toml | 38 +++++++++++++++++++ .../collatz_conjecture.vader | 32 ++++++++++++++++ .../collatz-conjecture/collatz_conjecture.vim | 16 ++++++++ 7 files changed, 161 insertions(+) create mode 100644 exercises/practice/collatz-conjecture/.docs/instructions.md create mode 100644 exercises/practice/collatz-conjecture/.meta/config.json create mode 100644 exercises/practice/collatz-conjecture/.meta/example.vim create mode 100644 exercises/practice/collatz-conjecture/.meta/tests.toml create mode 100644 exercises/practice/collatz-conjecture/collatz_conjecture.vader create mode 100644 exercises/practice/collatz-conjecture/collatz_conjecture.vim diff --git a/config.json b/config.json index 5321058..893e806 100644 --- a/config.json +++ b/config.json @@ -74,6 +74,14 @@ "strings" ] }, + { + "slug": "collatz-conjecture", + "name": "Collatz Conjecture", + "uuid": "4eed1dde-4e87-4cbf-834e-71da55128215", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "difference-of-squares", "name": "Difference of Squares", diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md new file mode 100644 index 0000000..ba06048 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -0,0 +1,29 @@ +# Instructions + +The Collatz Conjecture or 3x+1 problem can be summarized as follows: + +Take any positive integer n. +If n is even, divide n by 2 to get n / 2. +If n is odd, multiply n by 3 and add 1 to get 3n + 1. +Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will always reach 1 eventually. + +Given a number n, return the number of steps required to reach 1. + +## Examples + +Starting with n = 12, the steps would be as follows: + +0. 12 +1. 6 +2. 3 +3. 10 +4. 5 +5. 16 +6. 8 +7. 4 +8. 2 +9. 1 + +Resulting in 9 steps. +So for input n = 12, the return value would be 9. diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json new file mode 100644 index 0000000..121cc03 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "collatz_conjecture.vim" + ], + "test": [ + "collatz_conjecture.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", + "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" +} diff --git a/exercises/practice/collatz-conjecture/.meta/example.vim b/exercises/practice/collatz-conjecture/.meta/example.vim new file mode 100644 index 0000000..5708238 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/example.vim @@ -0,0 +1,19 @@ +function! Steps(number) abort + if a:number < 1 + throw 'Only positive integers are allowed' + endif + + let l:step = 0 + let l:working = a:number + while l:working != 1 + if l:working % 2 == 0 + let l:working /= 2 + else + let l:working = 3 * l:working + 1 + endif + + let l:step += 1 + endwhile + + return l:step +endfunction 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.vader b/exercises/practice/collatz-conjecture/collatz_conjecture.vader new file mode 100644 index 0000000..ddd0234 --- /dev/null +++ b/exercises/practice/collatz-conjecture/collatz_conjecture.vader @@ -0,0 +1,32 @@ + +Execute (zero steps for one): + let g:number = 1 + let g:expected = 0 + AssertEqual g:expected, Steps(g:number) + +Execute (divide if even): + let g:number = 16 + let g:expected = 4 + AssertEqual g:expected, Steps(g:number) + +Execute (even and odd steps): + let g:number = 12 + let g:expected = 9 + AssertEqual g:expected, Steps(g:number) + +Execute (large number of even and odd steps): + let g:number = 1000000 + let g:expected = 152 + AssertEqual g:expected, Steps(g:number) + +Execute (zero is an error): + let g:number = 0 + let g:expected = "Only positive integers are allowed" + AssertThrows call Steps(g:number) + AssertEqual g:expected, g:vader_exception + +Execute (negative value is an error): + let g:number = -15 + let g:expected = "Only positive integers are allowed" + AssertThrows call Steps(g:number) + AssertEqual g:expected, g:vader_exception diff --git a/exercises/practice/collatz-conjecture/collatz_conjecture.vim b/exercises/practice/collatz-conjecture/collatz_conjecture.vim new file mode 100644 index 0000000..0590875 --- /dev/null +++ b/exercises/practice/collatz-conjecture/collatz_conjecture.vim @@ -0,0 +1,16 @@ +" +" Returns the number of steps to reach 1 for a given number +" using the Collatz Conjecture or 3x+1 problem. +" Throws an error if input is less than 1. +" +" Example: +" +" :echo Steps(16) +" 4 +" +" :echo Steps(-1) +" E605: Exception not caught: Only positive integers are allowed +" +function! Steps(number) abort + " your solution goes here +endfunction