Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Size doesn't really matters...right?

$
0
0
I was completing a challenge on codefights, the challenge was: Below my solution and the top rated solution: MySolution: string buildPalindrome(string str) { auto BeginIt = str.begin(); auto EndIt = str.end() - 1; bool IsAlreadyPalindrome = true; for (; BeginIt < EndIt; BeginIt++, EndIt--) { if (*BeginIt != *EndIt) { EndIt = str.end(); IsAlreadyPalindrome = false; } } //case midpoint not found if (BeginIt != EndIt) { //case is a palindrome if (IsAlreadyPalindrome) { return str; } //case not a palindrome, no midpoint if (str[str.size() - 1] == str[str.size() - 2]) { string HalfStr(str.substr(0, BeginIt - str.begin())); return string(HalfStr + string(HalfStr.rbegin(), HalfStr.rend())); } } //case not a palindrome, with midpoint auto MidIt = BeginIt != EndIt? str.end() - 1: BeginIt; string HalfStr(str.substr(0, MidIt - str.begin())); return string(HalfStr + *MidIt + string(HalfStr.rbegin(), HalfStr.rend())); } TopRatedSolution: std::string buildPalindrome(std::string st) { string ret(st); auto it = st.begin(); while (ret != string(ret.rbegin(), ret.rend())) { ret.insert(st.size(), 1, *it++); } return ret; } Now, when I completed the challenge and checked others solution I was a bit discouraged to see that it could be completed in so few lines and took me so many, but then out of curiosity I timed it online against mine with an impossibly long string, while the top rated took 13sec to solve it mine took 0,33sec... Now I'm pretty sure mine won't ever get a single vote because it looks ugly to see (too many lines). Now I'm not saying mine is better (now after solving I see 2 things that could be easily improved also), I realize that readability is important and such a function won't ever be used with strings that long, so my performance gain was pretty much pointless in all real contexts (and also accidental by the way, I was just trying to solve the challenge in the most straightforward way I could see), though I am kind of disappointed by the fact that a site about challenging others and improving, made by hundreds of programmers is "celebrating" something as empty/meaningless as "number of lines used" over readability or performance... I mean, couldn't that site have users voting on readability and a scoring given by the site based on performances?! I'm not a fan of that mentality of "watch how hacky I am, can solve it in 0 lines" if then is not readable and the performance are atrocious (not saying is the case with all the short solutions btw), I should probably just start avoiding that place because with that goals/highlights in mind, seems like a good way to train bad programmers in mass to me... Anyway, just my rant because I'm still disappointed it took me so many lines What's your opinion on this? Shouldn't sites like that hold to higher standards?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>