-7

I have Python code in multiple lines. I want to summarize in one line without ';' and 'exec function'

input_string = str(input())
array = []
for i in range(len(input_string)):
    if (ord(input_string[i]) - 97) % 2 == 0:
        array.append(input_string[i])
    else:
        array.append(input_string[i].upper())
array.sort(reverse=True)
answer = ' '.join(array)
print(answer)

2 Answers2

0

Well, here is the two lines solution:

array = [i if (ord(i) - 97) % 2 == 0 else i.upper() 
            for i in str(input())]
print(" ".join(sorted(array, reverse=True)))

Or as you said:

print(" ".join(sorted([i if (ord(i) - 97) % 2 == 0 else i.upper() for i in str(input())], reverse=True)))
OMY
  • 402
  • 1
  • 8
  • 18
0

You don't have to subtract 97 before applying mod 2 in the condition since it will merely invert the result and applying upper on characters below 97 will not change them.

so this should do the trick:

' '.join(sorted(c if ord(c)%2 else c.upper() for c in input_string))[::-1]
Alain T.
  • 40,517
  • 4
  • 31
  • 51