Click here to Skip to main content
14,836,169 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Rahul is facing a unique problem, which he doesn't know how to solve. The problem asks him to build the smallest possible number by applying inversion on any digit of the number any number of times.
Inversion of a digit is defined as that digit being replaced by 9 minus that digit. Meaning that inversion of 9 will be 9 - 9 = 0 and inversion of 1 will be 9 - 1 = 8 and so on.

The final output should not have any leading zeroes.

Input Format:
The only line of the input contains an integer N

Output Format:
Print only one single integer on a line as described above.

Constraints:
1 <= N <= 10^18

Examples:
Input:
87
Output:
12

Explanation:

9-8 = 1

and 9 -7 = 2

if we see carefully, 12 is the smallest possible number we can get.

What I have tried:

Python
x=input()
1 <= int(x) <= 10^18
for i in range(1,len(x)):
    x[i]=int(x[i])
    if x[i]>5:
        x[i]=9-x[i]
if x[0]==9:
    x[0]==9
elif x[0]>5:
    x[0]=9-x[i]
else:
    x[0]=x[0]
print(x)
Posted
Updated 18-Nov-20 14:57pm
v4
Comments
Gerry Schmitz 18-Nov-20 12:18pm
   
You need to show your "input", and what you got as "output", and what you "expected". We don't do "desk checking" for nothing.

1 solution

Here is the basic code for inverting a number:
Python
answer = ''
x=input()
for i in range(len(x)):
    digit = 9 - int(x[i])
    answer = answer + str(digit)
print(answer)

You may need to modify that to take account of any other rules you are supposed to follow. By the way the line
Python
1 <= int(x) <= 10^18

does not actually do anything. It is a good idea to test your code as you write it, and try and fix any errors immediately as you see them.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900