Tuesday, 22 December 2015

Kata Day 27: Reversing Fun


Problem Description: You are going to be given a string. Your job is to return that string in a certain order that I
will explain below:

Let's say you start with this: 012345

The first thing you do is reverse it:543210
Then you will take the string from the 1st position and reverse it again:501234
Then you will take the string from the 2nd position and reverse it again:504321
Then you will take the string from the 3rd position and reverse it again:504123

Continue this pattern until you have done every single position, and then you will return the string you have created. For this particular number, you would return:504132

Input:

A string of length 1 - 1000

Output:

A correctly reordered string.

Level:7kyu

Link to Kata: Reversing Fun

This Kata asks us to reverse a given string keeping every index of the string as a base. So I first reversed the string using the first character of the string. Then I looped through the string from index 1 to the end of the string and divided the string in two parts. The first part being a sub string of original string from index 0 to just before the index of loop. The next part was a sub string of original string from index of the loop to the end of the string. I reversed the second sub string and merged it with the first string to redefine the input string. I repeated the process for every index of the string.

Once the looping is done I return the redefined input string as the output for the function. 

Here is a link to the code: Kata Day 27

Please mention your suggestions or your doubts in the comment below.


Happy coding. :)

No comments:

Post a Comment