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

Saturday, 2 January 2016

Kata Day 38: Integers - Recreation One


Problem Description: Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 
196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!

Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.

The result will be an array of arrays, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.

Examples:

listSquared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
listSquared(42, 250) --> [[42, 2500], [246, 84100]]

Level:6kyu

Link To Kata: Integers - Recreation One

This is a 6 Kyu kata and so a level up from the previous day's 7 Kyu. In this Kata we aim to find such integers whose sum of squares of divisors is itself a square. We start solving this Kata by making two empty arrays as num_arr and sum_arr. We loop from the starting point of given range to its ending point. For each number we push the number to num_arr and sum of square of its divisor to sum_arr. 

Once we are out of the loop we initialize an empty array as ans_arr. We loop through each element of sum_arr and check if its square root after getting rounded to closest integer value when multiplied by itself i.e. after getting squared comes down to the same value of the sum_arr value. If it is then we push the value at the same index from num_arr to an empty array temp which we initialize every time the loop starts with a new index. We then also push the sum_arr value to temp. After that we push the temp array to ans_arr. We repeat this process for every number. 

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 38


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

Happy coding. :)

Kata Day 37: Split In Parts


Problem Description: The aim of this kata is to split a given string into different strings of equal size (note size
 of strings is passed to the method)

Example:

Split the below string into other strings of size #3

'supercalifragilisticexpialidocious'

Will return a new string
'sup erc ali fra gil ist ice xpi ali doc iou s'

Assumptions:

String length is always greater than 0
String has no spaces
Size is always positive

Level:7kyu

Link To Kata: Split In Parts

In this Kata we have to form sub strings of given length from the input string and output them separated by spaces. The first step which I took while solving this Kata was to initialize an empty string "ans" and then loop through the input string and add the sub string of required length to the ans string along with a space. We increment the index of loop by the same value as of the sub string length. 

If we reach a point where we don't have the sub string of length greater than the required length then we output whatever is left of the input string without any spaces.

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

Here is a link to the code: Kata Day 37

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

Happy coding. :)

Kata Day 36: Return String of First Characters


Problem Description: In this exercise, a string is passed to a method and a new string has to be returned with the first character of each word in the string

example: 'This Is A Test' will return 'TIAT' 

Level:7kyu

Link To Kata: Return String of First Characters

An easy Kata based on strings. We have to output first character of every word of a string. I started solving this Kata by splitting the string into an array based on a space as a delimiter. We initialize an empty array and then we start looping through the array

Here is a link to the code: Kata Day 36

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

Happy coding. :)