File tree Expand file tree Collapse file tree 4 files changed +36
-20
lines changed
Expand file tree Collapse file tree 4 files changed +36
-20
lines changed Original file line number Diff line number Diff line change @@ -24,16 +24,27 @@ def get_integer():
2424
2525
2626def addition (num ):
27+ """
28+ Returns the sum of the digits of a number.
29+ Negative numbers are handled using the absolute value.
30+
31+ Examples:
32+ >>> addition(123)
33+ 6
34+ >>> addition(-784)
35+ 19
36+ """
2737 Sum = 0
2838 if type (num ) is type (
2939 None
3040 ): # Checks if number type is none or not. If type is none program exits.
3141 print ("Try again!" )
3242 sys .exit ()
43+ num = abs (num ) # Handle negative numbers
3344 while num > 0 : # Addition- adding the digits in the number.
3445 digit = int (num % 10 )
3546 Sum += digit
36- num /= 10
47+ num // = 10
3748 return Sum # Returns sum to where the function is called.
3849
3950
@@ -42,4 +53,5 @@ def addition(num):
4253): # this is used to overcome the problems while importing this file.
4354 number = get_integer ()
4455 Sum = addition (number )
45- print (f"Sum of digits of { number } is { Sum } " ) # Prints the sum
56+ abs_display = f" (absolute value: { abs (number )} )" if number < 0 else ""
57+ print (f"Sum of digits of { number } { abs_display } is { Sum } " ) # Prints the sum
Original file line number Diff line number Diff line change 11# User inputs the string and it gets stored in variable str
2- str = input ("Enter a string: " )
2+ str = input ("Enter a string for str leangth : " )
33
44# counter variable to count the character in a string
55counter = 0
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Tubes==0.2.1
1010modules==1.0.0
1111pdf2docx==0.5.8
1212pong==1.5
13- beautifulsoup4==4.14.2
13+ beautifulsoup4==4.14.3
1414dictator==0.3.1
1515caller==0.0.2
1616watchdog==6.0.0
@@ -21,14 +21,14 @@ backend==0.2.4.1
2121win10toast==0.9
2222Counter==1.0.0
2323Flask==3.1.2
24- selenium==4.38 .0
24+ selenium==4.39 .0
2525firebase-admin==7.1.0
2626ujson==5.10.0
2727requests==2.32.5
2828quo==2023.5.1
2929PyPDF2==3.0.1
3030pyserial==3.5
31- twilio==9.8.5
31+ twilio==9.8.8
3232tabula==1.0.5
3333nltk==3.9.2
3434Pillow==12.0.0
@@ -37,26 +37,26 @@ xlrd==2.0.2
3737fpdf==1.7.2
3838mysql-connector-repackaged==0.3.1
3939word2number==1.1
40- tornado==6.5.2
40+ tornado==6.5.3
4141obs==0.0.0
4242todo==0.1
4343oauth2client==4.1.3
4444keras==3.12.0
45- pymongo==4.15.4
45+ pymongo==4.15.5
4646playsound==1.3.0
4747pyttsx3==2.99
4848auto-mix-prep==0.2.0
4949lib==4.0.0
5050pywifi==1.1.12
5151patterns==0.3
52- openai==2.8.1
52+ openai==2.9.0
5353background==0.2.1
54- pydantic==2.12.4
54+ pydantic==2.12.5
5555openpyxl==3.1.2
5656pytesseract==0.3.13
5757requests-mock==1.12.1
5858pyglet==2.1.11
59- urllib3==2.5.0
59+ urllib3==2.6.2
6060thirdai==0.9.33
6161google-api-python-client==2.187.0
6262sound==0.1.0
@@ -81,12 +81,12 @@ Unidecode==1.4.0
8181Ball==0.2.9
8282pynput==1.8.1
8383gTTS==2.5.4
84- ccxt==4.5.22
84+ ccxt==4.5.27
8585fitz==0.0.1.dev2
86- fastapi==0.122.0
87- Django==5.2.7
86+ fastapi==0.124.4
87+ Django==6.0
8888docx==0.2.4
89- matplotlib==3.10.7
89+ matplotlib==3.10.8
9090pyshorteners==1.0.1
9191geocoder==1.38.1
9292APScheduler==3.11.1
@@ -97,15 +97,15 @@ newspaper==0.1.0.7
9797opencv-python==4.12.0.88
9898tensorflow==2.20.0
9999pandas==2.3.3
100- pytest==8.4 .2
100+ pytest==9.0 .2
101101qrcode==8.2
102102googletrans==4.0.2
103103slab==1.8.2
104104psutil==7.1.3
105105mediapipe==0.10.21
106106rich==14.2.0
107107httplib2==0.31.0
108- protobuf==6.33.1
108+ protobuf==6.33.2
109109colorama==0.4.6
110110plyer==2.1.0
111111Flask-Ask==0.9.8
Original file line number Diff line number Diff line change 1313 0
1414 >>> sum_of_digits(999)
1515 27
16+ >>> sum_of_digits(-123)
17+ 6
1618"""
1719
1820import sys
@@ -49,8 +51,8 @@ def sum_of_digits(n: int) -> int:
4951 Compute the sum of the digits of an integer.
5052
5153 Args:
52- n:Non-negative integer
53- If the integer is negative , it is converted to postive interger and assigned to same number
54+ n: Non-negative integer.
55+ If the integer is negative, it is converted to positive before computing the sum.
5456
5557 Returns:
5658 Sum of digits of the number.
@@ -60,8 +62,10 @@ def sum_of_digits(n: int) -> int:
6062 6
6163 >>> sum_of_digits(405)
6264 9
65+ >>> sum_of_digits(-789)
66+ 24
6367 """
64- n = abs (n )
68+ n = abs (n ) # FIX: handle negative numbers
6569 total = 0
6670 while n > 0 :
6771 # Add last digit and remove it from n
You can’t perform that action at this time.
0 commit comments