Wednesday, 27 January 2016

Kata Day 59: Rank Vector


Problem Description: Given an array (or list) of scores, return the array of ranks for each value in the array. The largest value has rank 1, the second largest value has rank 2, and so on. Ties should be handled by assigning the same rank to all tied values. For example:

ranks([9,3,6,10]) = [2,4,3,1]
and

ranks([3,3,3,3,3,5,1]) = [2,2,2,2,2,1,7]
because there is one 1st place value, a five-way tie for 2nd place, and one in 7th place.

Level: 6kyu

Link To Kata: Rank Vector

In this Kata for every element in the given array we will be returning its rank in the vector. We start solving this Kata by creating a copy of the given array using the Javascript slice function. Then we sort the array in decreasing order. We create an empty answer array and then loop through every element of the given array. For every element in the array we check the index of the element in the sorted copy of the array which we created. We push the sum of the index and 1 to the answer array. We repeat this for every element in the given array. Then we return the answer array as the solution to this Kata.

Here is the link to code: Kata Day 59

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

Happy coding. :)

Sunday, 24 January 2016

Kata Day 58: How many are smaller than me


Problem Description: Write a function smaller(arr) that given an array arr, you have to return the amount of numbers that are smaller than arr[i] to the right.

For example:

smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0]
smaller([1, 2, 0]) === [1, 1, 0]

Level: 7kyu

Link To Kata: How many are smaller than me 

In this Kata we have to return the amount of numbers that are smaller than the given number at the index of given array.

We start solving this Kata by we loop through the given array and initialize another index variable j to 0 for every looping index. We also initialize a count variable to 0.Then we check how many elements to the right of the element at current index are less than the element at current index. For every match we increase the count by 1. In the end we push the count to a new answer array. We repeat this process for every element in the given array and return the answer array as the solution to this Kata in the end.

Here is the link to code: Kata Day 58

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

Happy coding. :)

Friday, 22 January 2016

Kata Day 57: Product of consecutive Fib numbers


Problem Description: The Fibonacci numbers are the numbers in the following integer sequence (Fn):

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
such as

F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying

F(n) * F(n+1) = prod.
Your function productFib takes an integer (prod) and returns an array:

[F(n), F(n+1), true]
if F(n) * F(n+1) = prod or returns

[F(m), F(m+1), false]
if you don't find two consecutive F(m) verifying F(m) * F(m+1) = prod. F(m) will be the smallest one such as 
F(m) * F(m+1) > prod.

Level:5kyu

Link To Kata: Product of consecutive Fib numbers

Here we have to find if the given number is a product of two consecutive numbers. We start by looping through a fibonacci series and check for product of every two terms of the series to be same as the given number. We also keep a check if the product comes out to be greater than the given number. If so then we break out of the loop and push both the numbers and a false boolean value to an empty array.

If we find two such consecutive terms whose product is same as the number then we push those two numbers and a true boolean value to the an empty array.

In the end we return the array as the solution to our Kata.
Here is the link to code: Kata Day 57

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

Happy coding. :)

Wednesday, 20 January 2016

Kata Day 56: Least Common Multiple


Problem Description: Write a function that calculates the least common multiple of its arguments; each argument is assumed to be a non-negative integer.

Level:5kyu

Link To Kata: Least Common Multiple 

We can find LCM of two numbers by a method which we learnt at high school but finding LCMs of n number of arguments can become a tiresome process through that method. Here our Javascript function takes n number of arguments and we have to find their LCM. We start by pushing our input arguments to an input array. Then we declare a variable L and initialize it to the value of LCM of first 2 numbers of input array. We then loop through the complete array and continue finding LCM of consecutive numbers.

In the end we return the value of L as the answer to this Kata. Keep in mind that this Kata can also be solved through recursion.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 56

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

Happy coding. :)

Tuesday, 19 January 2016

Kata Day 55: Efficient Power Modulo n


Problem Description: Your task is to create a new implementation of modpow so that it computes (x^y)%n for large y. 
The problem with the current implementation is that the output of Math.pow is so large on our inputs that it won't fit in a 64-bit float.

You're also going to need to be efficient, because we'll be testing some pretty big numbers.

Level:5kyu

Link To Kata: Efficient Power Modulo n

This was a 1 level up Kata. From today onwards I will start working on level 5 Kata. Here we are asked to find an efficient algorithm to find a^b%n for very big numbers. We start solving this by initializing our result variable to 1. Then we move through a loop which continues until  our b variable is not equal to zero. We check if b modulus 2 is a non zero number then we multiply our result variable with a and get its modulus with n. We square the value of a and get its modulus with n. In the end we change the value of b to its half.

In the end we return the value of our result variable as the answer to this Kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 55

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

Happy coding. :)

Monday, 18 January 2016

Kata Day 54: Multiples of 3 and 5


Problem Description: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. 
The sum of these multiples is 23.

Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

Courtesy of ProjectEuler.net

Level:6kyu

Link To Kata: Multiples of 3 and 5

It is the first project euler problem which I encountered at codewars. In this Kata we have to find the sum of all the multiples of 3 or 5 below a given number. We loop from 1 to a number just 1 less than the given number and keep adding the numbers which are factor of either 3 or 5 to a sum variable which we had initialized to 0.

In the end we return the value of sum as the answer to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 54

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

Happy coding. :)

Sunday, 17 January 2016

Kata Day 53: N-th Fibonacci


Problem Description: I love Fibonacci numbers in general, but I must admit I love some more than others.

I would like for you to write me a function that when given a number (n) returns the n-th number in the Fibonacci 
Sequence.

For example:

   nthFibo(4) == 2
Because 2 is the 4th number in the Fibonacci Sequence.

For reference, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

Level:6kyu

Link To Kata: N-th Fibonacci 

Its a pretty easy Kata if you are familiar with Fibonacci numbers. The numbers where the nth term is the sum of previous two terms. We have to find the nth Fibonacci term given n as an input. We start solving this kata by checking if the given n is 1 or 2 in which case we can return 0 or 1 as they are first and second term of the series. If n is greater than 2 then we initialize a count variable equal to 2 and loop until count becomes equal to n. Inside the loop we change the first and second term of the series with second and third term and so on. Each time we increment the count variable by 1.

In the end when we get out of the loop we return the second term of the series as our result to this Kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 53

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

Happy coding. :)