r/tinycode • u/IWannaFuckLarryPage • Feb 27 '14
Reversing a string in 23 Bytes (Python 2.7)
print raw_input()[::-1]
1
0
u/Drainedsoul Feb 27 '14
I hope people don't think they can actually reverse strings in this way.
3
u/isobit Feb 28 '14
Why? I'm really curious why you think that, not being an ass.
6
u/Drainedsoul Feb 28 '14 edited Feb 28 '14
Let us assume you're using a UTF-32 build of Python (32-bit wide characters, 1 character per code point).
Consider the following string:
naïveté
I don't know if Reddit preserves normalization forms, but the above string (as entered) is actually 9 code points in length, as it is in Normal Form Canonical Decomposition. The 9 code points are (in order):
U+006E ==> LATIN SMALL LETTER N U+0061 ==> LATIN SMALL LETTER A U+0069 ==> LATIN SMALL LETTER I U+0308 ==> COMBINING DIAERESIS U+0076 ==> LATIN SMALL LETTER V U+0065 ==> LATIN SMALL LETTER E U+0074 ==> LATIN SMALL LETTER T U+0065 ==> LATIN SMALL LETTER E U+0301 ==> COMBINING ACUTE ACCENTLet us assume you reverse this string in the manner the OP proposes. This will result in:
U+0301 ==> COMBINING ACUTE ACCENT U+0065 ==> LATIN SMALL LETTER E U+0074 ==> LATIN SMALL LETTER T U+0065 ==> LATIN SMALL LETTER E U+0076 ==> LATIN SMALL LETTER V U+0308 ==> COMBINING DIAERESIS U+0069 ==> LATIN SMALL LETTER I U+0061 ==> LATIN SMALL LETTER A U+006E ==> LATIN SMALL LETTER NNotice how in the original string the
COMBINING DIAERESIScame after, and thus was attached to theLATIN SMALL LETTER I, and theCOMBINING ACUTE ACCENTwas attached to theLATIN SMALL LETTER E. In the new string, theCOMBINING DIAERESISis combined with theLATIN SMALL LETTER V, and theCOMBINING ACUTE ACCENTis actually in the first position of the string, which means it combines with nothing.Is this the result you expect when you "reverse" a string? Is this what you want to happen? Probably not. In 99.9% of cases, working with strings is a lot more than just blindly performing manipulations on what is basically a glorified array.
In this case what you probably wanted to do was find each grapheme cluster break, and reverse the order of grapheme clusters.
Note that the scenario I gave -- assuming you're working with UTF-32 strings -- is actually the best case scenario. In this case you form a string that's meaningless garbage (what does it mean for a string to begin with a combining mark? If you concatenate it with another string, the string preceding it will be altered, as the combining mark will combine with the last starter in that string, was this really what you wanted?), but what if you were working with UTF-16 strings? Then you could possibly reverse lead/trail surrogate pairs when you reverse the string, leading to a corrupted string that's actually illegal Unicode. UTF-8 has the same problem, magnified (a lot of people sweep UTF-16 lead/trail surrogate pairs under the rug, because it only happens for characters outside the BMP and they say "oh that'll never happen" (which is one of the most evil things a programmer can say), a lot of characters have multi-code unit representations in UTF-8, however), since you can reverse the order of lead byte/continuation byte sequences.
2
u/isobit Feb 28 '14 edited Feb 28 '14
Well, that was my theory.
Edit: Wait... I actually think I understand you. My studies... They are paying off! I'm a complete noob if that wasn't obvious, I just hang out here because you guys are fucking cool. Thanks for teaching me something!
4
u/[deleted] Feb 27 '14 edited Feb 27 '14
Yeah.
In Javascript it's less.
And your really just showing basic api, rather than any well coded thingy, so this fails to intrigue me.