Tuesday, 8 December 2015

Kata Day 13: Unique in Order


Problem Description: Problem Description: Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD')         == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1,2,2,3,3])       == [1,2,3]

Level: 6kyu

Link to Kata: Unique in Order

It was a simple Kata. What we are given is a string which contain a number of characters which can be repeated. We have to find the unique characters in the given order. So I created an answer array and looped through every character of the sequence. I checked if the character at the current index is equal to the character at the very next index or not. If it was not equal then I pushed the character at current index to the answer array.

When we have iterated through the entire array, we output the answer array.

Here is a link to the code: Kata Day 13

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

Happy coding. :)

No comments:

Post a Comment