Monday, 30 November 2015

Kata Day 6: Insert dashes


Problem Description: Write a function insertDash(num) that will insert dashes ('-') between each two odd numbers in num. 

For example: if num is 454793 the output should be 4547-9-3. 

Don't count zero as an odd number.

Level: 7kyu

Link to Kata: Insert Dashes

In this Kata we have to insert a dash between two odd numbers. I initially converted the input argument num to a string and then used a while loop to go through every element of the string one by one. I initialized a variable "ans" as an empty string. 

While looping through each element of the string we check that whether that element when converted to an integer leaves a remainder of 1 when divided by 2 (Condition for odd number) and also if the element which is present next to it when converted to an integer leaves a remainder of 1 when divided by 2. If these two condition satisfies then we would add the element which was present on the counter index followed by a "-" to the "ans" variable otherwise we would just add the element to the variable.

We will also have to keep a check on the counter variable so that we do not check for the element present next to counter when we reach the end of the string. 

Once the above procedure is done through the loop we would be able to get the desired result.

Here is a link to the code:  Kata Day 6

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

Happy coding. :)  

Sunday, 29 November 2015

Kata Day 5: Find the smallest integer in the array


Problem Description: Find the smallest integer in the array.

Given an array of integers your solution should find the smallest integer. For example:
Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345

You can assume, for the purpose of this Kata, that the supplied array will not be empty.

Level: 7kyu


This Kata was easy to solve if we know how to use sort function in Javascript. Sorting an array using a function is slightly different from the way we used sort function in Python where we can just directly get a sorted array by using a sort function against the given array.

In Javascript we have to use the sort function with a bit of additional code in order to sort an array of given integers. 

Sorting an array in Javascript is done in following manner:

arr.sort(function(a, b){return a-b});

This sorts the array in ascending order. 
To read further on this you can follow this MDN link: Sort in Javascript

Once we have got a sorted array we can return the first element of the array which will give us the smallest integer of that array.

Here is a link to the code: Kata Day 5

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

Happy coding. :)  

Kata Day 4: Collatz Conjecture Length


Problem Description: The Collatz Conjecture states that for any natural number n, if n is even, divide it by 2.

If n is odd, multiply it by 3 and add 1. If you repeat the process continuously for n, n will eventually reach 1.

For example, if n = 20, the resulting sequence will be:
[20, 10, 5, 16, 8, 4, 2, 1]

Write a program that will output the length of the Collatz Conjecture for any given n. In the example above, the output would be 8.

Level: 7kyu

Link to Kata: Collatz Conjecture Length

This was a pretty simple Kata. I used a while condition where I checked every time for the value of n and continue going through the loop until the value of is not equal to 1. I also initialized an empty array col_a to store different variations n goes through before it becomes 1. In the loop if n is an even number I changed the value of n to n/2 according to the conjecture and if n was odd I multiplied n by 3 and added 1 to the product. In both the cases I pushed the new value of n to the col_a array.

Once n becomes 1 and exits out of the while loop, I calculate the value of length of col_a array and add 1 to it. (Adding 1 because we consider the initial value of n and it will also be part of the array.)

We return the final value which will give us the length of the Collatz Conjecture for any given n.

If you want to do some further reading on Collatz Conjecture then here is the link: Collatz Conjecture

Here is a link to the code: Kata Day 4

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

Happy coding. :)  

Friday, 27 November 2015

Kata Day 3: Exes and Ohs


Problem Description: Check to see if a string has the same amount of 'x's and 'o's. The method must return a Boolean and be case insensitive. The string can contain any char.

Examples input/output:
XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
XO("zzoo") => false

Level: 7kyu

Link to Kata: Exes and Ohs

The very first thing which came to my mind while attempting this Kata was to convert the whole input string to lower case. I did that by using toLowerCase() function of Javascript.

From there on it was easy to solve the Kata. I initialized two variables as a counter for 'o' and 'x' to zero. I used a while loop to go through the string and check the number of 'o' and 'x' and increment the counter accordingly.

At last I compared both the counters and if they were having the same value then the function returned true otherwise it returned false. There can be a case where there can be no 'o' or 'x' present in the string. The initialization of counter to a value of zero would help in such a case as it will return true.

Please keep in mind that this Kata can also be done with help of Regex and that would be a much efficient way to solve the problem. I am still learning Regex and I would be implementing them in future.

Here is a link to the code: Day 3 Kata

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

Happy coding. :)  

Thursday, 26 November 2015

Kata Day 2: Printer Error


Problem Description:  In a factory a printer prints labels for boxes. For one kind of boxes the printer has to use colors which, for the sake of simplicity, are named with letters from a to m.

The colors used by the printer are recorded in a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used three times color a, four times color b, then one time color a...

Sometimes there are problems: lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm.

You have to write a function printer_error which given a string will output the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.

The string has a length greater or equal to one and contains only letters from a to z.

Example:
s="aaabbbbhaijjjm"
error_printer(s) => "0/14"

s="aaaxbbbbyyhwawiwjjjwwm"
error_printer(s) => "8/22"

Level: 7kyu

Link to Kata: Printer Error

To solve this Kata, I used a base array with characters from 'a-m' which I formed by using a split method.  Then I looped through the given color code string and compared the index of each character with the base array. I used a variable count which I incremented every time I  found the result of index comparison using array.indexOf() method equal to "-1".

To find the error fraction I kept the count in the numerator and length of input color code string in the denominator.

Keep in mind that this Kata can also be done with help of Regex and that would be a much efficient way to solve the problem. I am still learning Regex and I would be implementing them in future.

Here is a link to the code: Day 2 Kata

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

Happy coding. :)   

Wednesday, 25 November 2015

Kata Day 1: Find The Parity Outlier


Problem Description: You are given an array (which will have a length of at least 3, but could be very large) containing integers. The integers in the array are either entirely odd or entirely even except for a single integer N. Write a method that takes the array as an argument and returns N.

For example:

[2, 4, 0, 100, 4, 11, 2602, 36]

Should return: 11

[160, 3, 1719, 19, 11, 13, -21]

Should return: 160

Level: 6kyu

Link to the Kata: The Parity Outlier

I have followed an approach where I have to break the problem into two sub-problems.

Problem 1: To find whether its a all even but one an all odd but one array.

To solve this problem I checked the first 3 elements of an array. As it is given that the array has a minimum length of 3, it is safe to check first 3 elements for if they are odd or even. I store the number of odd elements in a odd count variable(m) and number of even elements in an even count variable (n). Comparing these 2, I can solve the problem 1 and find the the type of the array.

Problem 2: To find the parity outlier. There are two paths to check this and we take any 1 of them depending upon the type of our array. We loop through the array using a while loop and check every element by finding its modulus(%) value by dividing it by 2 and find the outlier based on its modulus value.

In case of an all even array the outlier will be the one with modulus value of 1 and in case of an all odd array the outlier will be the one with modulus value of 0.


We use the Math.abs() function of Javascript in order to keep a common ground and treat  all the elements of the array as positive numbers at the time of testing.

Here is a link to the code: Day 1 Kata

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

Happy coding. :)

The 180 day Kata Challenge


What else does a chess player do apart from studying chess books and previous chess games of great grandmasters? He plays chess everyday. That is where they try to implement what they have learnt and discover their mistakes and learn from them.

Same goes for a programmer who wants to improve his programming skills. The great programmers code everyday. Even if its just a small script but they make sure that hit the keyboard in front of an IDE once a day. Inspired by John Resig's blog where he advises to write code everyday and takes a pledge to write code for next 180 days and to push that code to version control I have taken up a challenge to solve a Kata everyday starting from today.

Here is a glimpse of his Github repository- YES!! Its a streak of 731 days.





As a part of my learning Javascript journey I have taken a challenge to solve 1 Kata everyday from Codewars. I will be keeping the solutions to Kata I solve everyday on my Github account and will be posting the approach I took to solve the problem in a blog post everyday.




Hakan Forss's blog define it as following:


"Kata means pattern, routine, habits or way of doing things. Kata is about creating a fast “muscle memory” of how to take action instantaneously in a situation without having to go through a slower logical procedure. A Kata is something that you practice over and over striving for perfection. "

So if you want to learn a new programming language or improve your problem solving skills you can tag along with me on this journey. Feel free to give a better approach to solve the problem in comments or fork the repository and clone it to start solving the problems yourself.

Here is a link to the github repository: Github

Happy Coding !!