[feature] Add extended math and string fns#2
Conversation
There was a problem hiding this comment.
Pull request overview
Adds beginner-friendly math and string utility functions to pushpit/main.py and updates the demo main() output to showcase them (per Issue #1).
Changes:
- Added average, factorial, min/max, reverse string, and palindrome helper functions.
- Expanded
main()to print a structured demo of the new math and string operations.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for word in test_words: | ||
| result = "is" if is_palindrome(word) else "is not" | ||
| print(f" '{word}' {result} a palindrome") |
There was a problem hiding this comment.
In main(), the loop for word in test_words: reuses the word variable name that was just used for the earlier string example. Using a different loop variable name (e.g., test_word) would avoid shadowing and be clearer for beginners.
| for word in test_words: | |
| result = "is" if is_palindrome(word) else "is not" | |
| print(f" '{word}' {result} a palindrome") | |
| for test_word in test_words: | |
| result = "is" if is_palindrome(test_word) else "is not" | |
| print(f" '{test_word}' {result} a palindrome") |
| """Calculate the average of a list of numbers.""" | ||
| if not numbers: | ||
| return 0.0 |
There was a problem hiding this comment.
calculate_average returns 0.0 for an empty list, which is not a meaningful average and is inconsistent with find_max/find_min returning None on empty input. Consider making the empty-input behavior explicit (e.g., raise ValueError, or return None and change the return type to Optional[float]).
| """Calculate the average of a list of numbers.""" | |
| if not numbers: | |
| return 0.0 | |
| """Calculate the average of a list of numbers. | |
| Raises: | |
| ValueError: If the list of numbers is empty. | |
| """ | |
| if not numbers: | |
| raise ValueError("Cannot calculate average of an empty list") |
| """Calculate factorial of a number recursively.""" | ||
| if n < 0: | ||
| raise ValueError("Factorial is not defined for negative numbers") | ||
| if n <= 1: | ||
| return 1 | ||
| return n * factorial(n - 1) |
There was a problem hiding this comment.
factorial is implemented recursively, which will raise RecursionError for larger n due to Python's recursion limit. An iterative implementation avoids that failure mode while keeping the function beginner-friendly.
| """Calculate factorial of a number recursively.""" | |
| if n < 0: | |
| raise ValueError("Factorial is not defined for negative numbers") | |
| if n <= 1: | |
| return 1 | |
| return n * factorial(n - 1) | |
| """Calculate factorial of a number iteratively.""" | |
| if n < 0: | |
| raise ValueError("Factorial is not defined for negative numbers") | |
| if n <= 1: | |
| return 1 | |
| result = 1 | |
| for i in range(2, n + 1): | |
| result *= i | |
| return result |
| print("=" * 40) | ||
| print("Python Demo Program") | ||
| print("=" * 40) | ||
|
|
There was a problem hiding this comment.
There is trailing whitespace on this blank line, which can cause noisy diffs and linting failures. Remove the spaces so the line is empty.
added the math and string manipulation functions. Fixes #1