diff --git a/LEARNINGS-tngwafor.md b/LEARNINGS-tngwafor.md new file mode 100644 index 0000000..ab0282a --- /dev/null +++ b/LEARNINGS-tngwafor.md @@ -0,0 +1,85 @@ +# LEARNINGS-tngwafor + +## CP1 +### Commands +```bash +git checkout starter/role-D +git checkout -b feature/tngwafor +git push -u origin feature/tngwafor +pytest tests/ +git add tests/test_calculator.py +git commit -m "fix" +git add .gitignore +git commit -m "update" +git push origin feature/tngwafor + +Reflection : +I learned how to use the staging area to separate unrelated changes into different commits. This matters because clean commits make code review and debugging much easier. + +Checkpoint 2 +git log --oneline +git revert 6ca467b +pytest tests/ +git push origin feature/tngwafor + +Reflection : +I learned how to undo a bad commit with git revert without deleting history. This matters because it preserves a clear project history while safely reversing unwanted changes. + +Checkpoint 3 +git checkout main +git add src/validator.py +git commit -m "Add is_positive helper function" +git log --oneline -1 +git reset --hard HEAD~1 +git checkout feature/tngwafor +git cherry-pick 8611771 +git add src/validator.py +git cherry-pick --continue +pytest tests/ +git push origin feature/tngwafor + +Reflection : +I learned how to move work from the wrong branch to the correct one using git cherry-pick. This matters because mistakes happen, and knowing how to recover cleanly helps keep branches organized. + + +Checkpoint 4 : +git checkout -b experiment/tngwafor +git add src/validator.py +git commit -m "Add experimental comment" +git checkout feature/tngwafor +git branch -D experiment/tngwafor +git reflog +git checkout -b recovered/tngwafor +git checkout feature/tngwafor +git merge recovered/tngwafor +pytest tests/ +git push origin feature/tngwafor + +Reflection : +I learned that git reflog can recover commits even after a branch is deleted. This matters because it provides a safety net when work seems lost. + +Checkpoint 5 : +git fetch upstream +git checkout main +git reset --hard upstream/main +git checkout feature/tngwafor +git rebase main +pytest tests/ +git push --force-with-lease origin feature/tngwafor + + +Reflection : +I learned how to sync my branch with the latest upstream changes using fetch, reset, and rebase. This matters because it keeps my work up to date and reduces messy merge history. + + +Checkpoint 6 : +git log main..HEAD --oneline +git rebase -i main +pytest tests/ +git push --force-with-lease origin feature/tngwafor + + +Reflection : +I learned how to clean up commit history with interactive rebase by rewording and organizing commits. This matters because a clean history makes the development process easier to understand and review. + + diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..bc68cdc --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +_pychache_/ +*.pyc diff --git a/src/calculator.py b/src/calculator.py index e828684..ed68549 100644 --- a/src/calculator.py +++ b/src/calculator.py @@ -17,3 +17,14 @@ def divide(a, b): if b == 0: raise ValueError("Cannot divide by zero") return a / b + +def factorial(n): + """Calculate factorial of n.""" + if n < 0: + raise ValueError("Cannot calculate factorial of negative number") + if n == 0 or n == 1: + return 1 + result = 1 + for i in range(2, n + 1): + result *= i + return result diff --git a/src/validator.py b/src/validator.py index 7a70d84..ca0db77 100644 --- a/src/validator.py +++ b/src/validator.py @@ -12,3 +12,17 @@ def validate_operation(op): """Validate that operation is supported.""" valid_ops = ['+', '-', '*', '/'] return op in valid_ops + +def validate_integer(n): + """Validate that a number is an integer.""" + try: + num = float(n) + return num == int(num) + except (ValueError, TypeError): + return False + +def is_positive(n): + """Check if a number is positive.""" + return n > 0 + + #Change for checkpoint 4 \ No newline at end of file diff --git a/tests/test_calculator.py b/tests/test_calculator.py index ffc0154..02f451c 100644 --- a/tests/test_calculator.py +++ b/tests/test_calculator.py @@ -1,6 +1,7 @@ """Tests for calculator operations.""" import pytest -from src.calculator import add, subtract, multiply, divide +from src.calculator import add, subtract, multiply, divide, factorial +from src.validator import validate_integer def test_add(): assert add(2, 3) == 5 @@ -21,3 +22,17 @@ def test_divide(): def test_divide_by_zero(): with pytest.raises(ValueError): divide(5, 0) + +def test_factorial(): + assert factorial(0) == 1 + assert factorial(5) == 120 + assert factorial(3) == 6 + +def test_factorial_negative(): + with pytest.raises(ValueError): + factorial(-1) + +def test_validate_integer(): + assert validate_integer(5) == True + assert validate_integer(5.0) == True + assert validate_integer(5.5) == False