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

Kata Day 52: Your order, please


Problem Description: Your task is to sort a given string. Each word in the String will contain a single number. This number is the position the word should have in the result.

Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).

If the input String is empty, return an empty String. The words in the input String will only contain valid 
consecutive numbers.

For an input: "is2 Thi1s T4est 3a" the function should return "Thi1s is2 3a T4est"

Level:6kyu

Link To Kata: Your order, please

This Kata asks us to sort a string in order of the number inserted inside each word of a string. The first step which is involved in solving this Kata is to split the given string with a delimiter of spaces and put all the words in an array. Then we initialize an empty answer array and loop through the array which was formed after splitting the input string. We find the number inserted in each word through a regex operation and store the word at current index at the same index of answer array as that of the number we get from the regex operation.

In the end we merge the answer array with space delimiter and output the string as the answer to this Kata. 

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 52

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

Happy coding. :)

Saturday, 16 January 2016

Kata Day 51: Find the divisors!


Problem Description: Create a function named divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime'
(use Either String a in Haskell).

Example:

divisors(12); //should return [2,3,4,6]
divisors(25); //should return [5]
divisors(13); //should return "13 is prime"
You can assume that you will only get positive integers as inputs.

Level:6kyu

Link To Kata: Find the divisors!

Its a pretty easy kata. We have to print out the number of divisors of a given number excluding 1 and the number itself. If there are no such divisors then we output that the number is prime. We initialize a divisor array. We start from 2 to half the value of the given number and push all such numbers which divide the given number completely to divisor array.

In the end if length of divisor array is equal to 0 then we output that the number is prime or else we return the divisor array as the solution to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 51

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

Happy coding. :)

Friday, 15 January 2016

Kata Day 50: Backwards Read Primes


Problem Description: Backwards Read Primes are primes that when read backwards in base 10 (from right to left) are a different prime. (This rules out primes which are palindromes.)

Examples:
13 17 31 37 71 73 are Backwards Read Primes
13 is such because it's prime and read from right to left writes 31 which is prime too. Same for the others.

Task

Find all Backwards Read Primes between two positive given numbers (both inclusive), the second one being greater than the first one. The resulting array or the resulting string will be ordered following the natural order of the prime numbers.

backwardsPrime(2, 100) => [13, 17, 31, 37, 71, 73, 79, 97] 
backwardsPrime(9900, 10000) => [9923, 9931, 9941, 9967]

Level:6kyu

Link To Kata: Backwards Read Primes

In this kata we aim to print out such prime numbers in a given range where if the prime number is read backwards i.e. reversed then also it remains a prime number. Also we have to keep in mind that this list should exclude numbers which are palindrome i.e. the numbers which remain same when read backwards and forward. So we loop from starting point to the ending point and keep a number of checks at each point. In the starting we check that the number is not a single digit number. Then we check if the number is not a palindrome. If both the above conditions are satisfied then we check that the given number as well as the reversed form of given number are prime. If they both are prime then  we push the number to an initialized answer array. 

In the end we return the answer array as the solution to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 50

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

Happy coding. :)

Thursday, 14 January 2016

Kata Day 49: Finding duplicate in an unsorted list in O(n) time


Problem Description: Spin-off of this kata, here you will have to figure out an efficient strategy to solve the problem of finding the sole duplicate number among an unsorted array/list of numbers starting from 1 up to n.

Hints: a solution in linear time can be found; using the most intuitive ones to search for duplicates that can run 
in O(n²) time won't work.

Level:6kyu

Link To Kata: Finding duplicate in an unsorted list in O(n) time

This kata demands complexity less than O(). So we cannot sort this function using Javascript prototype also we cannot use 2 for loops to check for a duplicate value. 
We start solving this kata by finding the maximum value in the given array. Then we define an array of length of maximum value in which every element is initialized to zero. 

We loop through the initial array and for every value in the array we map its value in our initialized array. If it is zero then we make it 1. But if its already 1 then we return the value at index of the input array as the duplicate value.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 49

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

Happy coding. :)

Wednesday, 13 January 2016

Kata Day 48: Multiplication Tables


Problem Description: Create a function that accepts dimensions, of Rows x Columns, as parameters in order to create a multiplication table sized according to the given dimensions. **The return value of the function must be an array,and the numbers must be Fixnums, NOT strings.

Example:

multiplication_table(3,3)

1 2 3
2 4 6
3 6 9

-->[[1,2,3],[2,4,6],[3,6,9]]

Each value on the table should be equal to the value of multiplying the number in its first row times the number in its first column.

Level:6kyu

Link To Kata: Multiplication Tables

In this kata we accept as an input the number of rows and columns as our input and we have to return a multiplication table according to the given dimensions.

We declare an empty array as ans. We loop from 1 till the end of the given input row.For every iteration we declare an empty sub-array. Then we loop from 1 to the given input column and multiply the row and column and push the product to sub-array. After complete iteration of columns we push the sub-array to ans array and continue the iteration with next row.

In the end we return the ans array as the required multiplication table.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 48

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

Happy coding. :)

Tuesday, 12 January 2016

Kata Day 47: Persistent Bugger


Problem Description: Write a function, persistence, that takes in a positive parameter num and returns its 
multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a 
single digit.

For example:

 persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
                       // and 4 has only one digit

 persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
                        // 1*2*6 = 12, and finally 1*2 = 2

 persistence(4) === 0 // because 4 is already a one-digit number

Level:6kyu

Link To Kata:  Persistent Bugger

This Kata asks us to return the count for which we can multiply the digits of a number until we reach a single digit. A fairly easy kata.

We start solving this kata by converting the given int to a string and then we loop through the string. We initialize a variable prod equal to 1 and repetitively multiply every digit of the input number with the prod. After looping through every digit of the input digit in string format we change the value of input integer to that of prod. We keep repeating this process until we reach a point where we get a single digit value of input number. Every time we repeat through this process we keep an account of counter and keep increasing it by 1.

In the end we return the count as the solution to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 47

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

Happy coding. :)

Monday, 11 January 2016

Kata Day 46: X marks the spot!


Problem Description: Write a function x(n) that takes in a number n and returns an nxn array with an X in the middle.
The X will be represented by 1's and the rest will be 0's. 
E.g.

x(5) === [[1, 0, 0, 0, 1],
          [0, 1, 0, 1, 0],
          [0, 0, 1, 0, 0],
          [0, 1, 0, 1, 0],
          [1, 0, 0, 0, 1]];

x(6) === [[1, 0, 0, 0, 0, 1],
          [0, 1, 0, 0, 1, 0],
          [0, 0, 1, 1, 0, 0],
          [0, 0, 1, 1, 0, 0],
          [0, 1, 0, 0, 1, 0],
          [1, 0, 0, 0, 0, 1]];

Level:6kyu

Link To Kata: X marks the spot!

This kata asks us to prepare a nxn array with an X printed in the middle. To print an X we have to make all the diagonal elements equal to 1 and keep other elements equal to 0.

We start doing this kata by initializing all the elements of the nxn array to 0.

Then we loop through the nxn array through two while loops and make all the elements equal to 1 whose both looping index are same. Doing this process makes all the elements on our right diagonal equal o 1.

We once again loop through the array and make all the elements equal to 1 where the sum of looping indexes is equal to n-1. This process makes all the elements on our left diagonal equal to 1.

After these two processes we end up with a nxn array with a X in the middle. We return the nxn array as the solution to this kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 46

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

Happy coding. :)