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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,18 @@
"topics": [

]
},
{
"slug": "collatz-conjecture",
"difficulty": 1,
"topics": [
"Control-flow (loops)",
"Control-flow (conditionals)",
"Recursion",
"Integers",
"Algorithms",
"Mathematics"
]
}
],
"deprecated": [
Expand Down
1 change: 1 addition & 0 deletions exercises/collatz-conjecture/.meta/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'generator/exercise_case'

class CollatzConjectureCase < Generator::ExerciseCase
using Generator::Underscore

def workload
case expected
when Fixnum
standard_assertion
when Hash
error_assertion
end
end

def standard_assertion
assert_equal { subject_of_test }
end

def error_assertion
"assert_raises(ArgumentError) { #{subject_of_test} }"
end

def subject_of_test
"CollatzConjecture.steps(#{input})"
end

def input
number.underscore
end
end

22 changes: 22 additions & 0 deletions exercises/collatz-conjecture/.meta/solutions/collatz_conjecture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module BookKeeping
VERSION = 1
end

module CollatzConjecture
def self.steps(num)
raise ArgumentError if num < 1
n = num
steps = 0
until n == 1
steps += 1
if n.even?
n /= 2
else
n = 3 * n + 1
end
end
steps
end
end


58 changes: 58 additions & 0 deletions exercises/collatz-conjecture/collatz_conjecture_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby
require 'minitest/autorun'
require_relative 'collatz_conjecture'

# Common test data version: 1.1.1 25c4479
class CollatzConjectureTest < Minitest::Test
def test_zero_steps_for_one
# skip
assert_equal 0, CollatzConjecture.steps(1)
end

def test_divide_if_even
skip
assert_equal 4, CollatzConjecture.steps(16)
end

def test_even_and_odd_steps
skip
assert_equal 9, CollatzConjecture.steps(12)
end

def test_large_number_of_even_and_odd_steps
skip
assert_equal 152, CollatzConjecture.steps(1_000_000)
end

def test_zero_is_an_error
skip
assert_raises(ArgumentError) { CollatzConjecture.steps(0) }
end

def test_negative_value_is_an_error
skip
assert_raises(ArgumentError) { CollatzConjecture.steps(-15) }
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end