RE: Coding challenge #1 - Winners!

You are viewing a single comment's thread from:

Coding challenge #1 - Winners!

in contest •  7 years ago 

Thanks @armandocat,

For anyone interested in knowing how it works, here's a - hopefully - quick description.

So the two public methods were what needed to be implemented, for my implementation I needed two private helper methods: swap and reversePartial.

swap should hopefully be easy to understand, it simply takes an array and two indexes and then "swaps" the array's characters at the index positions.

reversePartial is a bit more complicated, but simple enough to grasp I believe. It takes in an array and two index as well and then reverses the order of all the characters in the array between the two indexes. The tricky / clever part about it is that you can reverse an array more efficiently by swapping the first and last character and then the second and second to last, etc. I made this handy gif below to demonstrate.
Webp.net-gifmaker.gif

So now the stars of the show...

reverseAll becomes trivial to solve, since it is simply a reversePartial with the first and last index of the array.

reverseWords is a bit more involved. Here we have to loop through the array until we find a space character. Then do a reversePartial between the index before the space and the index after the last space found. I keep track of the last space found with integer i and the current place I'm checking with the integer j.

Thanks @armandocat for hosting the competition, was a fun little exercise.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks @hendrikcrause, very nice and detailed explanation!

Thanks for explanation