Saturday, 2 January 2016

Kata Day 37: Split In Parts


Problem Description: The aim of this kata is to split a given string into different strings of equal size (note size
 of strings is passed to the method)

Example:

Split the below string into other strings of size #3

'supercalifragilisticexpialidocious'

Will return a new string
'sup erc ali fra gil ist ice xpi ali doc iou s'

Assumptions:

String length is always greater than 0
String has no spaces
Size is always positive

Level:7kyu

Link To Kata: Split In Parts

In this Kata we have to form sub strings of given length from the input string and output them separated by spaces. The first step which I took while solving this Kata was to initialize an empty string "ans" and then loop through the input string and add the sub string of required length to the ans string along with a space. We increment the index of loop by the same value as of the sub string length. 

If we reach a point where we don't have the sub string of length greater than the required length then we output whatever is left of the input string without any spaces.

In the end we output the "ans" as the solution to this Kata.

Here is a link to the code: Kata Day 37

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

Happy coding. :)

No comments:

Post a Comment