This repository contains a set of Python functions that implement common algorithms and programming challenges. Your task is to implement and thoroughly test each function, including edge cases.
Description: Calculates the factorial of a non-negative integer.
- Returns the factorial of n (n!)
- Should handle edge cases like 0! = 1
- Should raise appropriate errors for negative numbers
Examples:
factorial(0) # -> 1
factorial(1) # -> 1
factorial(5) # -> 120
factorial(10) # -> 3628800Description: Returns the nth number in the Fibonacci sequence.
- The Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
- Should handle edge cases for n = 0 and n = 1
Examples:
fibonacci(0) # -> 0
fibonacci(1) # -> 1
fibonacci(5) # -> 5
fibonacci(10) # -> 55Description: Returns a list for the FizzBuzz game up to n.
- Multiples of 3 are "Fizz"
- Multiples of 5 are "Buzz"
- Multiples of both 3 and 5 are "FizzBuzz"
- All other numbers remain as integers
Examples:
fizzbuzz(15) # -> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
fizzbuzz(5) # -> [1, 2, "Fizz", 4, "Buzz"]-
Implement all functions in the main Python file (
main.py). -
Write comprehensive tests for all functions, including edge cases, such as:
- Negative numbers for factorial
- Large numbers for fibonacci
- Edge cases like n=0 and n=1
- Invalid inputs
-
Place all test cases in a separate file:
tests/test.py -
Use Python's built-in unittest framework for testing. Example:
import unittest
from main import factorial, fibonacci, fizzbuzz
class TestAlgorithmFunctions(unittest.TestCase):
def test_factorial_zero(self):
self.assertEqual(factorial(0), 1)
def test_factorial_positive(self):
self.assertEqual(factorial(5), 120)
def test_fibonacci_base_cases(self):
self.assertEqual(fibonacci(0), 0)
self.assertEqual(fibonacci(1), 1)
def test_fizzbuzz_basic(self):
result = fizzbuzz(5)
expected = [1, 2, "Fizz", 4, "Buzz"]
self.assertEqual(result, expected)
if __name__ == "__main__":
unittest.main()- Ensure all tests pass before submission.
- Python 3.6 or higher
python main.pypython -m unittest tests.testOr run tests with verbose output:
python -m unittest tests.test -vUnit-test-exercise1/
├── main.py # Main implementation file
├── tests/
│ └── test.py # Test cases
└── README.md # This file
- n = 0 (should return 1)
- n = 1 (should return 1)
- Negative numbers (should raise ValueError)
- Large numbers (consider performance)
- n = 0 (should return 0)
- n = 1 (should return 1)
- Negative numbers (should raise ValueError)
- Large numbers (consider performance and algorithm efficiency)
- n = 0 (should return empty list)
- n = 1 (should return [1])
- Negative numbers (should raise ValueError)
- Large numbers (memory considerations)
- Test normal cases and edge cases
- Test error conditions (like non-string input for
count_unique) - Use descriptive test method names
- Include comments explaining complex test scenarios
- Aim for high test coverage
import unittest
from main import factorial, fibonacci, fizzbuzz
class TestAlgorithmFunctions(unittest.TestCase):
def setUp(self):
"""Set up test fixtures before each test method."""
pass
def tearDown(self):
"""Clean up after each test method."""
pass
# Test cases for each function...
if __name__ == "__main__":
unittest.main()