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. :)

Sunday, 6 December 2015

Kata Day 12: Dubstep


Problem Description: Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters

Output

Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.

Examples

songDecoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
  // =>  WE ARE THE CHAMPIONS MY FRIEND

Level: 6kyu

Link to Kata: Dubstep

This is the first Kata where I am using Regex which is a powerful tool to work with strings and its implementation can be found in various languages including Javascript. Here I assigned the variable 'find' to a value of "WUB".  Then I assigned another variable 're' to locate te positions where "WUB" is present in the string. Then I replaced all the positions where "WUB" was present with a space(" ").

I implemented the following regex command to remove all the occurences of "WUB" with a space so that I do not end up giving more than 1 space to a continuous occurrence of "WUB".

str.replace(/\s+/g, ' ');

The '+' takes continuous occurrences into account and then replace them with a single space. We return the string by using a trim function on it so as to remove any space in the starting or ending of the string.

Here is a link to the code: Kata Day 12

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

Happy coding. :)

Saturday, 5 December 2015

Kata Day 11: Digital Root


Problem Description: In this kata, you must create a digital root function.

A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has two digits, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.

Here's how it works:
digital_root(16)
=> 1 + 6
=> 7

digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6

Level: 6kyu

Link to Kata: Digital Root

To find a digital sum/root of a number, we have to find the sum of its digit and if that sum is a single digit number then it is our required digital root otherwise we again find the sum of digits of the digits of the sum acquired and keep on doing this until we get a single digit number.

In this Kata we enter a loop which is always true and inside it we convert the given number to a string and then loop through the string. We initialize a variable sum to 0 and while looping through the array we keep on adding every single digit of given number by parsing the character element of string to an integer. Once we get the sum we check if the length of sum when converted to a string is 1. If it is then we push the value of sum to n and break from the while loop. If it is not then we push the value of sum to n and continue with the previous process until we get a sum with a single digit.

In the output we return the value of n as our value of digital root.

Here is a link to the code: Kata Day 11

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

Happy coding. :)

Friday, 4 December 2015

Kata Day 10: Format String


Problem Description: Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'

list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'

list([ {name: 'Bart'} ])
// returns 'Bart'

list([])
// returns ''

Level: 6kyu

Link to Kata: Format String

Our first aim while solving this Kata should be to extract all the names corresponding to hashes in a single array. We create an empty output array and push all the names into it while looping through the given array.

We create an empty string called "ans". If the length of output array is 1 then we put answer equal to first and only element of output array and return the answer. If the length is greater than 1 then we loop through all the elements of output array and add every element to ans variable followed by a comma and a star. We keep a check on looping index and if it points to the second last element of output array then we add the second last element, followed by an ampersand (&) and then the last element to the answer variable. We break out of the loop at this point.

We output the answer variable which gives the desired result.

Here is a link to the code: Kata Day 10

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

Happy coding. :)

Thursday, 3 December 2015

Kata Day 9: Sum of numbers


Problem Description: Given two integers, which can be positive and negative, find the sum of all the numbers between including them too and return it. If both numbers are equal return a or b.

Note! a and b are not ordered!

Level: 7kyu

Link to Kata: Sum of numbers

A pretty straightforward Kata. We have to find the sum of numbers between two numbers including the numbers themselves. 

We will be initializing a sum variable to zero in the starting. Then we compare the given two numbers. If both numbers are equal we output the same number without any further operation. Otherwise we start to move from the smaller number towards the bigger number and keep adding the number on every looping index to the sum variable. Once we have reached end of the loop we will be returning the sum as our output.

Here is a link to the code: Kata Day 9

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

Happy coding. :) 

Wednesday, 2 December 2015

Kata Day 8: Count Consonants


Problem Description: Write a function consonant-Count or consonant_count  that takes a string of English-language text and returns the number of consonants in the string.

Consonants are all letters used to write English excluding the vowels a, e, i, o, u.

Level: 7kyu

Link to Kata: Count Consonants

We create two strings initially- a vowel string which contains all the vowels and a cons string containing all 26 English alphabets. We also initialize a variable n to zero. 

We convert the given string to lowercase using Javascript's toLowerCase() function.

We then loop through the every element of given string and check if the element is a part of const string to avoid any special character or spaces. Then we check if the value of that element compared to vowel string is equal to -1. If it is then it proves that the element we are comparing is an English alphabet but not a vowel. In that case we increment the value of n by 1. We continue this process for every character of the input string and then after the loop we output the value of n as our consonant count.

Here is a link to the code: Kata Day 8

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

Happy coding. :)  

Tuesday, 1 December 2015

Kata Day 7: Two to one


Problem Description: Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, - each taken only once - coming from s1 or s2.

Examples:
a = "xyaabbbccccdefww"
b = "xxxxyyyyabklmopq"
longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz"
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"


Level: 7kyu

Link to Kata: Two to one

Given the two strings we first split them into two arrays using a split function and then join both these arrays into a single array using the concatenate function.

We then sort the larger concatenated array and initialize a new answer array. We loop through the larger array and if the index of an element from larger array is equal to -1 in the answer array, we push that particular element to answer array.

Once we have completely gone through the concatenated array we join the answer array into a single string using a join function and then return that string.

Here is a link to the code: Kata Day 7

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

Happy coding. :)