Sunday, 17 January 2016

Kata Day 52: Your order, please


Problem Description: Your task is to sort a given string. Each word in the String will contain a single number. This number is the position the word should have in the result.

Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).

If the input String is empty, return an empty String. The words in the input String will only contain valid 
consecutive numbers.

For an input: "is2 Thi1s T4est 3a" the function should return "Thi1s is2 3a T4est"

Level:6kyu

Link To Kata: Your order, please

This Kata asks us to sort a string in order of the number inserted inside each word of a string. The first step which is involved in solving this Kata is to split the given string with a delimiter of spaces and put all the words in an array. Then we initialize an empty answer array and loop through the array which was formed after splitting the input string. We find the number inserted in each word through a regex operation and store the word at current index at the same index of answer array as that of the number we get from the regex operation.

In the end we merge the answer array with space delimiter and output the string as the answer to this Kata. 

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 52

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

Happy coding. :)

No comments:

Post a Comment