Objective: Solve problems related to calculating factorials, including writing functions, analyzing performance, and debugging code.
Format:
Complete multiple questions which may include coding, theoretical answers, and debugging.
Requirements:
Implement factorial functions (iterative and recursive).
Analyze time complexity and optimize performance.
Debug and refactor existing code if needed.
Answer theoretical questions about factorials.
Submission:
Submit your solutions according to the provided guidelines.
Ensure correctness, efficiency, and clarity.
Time Limit:
Complete the quiz within [Insert Time Limit].
Resources:
Use online resources for reference, but ensure your answers are your own work.
Good luck!
import math
import time
def calculate_factorial_time(n):
if n < 0:
return "Factorial is not defined for negative numbers", 0
start_time = time.time()
factorial_result = math.factorial(n)
end_time = time.time()
calculation_time = end_time - start_time
return factorial_result, calculation_time
import codewars_test as test
from solution import calculate_factorial_time
@test.describe("Fixed tests")
def fixed_tests():
@test.it("Testing factorial of 0")
def test_factorial_0():
result, time_taken = calculate_factorial_time(0)
test.assert_equals(result, 1)
test.assert_equals(time_taken >= 0, True)
@test.it("Testing factorial of 1")
def test_factorial_1():
result, time_taken = calculate_factorial_time(1)
test.assert_equals(result, 1)
test.assert_equals(time_taken >= 0, True)
@test.it("Testing factorial of 5")
def test_factorial_5():
result, time_taken = calculate_factorial_time(5)
test.assert_equals(result, 120)
test.assert_equals(time_taken >= 0, True)
@test.it("Testing factorial of 10")
def test_factorial_10():
result, time_taken = calculate_factorial_time(10)
test.assert_equals(result, 3628800)
test.assert_equals(time_taken >= 0, True)
@test.it("Testing factorial of 20")
def test_factorial_20():
result, time_taken = calculate_factorial_time(20)
expected_result = 2432902008176640000
test.assert_equals(result, expected_result)
test.assert_equals(time_taken >= 0, True)
@test.it("Testing factorial of negative number")
def test_factorial_negative():
result, time_taken = calculate_factorial_time(-5)
test.assert_equals(result, "Factorial is not defined for negative numbers")
test.assert_equals(time_taken >= 0, True)