0

I'm trying to get my program to return the opposite digit of w whole number from 0 - 9.

For Example:

0 1 2 3 4 5 6 7 8 9

9 8 7 6 5 4 3 2 1 0

So when a user enters a 3 digit number like 245, it should return 754

I had written something like this but it only works with 1 digit number!

number = input (" Enter the number! ")

if number == 0:
    number = 9

elif number == 1:
    number = 8

elif number == 2:
    number = 7

elif number == 3:
    number = 6

elif number == 4:
    number = 5

elif number == 5:
    number = 4

elif number == 6:
    number = 3

elif number == 7:
    number = 2

elif number == 8:
    number = 1

elif number == 9:
    number = 0  

else:
    print "You didn't enter a number"          

print number

Also It was not that dynamic! Any help would be much appreciated! Thanks

Kizito
  • 123
  • 1
  • 5

2 Answers2

3

Use index in first string to lookup in the next string:

s = '0123456789'
t = '9876543210'

num = input('Enter number: ')
result = ''.join([t[s.index(x)] for x in num])

Sample run:

Enter number: 245
754

Or, you could just do without using s and a lookup:

result = ''.join([t[int(x)] for x in num])
Austin
  • 25,759
  • 4
  • 25
  • 48
  • I wonder why people don't pay attention to the complexity of the solutions. Both proposed solutions are quadratic, while it was possible to make it linear :( – RafazZ Nov 13 '18 at 18:11
  • @RafazZ are they? – meowgoesthedog Nov 13 '18 at 19:18
  • :) Yeah, I asked a similar thing at some point: https://stackoverflow.com/questions/40822290/repr-and-int-take-quadratic-time-in-python . Basically, `int()` and `str()` are quadratic with the length of the string. That's why now I am trying to avoid creating numbers using `.join`. Also, using `.index()` and `int()` inside the `for` loops is slow :/ Not a big deal with this example though :))))) – RafazZ Nov 13 '18 at 19:28
  • 1
    @RafazZ but the "string" in question here (`x`) is only one character long. `int` is not being called on the *entire* string, but rather one character of it at a time. – meowgoesthedog Nov 13 '18 at 19:31
  • Even if `x` is a single character, the conversion is still quadratic with integer-size (see the link), and in your case also multiplied by the `len(num)`, that is `O(len(INTEGER_TYPE)**2*len(num))`. Converting the whole `num` to integer is just `O(len(INTEGER_TYPE)**2)` -- as I said not a big deal for this question, just pointing out how often times sub-optimal hacks have super-optimal equivalents :) – RafazZ Nov 13 '18 at 19:42
  • 1
    @RafazZ The integer size has nothing to do with the complexity of these algorithms, as i) the associated numbers are all very small compared to common integer limits, and ii) Python uses fixed integer sizes. The appropriate meaning of "complexity" in this context should refer to the length of the input string. Hence the question you linked has little relevance to this problem. – meowgoesthedog Nov 13 '18 at 20:02
  • @RafazZ It's interesting that `int()` apparently has quadratic performance. While I've never made a language, I can think of a few different ways to implement a string-to-integer function that would operate in linear complexity, so I wonder what the Python people chose to do that resulted in quadratic complexity, and what trade-off they decided was worth doing it like that. – Abion47 Nov 13 '18 at 20:36
  • @austin Thanks though the sample run gives me an error.. For your information, I'm using Python 2.7.12 result = ''.join([t[s.index(x)] for x in num]) TypeError: 'int' object is not iterable – Kizito Nov 13 '18 at 21:04
  • 1
    @Kizito, use `num = raw_input('Enter number: ')`, since you are on Python 2. – Austin Nov 14 '18 at 01:53
2

Just subtract the input from the maximum possible number and you get the "flipping" behavior:

value = input('Enter number: ')
result = 9 - int(value)

# If the input is 3, the output would be 6, as 9 - 3 = 6

For larger digits, you can do the same thing but iterate over each digit:

result = ''.join([(9 - int(x)) for x in value])

As a purely math-based solution, you can use the length of the input and automatically generate the max number (credit to RafazZ in the comments):

result = (10**len(value)-1) - int(value)

# If the input is 251, the output would be 748, as:
#     len('251') = 3
#     10**3 - 1 = 999
#     999 - 251 = 748
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • How about subtracting from `999...9`? You can find the length of the input string and make that many `9`s :))) – RafazZ Nov 13 '18 at 17:49
  • @RafazZ Using such an integer `999...9` sounds like a more elegant solution in theory, but in practice would require quite a bit more code to generate such a number and would amount to the same thing as my second solution anyway - subtracting 9 from each digit in the input number. – Abion47 Nov 13 '18 at 17:56
  • 1
    Not really: `10**len(input)-1` is the only one-liner needed to create an integer with enough `9`s. Basically everything boils down to `result = 10**len(input)-1-int(input)` – RafazZ Nov 13 '18 at 18:00
  • 1
    Length is known for input string, do generating is simple `10**length-1` – MBo Nov 13 '18 at 18:00
  • 1
    @RafazZ Fair enough, I don't use python much and completely forgot about the exponent operator. I'll add it as a solution. – Abion47 Nov 13 '18 at 18:04