Wednesday, 16 December 2015

Kata Day 21: Formatting decimal places


Problem Description: Each number should be formatted that only the first two decimal places are returned. You don't need to check whether the input is a valid number because only valid numbers are used in the tests.

Don't round the numbers! Just cut them after two decimal places!

Right examples:  
32.8493 is 32.84  
14.3286 is 14.32

Incorrect examples (e.g. if you round the numbers):  
32.8493 is 32.85  
14.3286 is 14.33

Level:7kyu

Link to kata: Formatting decimal places

This was an easy kata. The only catch was that we don't have to round up a decimal but rather we have to cut the given number to 2 decimal places. I first converted the given number to a string and then found the index of decimal(.) in the string. The index was assigned to a variable 'n'. We then divided the string in two parts. One part was from starting of string to just before the decimal point and the other part from the decimal point to next two places after it. We then merge these two substrings and parse the string to a float. 

Please keep in mind that the online judge fails the solution if we return the string as an answer without parsing it to a float. 

Here is a link to the code: Kata day 21

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


Happy coding. :)

Tuesday, 15 December 2015

Kata Day 20: A Rule of Divisibility by 13


Problem Description: When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:

1, 10, 9, 12, 3, 4.

Then the whole pattern repeats.

Hence the following method: Multiply the right most digit of the number with the left most number in the sequence 
shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.

...........................................................................

Example: What is the remainder when 1234567 is divided by 13?

7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178

We repeat the process with 178:

8x1 + 7x10 + 1x9 = 87

and again with 87:

7x1 + 8x10 = 87

...........................................................................

From now on the sequence is stationary and the remainder of 1234567 by 13 is the same as the remainder of 87 by 13: 9

Call thirt the function which processes this sequence of operations on an integer n (>=0). thirt will return the 
stationary number.

thirt(1234567) calculates 178, then 87, then 87 and returns 87.

thirt(321) calculates 48, 48 and returns 48

Level: 7kyu

Link to Kata: A Rule of Divisibility by 13

This Kata is based on a mathematical result of division by 13. We start solving this Kata by initializing an array "base_arr" to all the remainder which we get when we divide successive powers of 10 by 13. Then we declare a variable "j" equal to 0 and another variable "sum" equal to 0. We start looping through a while loop and multiply every digit on the index with a digit from the "base_arr". Then we increment the value of index and "j" which is an index for base array. If "j" becomes equal to 6 then we reinitialize it to 0. We add the product in every step to "sum" variable. 

Once we are done with the while loop, we check the value of sum and compare it with the initial value of input. If it is the same then we break out from the loop or else we assign the value of sum to input variable and continue with the above operation again.

In the end we return the value of input variable.

Here is a link to the code: Kata Day 20


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


Happy coding. :)

Monday, 14 December 2015

Kata Day 19: Area and volume


Problem Description: Write a function that returns the total surface area and volume of a box as an array:
[area, volume].

Level: 8kyu

Link to Kata: Area and Volume

Doing a simple Kata as I am a bit short on time. This is a 8kyu Kata so it is a very simple Kata. We are given width,height and depth of a box and we have to find the total surface area and volume of the box. We declare an "ans" array. After we are able to find the total surface area and volume of a box by using formula for a cuboid we push both the values to the "ans" array and output our array.

Here is a link to the code: Kata Day 19


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


Happy coding. :)

Sunday, 13 December 2015

Kata Day 18: Find Count of Most Frequent Item in an Array


Problem Description: Write a JavaScript program to find count of the most frequent item of an array.

Assume that input is array of integers.

Ex.:

input array: [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]

ouptut: 5

Most frequent number in example array is -1. It occures 5 times in input array.


Level: 7kyu

Link to Kata: Find Count of Most Frequent Item in an Array

It was a simple Kata looking at the level of Katas I have solved previously. We were given an array of integers and we have to find the count of the integer which occurred most frequently.

We start by sorting the array in ascending order by using the array prototype Javascript function. Then we create two empty arrays- "ans" and "int_ans". We loop through the sorted array and check if the element on the current index is equal to element at the next index. If it is then we push the element on  current index to "int_ans". If it is not then also we push the element on  current index to "int_ans" but at the same time we push "int_ans" to "ans" array and re-initialize the "int_ans"
 array.

Once we are done looping through the given array we loop though the "ans" array and check for the sub-array with maximum length. Once we find the sub-array with maximum length we output the length as our answer.

Here is a link to the code: Kata Day 18

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

Happy coding. :)


Saturday, 12 December 2015

Kata Day 17: Summarize Ranges


Problem Description: Write function summaryRanges(nums) that given a sorted array of numbers, returns the summary of its ranges.

For example:

summaryRanges([1,2,3,4]) === ['1->4']
summaryRanges([0, 1, 2, 5, 6, 9]) === ["0->2", "5->6", "9"]
summaryRanges([0, 1, 2, 3, 3, 3, 4, 5, 6, 7]) === ["0->7"]
summaryRanges([0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10]) === ["0->7","9->10"]
summaryRanges([-2,0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10, 12]) ===["-2", "0->7", "9->10", "12"]
summaryRanges([1,1,1,1,1]) === ['1']

Level: 6kyu

Link to Kata: Summarize Ranges

This was a very interesting Kata. We are given with a sorted array of numbers and we ave to find the range of numbers which are present in the array. We initialize an "ans" array and start going through every element of the array using a while loop. If the difference of element on the index is 1 or 0 we push the element to another "inst" array which we initialized in the starting of the loop. If we do not find such an element then we push the  "inst" array to "ans" array and re-initialize the "inst" array to an empty array. 

After we are done with the above step we start to go through every sub-array of the answer array and check if all the elements of sub-array are equal or not using a function. If it is then we put the single element in the "answer" which we initialized in the starting. If its not equal then we push a string which contains first element of sub-array followed by a "->" and then the last element of sub-array so as to define the range of that sub-array.

Finally we output the "answer" in order to output the required ranges.

This was a fairly complex Kata so go through the code and explanation simultaneously and please mention if you can come up with a better approach.

Here is a link to the code: Kata Day 17



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

Happy coding. :)

Thursday, 10 December 2015

Kata Day 16: Playing with digits


Problem Description: Some numbers have funny properties. For example:

89 --> 8¹ + 9² = 89 * 1

695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.

Note: n, p will always be given as strictly positive integers.

Level: 6kyu

Link to Kata: Playing with digits

As shown in the example above 89 is having properties similar to a funny number. If we add squares of its digits we get a number which can be expressed as a product of two integers out of which 1 integer is 89. We have to find whether the given number has properties similar to a funny number or not.

First we find the sum of squares of individual digits of the given number. Then we initialize a variable to a value of 1 and start a while loop to check if the product of variable and given integer is equal to the sum we calculated.

If we are able to equate the product and the sum, we return true otherwise we continue to increment the value of the variable till the value of product obtained exceeds the sum, in which case we return false.

Here is a link to the code: Kata Day 16

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

Happy coding. :)

Kata Day 15: Find the odd int


Problem Description: Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

Level: 6kyu

Link to Kata: Find the odd int

In this Kata we have to find the integer which comes odd number of times. We know that if we sort the array then all the occurrences of a particular integer will be in one continuous line. We initialize an array "indexes" and then we loop through the array. We push the index of the loop to the initialized array till we get the same integers. Once we stop getting the same integers we break out of the loop and check the length of indexes array. If its an odd number then we return the integer which we were checking as answer to this Kata otherwise we reinitialize the indexes array and continue checking with the next integer.

Here is a link to the code: Kata Day 15

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

Happy coding. :)