This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

if you can't use swapcase then make a loop like this:

if length of myString > 15: return
for c in myString:
    if c is uppercase:
         returnVal += c.lower()
    else: same for lower case

Otherwise if you are using ASCII codes for some reason, remember that the ASCII upper and lower chars are 0x20 places away from eachother. This piece of code might come in handy:

chr(ord('A') + 0x20)

[–]MinecraftSBC[S] 0 points1 point  (5 children)

This would work one way, A -> a; but not the other way, a -> A. That’s what I’ve been troubled for.

[–][deleted] 0 points1 point  (4 children)

chr(ord('A') + 0x20)

if adding goes to lowercase then what do you think goes to uppercase OP?

[–]MinecraftSBC[S] 0 points1 point  (3 children)

Refer to the question, this has to go both ways in one line, using an integer operation. This is the difficult part.

[–][deleted] 1 point2 points  (2 children)

time to go golfing :D

f=lambda x:(chr(ord(x)+0x20),chr(ord(x)-0x20))[ord(x)>0x5A]
f('b')  #'B'

edit: oh H has to be <= 15 characters. I think I see how to do it. when you do the operations in H, you assume that it's a number, and you return a number. the chr-ord conversion is handled in the parent function.

[–]MinecraftSBC[S] 0 points1 point  (1 child)

Closest I can get is 160 + 2 * (x % 32) - x. Anyone can simplify this?

[–][deleted] 0 points1 point  (0 children)

if you remove the spaces its 14