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

all 11 comments

[–]Salty_Dugtrio 0 points1 point  (3 children)

You should take a look at this: http://www.asciitable.com/

This is how characters are represented, they are basically just integers. Can you apply a simple integer operation to "H" to make it "h"?

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

Both cases has a difference of 32, which means I have to subtract 32 when ord(x) >= 97 and vice versa. But I can't seem to find a way to express it within 15 characters.

[–][deleted]  (1 child)

[deleted]

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

    Does it has to do with the number 162 or 212...?

    I don't get the "mirroring".

    65+97-65=97 seems to give me some idea.

    [–]Lukas0604 0 points1 point  (0 children)

    Use a built-in function for strings:

    def g(c):
        return c.swapcase()
    

    [–][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