Normalize Number String

Normalize Number String

Hard Python Data Types 23 views
Explanation Complexity

Problem Statement

A number is provided as string. It may have + sign, leading zeros, and decimal part. Normalize it: remove +, remove extra leading zeros, remove trailing zeros in decimal, and if it becomes -0 or 0.0 then output 0.

Input Format

• First line: A string representing a number

• It may contain:

• Optional + sign

• Leading zeros

• Decimal part

Output Format

• Print the normalized number string after:

• Removing + sign

• Removing extra leading zeros

• Removing trailing zeros in decimal part

• Removing trailing decimal point (if any)

• If result becomes -0, 0.0, -0.0, etc., output 0

Example

+0012.3400
12.34

Constraints

• 1 ≤ length ≤ 10^5

• Input is a valid numeric string

Concept Explanation

Input: +00012.3400

Steps:

• Remove + → 00012.3400

• Remove leading zeros → 12.3400

• Remove trailing zeros in decimal → 12.34

Final result → 12.34

If input was -0000.000,
it becomes 0.

Step-by-Step Explanation

1.Read the string s.

2.If it starts with +, remove it.

3.Check if it starts with -, store the sign and remove it temporarily.

4.Split into integer and decimal parts if . exists.

5.Remove leading zeros from integer part.

• If empty, set integer part to "0".

6.If decimal part exists:

• Remove trailing zeros from decimal part.

• If decimal part becomes empty, remove the dot.

7.Reconstruct the number with sign (if negative).

8.If final value equals 0 (like 0, -0, 0.0, etc.), output "0".

9.Otherwise, print the normalized string.

Concept Explanation

Input: +00012.3400

Steps:

• Remove + → 00012.3400

• Remove leading zeros → 12.3400

• Remove trailing zeros in decimal → 12.34

Final result → 12.34

If input was -0000.000,
it becomes 0.

Step-by-Step Explanation

1.Read the string s.

2.If it starts with +, remove it.

3.Check if it starts with -, store the sign and remove it temporarily.

4.Split into integer and decimal parts if . exists.

5.Remove leading zeros from integer part.

• If empty, set integer part to "0".

6.If decimal part exists:

• Remove trailing zeros from decimal part.

• If decimal part becomes empty, remove the dot.

7.Reconstruct the number with sign (if negative).

8.If final value equals 0 (like 0, -0, 0.0, etc.), output "0".

9.Otherwise, print the normalized string.

Input / Output Format

Input Format
• First line: A string representing a number

• It may contain:

• Optional + sign

• Leading zeros

• Decimal part
Output Format
• Print the normalized number string after:

• Removing + sign

• Removing extra leading zeros

• Removing trailing zeros in decimal part

• Removing trailing decimal point (if any)

• If result becomes -0, 0.0, -0.0, etc., output 0
Constraints
• 1 ≤ length ≤ 10^5

• Input is a valid numeric string

Examples

Input:
+0012.3400
Output:
12.34

Example Solution (Public)

Python
import sysns=sys.stdin.read().strip()nif not s: sys.exit(0)nneg=Falsenif s[0]=='+':n  s=s[1:]nelif s[0]=='-':n  neg=Truen  s=s[1:]nif '.' in s:n  a,b=s.split('.',1)nelse:n  a,b=s,''na=a.lstrip('0')nif a=='':n  a='0'nif b!='':n  b=b.rstrip('0')nres=anif b!='':n  res=a+'.'+bnif res=='0' or res=='0.':n  res='0'nif res=='0':n  sys.stdout.write('0')nelse:n  sys.stdout.write(('-' if neg else '')+res)

Official Solution Code

import sysns=sys.stdin.read().strip()nif not s: sys.exit(0)nneg=Falsenif s[0]=='+':n  s=s[1:]nelif s[0]=='-':n  neg=Truen  s=s[1:]nif '.' in s:n  a,b=s.split('.',1)nelse:n  a,b=s,''na=a.lstrip('0')nif a=='':n  a='0'nif b!='':n  b=b.rstrip('0')nres=anif b!='':n  res=a+'.'+bnif res=='0' or res=='0.':n  res='0'nif res=='0':n  sys.stdout.write('0')nelse:n  sys.stdout.write(('-' if neg else '')+res)
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.