@raymii pop_back on an empty string is undefined[1] so your solution would probably behave correctly most of the time on empty string as input but might just crash too
Thanks for noticing! In the last paragraph the complete code does a check for input.empty() and returns “” if true, so the pop back shouldn’t happen, or I might miss something.
Cool! I love how Perl looks like a shell script bus is a real language as well. I also did this kata in Python and faster than in C++. Not being as familiar with the standard library and strong typing cost most of the time.
k solution:
Explanation:
This is missing the part where ties by weight are broken by ordinary string order.
This is what the initial string sorting is for.
<
seems to preserve order in the case of ties.Haskell solution (requires the packages
sort
andextra
):Here’s a Rust transliteration of the Haskell, made entirely for curiosity (this isn’t a great example of idiomatic Rust): Rust Playground.
Python solution:
And in D, for good measure. I like how easy it is to literally translate Python into D:
Btw, “kata” is a Japanese loanword. For the plural, just add s, “katas”.
@raymii pop_back on an empty string is undefined[1] so your solution would probably behave correctly most of the time on empty string as input but might just crash too
[1] https://en.cppreference.com/w/cpp/string/basic_string/pop_back
Thanks for noticing! In the last paragraph the complete code does a check for input.empty() and returns “” if true, so the pop back shouldn’t happen, or I might miss something.
Fun little exercise. Here’s a Perl solution that works for the test input: https://pastebin.com/yiesqUbg
Cool! I love how Perl looks like a shell script bus is a real language as well. I also did this kata in Python and faster than in C++. Not being as familiar with the standard library and strong typing cost most of the time.