0

How can I write this code in one line? The concept is that you should:

  • get one input from the user and check if the (ASCII form of the character) - 97 is divisible by 2 or not
    • if yes, you should print the original form of the character
    • else, you should print the upper case form of the character.
  • last, you should reverse the answer.
  • Example, if the input is alexander, the output should be e e a a X R N L D
  • But it should be in one line and only one, I came up with a solution but it was 3 lines, I don't know what to do next.

This is the code I came up with so far:

h = []
for i in input() : h.append(i.lower() if (ord(i) - 97) % 2 == 0 else i.upper())
print(*sorted(h, reverse=True))

The original code for the question, which you should convert to one line is:

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)
smci
  • 32,567
  • 20
  • 113
  • 146
  • Does this answer your question? [How to print without a newline or space](https://stackoverflow.com/questions/493386/how-to-print-without-a-newline-or-space) – S4eed3sm Jan 21 '22 at 07:23

1 Answers1

2

List comprehension (I did not check your code, only rewrite it):

h = []
for i in input() : h.append(i.lower() if (ord(i) - 97) % 2 == 0 else i.upper())
print(*sorted(h, reverse=True))

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

To quote your question:

you should print the original form of the character

That is not what the code does at the moment, just saying so you are aware.

after reading your deleted comment:

And if you are wondering about the if and else in list comprehension:

You can put it in your list, but if that was your problem (the actual question apparently) then I would suggest to use google, there are plenty of examples where this is used.: if/else in a list comprehension, https://towardsdatascience.com/a-gentle-introduction-to-flow-control-loops-and-list-comprehensions-for-beginners-3dbaabd7cd8a, https://pythonguides.com/python-list-comprehension-using-if-else/

3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18
  • 2
    I'm not disagreeing with the answer. But what sort of stupid homework assignment is this? Why should we be teaching students to write unreadable code? I – Frank Yellin Jan 21 '22 at 07:37
  • @FrankYellin I agree with you, but I hope that the questioner learns something from this code, and most important how to write comprehensive lists. That is a technique often used also by others thus it is good to understand how it works so one can read code from others. – 3dSpatialUser Jan 21 '22 at 07:50
  • 1
    @3DspatialUser. I realize it's the "right answer", but I would hope that in actual code the student should learn that the correct answer is `letters = [.....];` and then `print(*sorted(letters))`. – Frank Yellin Jan 21 '22 at 07:59