Add support for negative numbers in sum_of_digits function
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 74/100
Hướng nghiên cứu
Mở sum_of_digits.py hoặc tệp liên quan và tìm hàm sum_of_digits cùng cách xử lý hiện tại đối với đầu vào số nguyên. Xác minh các trường hợp được liệt kê, bao gồm các giá trị âm và số 0, đồng thời cập nhật tài liệu hoặc các ví dụ nếu có; hoàn tất khi đầu vào âm trả về tổng các chữ số của giá trị tuyệt đối mà không làm hỏng các trường hợp hiện có.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
🐛 Current Behavior
The sum_of_digits() function currently only works with non-negative integers. When a negative number is passed, it enters an infinite loop or produces incorrect results.
python:
Current implementation fails with negative numbers
sum_of_digits(-123) # Does not work correctly
✅ Expected Behavior
The function should handle negative numbers by computing the sum of digits using the absolute value.
python
sum_of_digits(-123) # Should return 6 (1 + 2 + 3)
sum_of_digits(-456) # Should return 15 (4 + 5 + 6)
💡 Proposed Solution
Add abs() to handle negative numbers in the sum_of_digits function:
def sum_of_digits(n: int) -> int:
"""
Compute the sum of the digits of an integer.
Args:
n: An integer (negative values are converted to absolute).
Returns:
Sum of digits of the absolute value of the number.
Examples:
>>> sum_of_digits(123)
6
>>> sum_of_digits(-789)
24
"""
n = abs(n) # Handle negative numbers
total = 0
while n > 0:
total += n % 10
n //= 10
return total
📋 Additional Improvements (Optional)
Update docstring to clarify behavior with negative numbers
Add doctests for negative numbers
Update main() to display absolute value when input is negative:
python
abs_display = f" (absolute value: {abs(number)})" if number < 0 else ""
print(f"The sum of the digits of {number}{abs_display} is: {result}")
🧪 Test Cases
sum_of_digits(123) → 6
sum_of_digits(-123) → 6
sum_of_digits(0) → 0
sum_of_digits(-999) → 27
sum_of_digits.py (or the relevant file name)
🏷️ Labels
enhancement, good first issue, bug
- Ngôn ngữ chính
- Python
- Star
- 35.4k
- Fork
- 12.9k
- Merge trung bình
- 2 giờ 37 phút
- Pull request đã merge (30 ngày)
- 1
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của geekcomputers/Python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
geekcomputers/Python#3205 · 2 bình luận ·
-
Add number_guesse.py feature Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
geekcomputers/Python#3188 · 3 bình luận ·
-
[BUG] shapchat Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 10/100
geekcomputers/Python#3214 · 5 bình luận ·
-
Test of an Issue Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 10/100
geekcomputers/Python#3209 · 5 bình luận ·
-
Add a feature by AI Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 10/100
geekcomputers/Python#3185 ·
Tất cả issue của geekcomputers/Python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
bancolombia/sentinel#23 ·
-
test md Đang mởCI
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100