Sunday, 10 January 2016

Kata Day 45: Triple trouble


Problem Description: Write a function tripledouble(num1,num2) which takes in numbers num1 and num2 and returns 1 if there is a straight triple of a number at any place in num1 and also a straight double of the same number in num2.

For example:
tripledouble(451999277, 41177722899) == 1 // num1 has straight triple 999s and 
                                          // num2 has straight double 99s

tripledouble(1222345, 12345) == 0 // num1 has straight triple 2s but num2 has only a single 2

tripledouble(12345, 12345) == 0

tripledouble(666789, 12345667) == 1
If this isn't the case, return 0

Level:6kyu

Link To Kata: Triple trouble

This kata asks us to check if the given input which consists of two numbers have a triplet and a twin of the same number within the given number.

We start solving this kata by converting the given numbers to string and then we define two flags and set them to 0. We initialize two empty arrays. We start looping through the first number and check for triplets. If we find them then we push them to our first empty array and set our flag to 1. Keep in mind that there can be more than 1 triplets in a given number.

If our flag1 is still zero after coming out of the loop then we return 0 as the solution to this kata and come out of the function. But if the flag1 is 1 then we proceed by looping through the second number and check for twins and push them to second empty array and set flag2 to 1.

We check flag2 after the loop and work in a similar manner as of flag1 in case it is 0. If both the flags are 1 then we check for common elements in two arrays. If we find a common element then we return 1 as the solution to this kata. But if there are no common elements then we return a 0.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 45

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

Happy coding. :)

Saturday, 9 January 2016

Kata Day 44: Count the divisible numbers


Problem Description: Write function divisibleCount(x, y, k) that takes in 3 numbers x, y and k, and returns the number of integers within the range [x..y] that are divisible by k i.e.: 
{ i : x ≤ i ≤ y, i mod k = 0 }
For example:
x = 6, y = 11 and k = 2, your function should return 3, because there are three numbers divisible by 2 within the 
range [6..11], namely 6, 8 and 10.
Note: x<=y

Level:6kyu

Link To Kata: Count the divisible numbers

This array at first look seems to be a basic problem where we can loop from the starting range to the end range and count the numbers divisible by the given number. But there is the catch. This naive solution ends up slowing down our algorithm which results in time limit exceeded. 

So in order to solve this Kata we take a much more algorithmic approach. We first of all find the remainder of starting range number by using a modulus operator against the divisor. If the divisor is not equal to 0 then we add the difference of divisor and remainder to x. Next we subtract the remainder in case of end range number using the same method from the end range number.

Then if we start range is greater then end range we conclude that there is no such number in our given range which is completely divided by our divisor. We return a 0 as the solution to this kata.

Otherwise we divide the difference of end range and start range by our divisor. We add 1 to the value which we get after dividing and return the sum as the solution to this Kata. 

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 44


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

Happy coding. :)

Friday, 8 January 2016

Kata Day 43: Greatest Position Distance Between Matching Array Values


Problem Description: The goal of this Kata is to return the greatest distance of index positions between matching number values in an array or 0 if there are no matching values.

Example: In an array with the values [0, 2, 1, 2, 4, 1] the greatest index distance is between the matching '1' 
values at index 2 and 5. Executing greatestDistance against this array would return 3. (i.e. 5 - 2)

Here's the previous example in test form:

Test.assertEquals(greatestDistance([0, 2, 1, 2, 4, 1]), 3);
This is based on a Kata I had completed only to realize I has misread the instructions. I enjoyed solving the 
problem I thought it was asking me to complete so I thought I'd add a new Kata for others to enjoy. There are no tricks in this one, good luck!

Level:6kyu

Link To Kata:  Greatest Position Distance Between Matching Array Values

In this kata we have to find two equal numbers in a given array where the distance is maximum. We start solving this kata by initializing a variable dist to 0. We loop through the given array and initialize another index variable which is 1 more than the current index. We find the element which is equal to the element in the looping index and store the difference of two indexes in the dist variable. 

In the end we return the value of dist variable as the solution to this Kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 43


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

Happy coding. :)

Thursday, 7 January 2016

Kata Day 42: Alternating Loops


Problem Description: Write a function combine()that combines arrays by alternately taking elements passed to it.

E.g

combine(['a', 'b', 'c'], [1, 2, 3]) == ['a', 1, 'b', 2, 'c', 3]
combine(['a', 'b', 'c'], [1, 2, 3, 4, 5]) == ['a', 1, 'b', 2, 'c', 3, 4, 5]
combine(['a', 'b', 'c'], [1, 2, 3, 4, 5], [6, 7], [8]) == ['a', 1, 6, 8, 'b', 2, 7, 'c', 3, 4, 5]
Arrays can have different lengths.


Level:6kyu

Link To Kata:  Alternating Loops

We put all the input arrays in a single array using the slice function of Javascript. We declare a variable max_len and initialize it to zero. We loop through the input merged array 
and find the max_len value by comparing it with length of every sub array and comparing it with the max_len value. 

We initialize an empty array as ans. Then we loop from 0 to max_len and push 1 element from every subarray by keeping a check if the array has reached its last element or not.

In the end we output ans array as the solution to this Kata.

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 42

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

Happy coding. :)

Wednesday, 6 January 2016

Kata Day 41: Surrounding Primes for a value


Problem Description: We need a function prime_bef_aft() that gives the largest prime below a certain given value n, befPrime or bef_prime (depending on the language),and the smallest prime larger than this value,
aftPrime/aft_prime (depending on the language).

The result should be output in a list like the following:

primeBefAft == [befPrime, aftPrime]
If n is a prime number it will give two primes, n will not be included in the result.

Let's see some cases:

primeBefAft(100) == [97, 101]

primeBefAft(97) == [89, 101]

primeBefAft(101) == [97, 103]
Happy coding!!

Level:6kyu

Link To Kata: Surrounding Primes for a value

In this Kata the problem asks us to find the greatest prime number less than a given prime number and smallest prime number greater than the given prime number. 

We create an empty answer array. We loop in a decreasing order from a number one less than the given number. We push the number which is prime in the answer array and break out of the loop.

We do the same but in increasing order from the given number. We check for prime by a simple prime check function. In the end we output the answer array as the solution to this Kata.

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 41

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

Happy coding. :)

Tuesday, 5 January 2016

Kata Day 40: Divisible Ints


Problem Description: You are given an integer N. Your job is to figure out how many substrings inside of N divide evenly with N.

Confused? I'll break it down for you.

Let's say that you are given the integer '877692'.

8 does not evenly divide with 877692. 877692/8 = 109711 with 4 remainder.

7 does not evenly divide with 877692. 877692/7 = 125384 with 4 remainder.

7 does not evenly divide with 877692. 877692/7 = 125384 with 4 remainder.

6 evenly divides with 877692. 877692/6 = 146282 with 0 remainder.

9 does not evenly divide with 877692. 877692/9 = 97521 with 3 remainder.

2 evenly divides with 877692. 877692/2 = 438846 with 0 remainder.
We aren't going to stop there though. We need to check ALL of the substrings inside of 877692.

87 does not evenly divide with 877692. 877692/87 = 10088 with 36 remainder.

77 does not evenly divide with 877692. 877692/77 = 11398 with 46 remainder.

76 does not evenly divide with 877692. 877692/76 = 11548 with 44 remainder.

69 does not evenly divide with 877692. 877692/69 = 12720 with 12 remainder.

etc.
Rules:

-If an integer is 0, then it does NOT divide evenly into anything.
-Even though N can divide evenly with itself, we do not count it towards the end number. For Example:

N = 23, the answer will be 0.
-If there are multiple instances of a number, they all get counted. For example:

N = 11, the answer will be 2
Input:

A non negative integer.

Output:

The number of times you found an integer that was evenly divisible with N.

Level:6kyu

Link To Kata: Divisible Ints

In this particular Kata we have to find the number of substrings of a given number who completely divide te given number.We start solving the Kata by converting the given numbers to a string as it makes using the substring parts easier for us. We initialize  a variable count equal to 0 to keep in check of the number of times we get the desired results. Then we start to loop from 1 to just before the length of the substring of input number. For every counter of the loop we initialize a variable i equal to 0. We check if the number divided by te substring from i to the sum of i and the loop counter gives a remainder of 0 and if the divisor substring is not equal to 0 or equal to the input number itself. If the above conditions are satisfied then we increment our count variable by 1. We repeat the above procedure for every loop counter.

In the end we output the count variable as the solution to this Kata.  

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 40

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

Happy coding. :)

Monday, 4 January 2016

Kata Day 39: Equal Sides Of An Array


Problem Description: You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.

For example:

Let's say you are given the array {1,2,3,4,3,2,1}:
Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index 
({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.

Let's look at another one.
You are given the array {1,100,50,-51,1,1}:
Your function will return the index 1, because at the 1st position of the array, the sum of left side of the index 
({1}) and the sum of the right side of the index ({50,-51,1,1}) both equal 1.

Note: Please remember that in most programming/scripting languages the index of an array starts at 0.

Input:

An integer array of length 0 < arr < 1000. The numbers in the array can be any integer positive or negaive.

Output:

The index N such as the side to the left of N is equal to the side to the right of N. If you do not find an index 
that fits these rules, then you will return -1.

Level:6kyu

Link To Kata: Equal Sides Of An Array

Another level 6 Kata. I believe I'm making progress. Here we need to find an index in an array where the sum of elements to the left of index is equal to sum of elements to the right of that index. If such an index is not possible then we return a value of -1.

We start solving this array by initializing an index variable to 1 and an answer variable to -1.
We loop through every element in the array and for every looping index we initialize two variables sum1 and sum2 to 0. We loop from the starting of array to just before the looping index and also from the element at index just after the index to the end of the array. In both cases we store the sum of elements in sum1 and sum2. We check if both these values are same and if they are then we change the value of answer to the index of the loop and break out of the loop. Otherwise we keep increasing the looping index.

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

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 39

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

Happy coding. :)