-
Notifications
You must be signed in to change notification settings - Fork 5
Create simple_utils.py #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
# simple_utils.py - A tiny utility libraryq | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def reverse_string(text): | ||||||||||||||||||||||||||||||||||||||||||||||
"""Reverses the characters in a string.""" | ||||||||||||||||||||||||||||||||||||||||||||||
return text[::-1] | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def count_words(sentence): | ||||||||||||||||||||||||||||||||||||||||||||||
return len(sentence.split()) | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add documentation and input validation. The function lacks documentation and input validation. Consider the following improvements: def count_words(sentence):
+ """Counts the number of words in a given text.
+
+ Args:
+ sentence (str): The text to count words in.
+
+ Returns:
+ int: The number of words found.
+
+ Example:
+ >>> count_words("Hello world")
+ 2
+ >>> count_words(" multiple spaces ")
+ 2
+ """
+ if sentence is None:
+ raise ValueError("Input cannot be None")
+ if not isinstance(sentence, str):
+ raise TypeError("Input must be a string")
return len(sentence.split()) 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def celsius_to_fahrenheit(celsius): | ||||||||||||||||||||||||||||||||||||||||||||||
return (celsius * 9/5) + 32 | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add documentation, input validation, and type hints. The conversion formula is mathematically correct, but the function needs documentation and input validation for robustness. -def celsius_to_fahrenheit(celsius):
+def celsius_to_fahrenheit(celsius: float) -> float:
+ """Converts temperature from Celsius to Fahrenheit.
+
+ Args:
+ celsius (float): Temperature in Celsius.
+
+ Returns:
+ float: Temperature in Fahrenheit.
+
+ Example:
+ >>> celsius_to_fahrenheit(0)
+ 32.0
+ >>> celsius_to_fahrenheit(100)
+ 212.0
+ """
+ if celsius is None:
+ raise ValueError("Input cannot be None")
+ if not isinstance(celsius, (int, float)):
+ raise TypeError("Input must be a number")
return (celsius * 9/5) + 32 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation and improve documentation.
The implementation is correct and Pythonic, but consider adding input validation to handle edge cases like
None
values or non-string types.📝 Committable suggestion
🤖 Prompt for AI Agents