3 ms·
Writing tac in C++ is trivial: std::deque<std::string> lines; for (std::string line; std::getline(std::cin, line); ) lines.push_front(line); for (const auto &li
by saurik 13y ago
Writing tac in C++ is trivial: std::deque<std::string> lines; for (std::string line; std::getline(std::cin, line); ) lines.push_front(line); for (const auto &line : lines) std::cout << line << std::endl; // Note that I typed this quickly off the top of my head: I'm willing to believe there is a trivial typo the compiler would catch, but the overall implementation should be fine ;P.
As for ohce, you have defined a very very hard problem, one that it does not seem you realize is quite as hard as it actually is: if you have a sequence of Unicode codepoints and reverse their order you do not end up with a string of reversed characters, not in the general case, and not even for some reasonable encodings of seemingly-simple cases like an accented letter e.
Like, I challenge you to provide a working version of ohce in Python (2 or 3: your choice). Virtually no language actually provides a string type that makes this problem reasonable. It simply isn't fair to pick on C++ in this regard when no language "gets this right": at least C++ is being honest about the lack of guarantees it is making about string manipulation.
For more information, I recommend reading this article:
http://mortoray.com/2013/11/27/the-string-type-is-broken/ http://mortoray.com/2013/11/27/the-string-type-is-broken/
- e12e 13y ago> Like, I challenge you to provide a working version of ohce in Python (2 or 3: your choice). Not a full implementation, but wouldn't this approach actually work (note, doesn't work for python2): $ python3 Python 3.2.3 (default, Feb 20 2013, 14:44:27) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> "abc"[::-1] 'cba' >>> "øæåにほ言"[::-1] '言ほにåæø' [edit: with accents]: >>> "eẽêëèøæåにほ言"[::-1] '言ほにåæøèëêẽe' [edit2: formatting, indentation] There might very well be problems with this, but I'm not aware of any? [edit4: This is indeed broken for the ligature (baffle) case in python3. I'm not entirely sure that is an entirely fair test (but it is very interesting). I would argue that the ligature should probably be a replacement done for display/print, not in a text file. Just like the reverse of "æ" isn't a reverse composition of "e" and "a" (even if "æ" might be seen as a compositon of "a" and "e". I'm not sure how it deals with changing direction (left-to-right, right-to-left) -- comments welcome.] [edit3, sorry for the many edits] To be clear, I do not wish to "pick on c++", nor do I think the example is trivial. I do think it probably should be trivial -- it is something that should be supported in a canonical way by a standard library/implementation. Working with graphemes is a very fundamental part of working with text -- the fact that half(?) of developers have been able to hide behind ascii isn't a good excuse for not fixing it. How would one implement an editor if you can't access graphemes in a reasonable way? And more importantly, how would you test for palindromes? ;-)