You are viewing a single comment's thread from:

RE: Coding challenge #1 - Winners!

in #contest7 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.

Sort:  

Thanks @hendrikcrause, very nice and detailed explanation!

Thanks for explanation

Coin Marketplace

STEEM 0.18
TRX 0.17
JST 0.032
BTC 63686.15
ETH 2727.43
USDT 1.00
SBD 2.59