Skip to content
Open
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
16 changes: 13 additions & 3 deletions lib/factorial.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Computes factorial of the input number and returns it
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the number
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does time relate to the number?

# Space complexity: O(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain your reasoning for constant space.

def factorial(number)
raise NotImplementedError
return 1 if number == 0
return 1 if number == 1
raise ArgumentError if number == nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone put in factorial("2") you'd get an exception. Consider checks such as if number.is_a?(Integer)

fact = 1
index = 1
while index < number
fact *= (index + 1)
index += 1
end

return fact
end
12 changes: 8 additions & 4 deletions specs/factorial_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/factorial'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/factorial"

describe "factorial" do
describe "basic tests" do
it "factorial(5) = 120" do
factorial(5).must_equal 120
end

it "factorial(3) = 6" do
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition of extra testing!

factorial(3).must_equal 6
end

it "factorial(7) = 5040" do
factorial(7).must_equal 5040
end
Expand All @@ -17,7 +21,7 @@
describe "edge cases" do
# if the parameter is an object, check for nil
it "nil object is not an integer" do
proc {factorial(nil)}.must_raise ArgumentError
proc { factorial(nil) }.must_raise ArgumentError
end

it "factorial(0) = 1" do
Expand Down