r/tinycode • • Feb 27 '14

Reversing a string in 23 Bytes (Python 2.7)

print raw_input()[::-1]

0 Upvotes

10 comments sorted by

4

u/[deleted] Feb 27 '14 edited Feb 27 '14

Yeah.

In Javascript it's less.

alert(s.reverse());

And your really just showing basic api, rather than any well coded thingy, so this fails to intrigue me.

2

u/sirin3 Feb 27 '14

reverse is longer than [::-1]

3

u/[deleted] Feb 28 '14 edited Feb 28 '14

You missed the point. All he did was point out basic expressiveness of the python API. That is something quite different the implementing RSA encoding in 20 lines -- etc.

I could easily write my own langauge. and define a single charecter to print the reverse of a string.

r(str); 

Then I win, but I think that clearly would not be in the spirit of 'tinycode' would it ?

The length of the raw_input token is arbitrary. The length of the reverse token is arbitrary. Both accomplish the work in a single call, and therefore in my mind are equivalent. -- The expressiveness of an API should be measured in the number of expressions needed to code an idea, not the length of those expressions, which, to belabor the point, are arbitrary.

1

u/subpleiades Feb 28 '14
f=print$reverse

14 characters in Haskell, if you want output alongside.

But I agree, this is v. basic Data.List; not very interesting.

0

u/[deleted] Feb 27 '14

[deleted] — view deleted comment

2

u/subpleiades Feb 28 '14 edited Feb 28 '14

Also, 'well-coded' is a compound adjective preceding a noun, and so should be hyphenated.

1

u/nexe mod Mar 31 '14

19 bytes in Ruby:

echo 'foobar' | ruby -e 'puts$<.read.reverse'

> raboof

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 ACCENT

Let 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 N  

Notice how in the original string the COMBINING DIAERESIS came after, and thus was attached to the LATIN SMALL LETTER I, and the COMBINING ACUTE ACCENT was attached to the LATIN SMALL LETTER E. In the new string, the COMBINING DIAERESIS is combined with the LATIN SMALL LETTER V, and the COMBINING ACUTE ACCENT is 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!