Thursday, 31 December 2015

Kata Day 35: Going to cinema


Problem Description: My friend John likes to go to the cinema. He can choose between system A and system B.

System A : buy a ticket (15 dollars) every time
System B : buy a card (500 dollars) and every time buy a ticket the price of which is 0.90 times the price he paid for the previous one.
Example:

If John goes to the cinema 3 times:

System A : 15 * 3 = 45
System B : 500 + 15 * 0.90 + (15 * 0.90) * 0.90 + (15 * 0.90 * 0.90) * 0.90
John wants to know how many times he must go to the cinema so that the final result of System B, when rounded up to the next dollar, will be cheaper than System A.

The function movie has 3 parameters: card (price of the card), ticket (normal price of a ticket), perc (fraction of what he paid for the previous ticket) and returns the first n such that

ceil(price of System B) < price of System A.
More examples:

movie(500, 15, 0.9) should return 43
    (with card the total price is 634, with tickets 645)
movie(100, 10, 0.95) should return 24
    (with card the total price is 235, with tickets 240)

Level:7kyu

It is a Kata involving basic implementation of maths. We have to find the number of tickets where the total cost involved in system B comes out to be less than the cost involved in system A. We start from 1 and then keep increasing the number of tickets by 1 and keep comparing the value in two systems.

Whenever the value of cost involved in system B is found less than system A we break out of the loop and return the value of number of tickets  as our answer to this Kata.

Happy New Year!!

Link to Kata: Going to cinema



Here is a link to the code: Kata Day 35

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

Happy coding. :)

Tuesday, 29 December 2015

Kata Day 34: 80's Kids #3: Punky Brewster's Socks


Problem Description: Punky loves wearing different colored socks, but Henry can't stand it.

Given an array of different colored socks, return a pair depending on who was picking them out.

Example:

getSocks('Punky',['red','blue','blue','green']) -> ['red', 'blue']
Note that punky can have any two colored socks, in any order, as long as they are different and both exist. Henry always picks a matching pair.

If there is no possible combination of socks, return an empty array.

Level:7kyu

Link to Kata: 80's Kids #3: Punky Brewster's Socks

Another Kata from the 80's kids series. In this kata the aim was to return a pair of socks based upon who was choosing the socks and if such a pair was not possible then return an empty string.

In the starting I sorted the given array of socks so that if Henry tries to choose the socks then the repeated socks come in a sequence. I declare an answer array and a variable c which I have initialized to 0. Then I check if Henry or Punky were choosing the socks. Based on the name I loop through the sock array. If Punky is making the call then I choose two socks which are different in color. Push both socks in answer array and change the value of c to 1. If Henry is making the call then I check for two socks which are same in color. If I found such a pair then I push the pair to answer array and change the value of c to 1. In both cases if I get the desired result I break out of the loop.

In the end I check for the value of c. If it is equal to 1 then I return the answer array as a solution to this Kata else if its equal to 0 I return an empty array.

Here is a link to the code: Kata Day 34

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

Happy coding. :)

Monday, 28 December 2015

Kata Day 33: 80's Kids #7: She's a Small Wonder


Problem Description: Vicky is quite the small wonder. Most people don't even realize she's not a real girl, but a robot living amongst us. Sure, if you stick around her home for a while you might see her creator open up her back and make a few tweaks and even see her recharge in the closet instead of sleeping in a bed.

In this kata, we're going to help Vicky keep track of the words she's learning.

Write a function, learnWord(word) which is a method of the Robot object. The function should report back whether the word is now stored, or if she already knew the word.

Example:

var vicky = new Robot();
vicky.learnWord('hello') -> 'Thank you for teaching me hello'
vicky.learnWord('abc') -> 'Thank you for teaching me abc'
vicky.learnWord('hello') -> 'I already know the word hello'
vicky.learnWord('wow!') -> 'I do not understand the input'

Case shouldn't matter. Only alpha characters are valid. There's also a little trick here. Enjoy!

Level:6kyu

Link to Kata: 80's Kids #7: She's a Small Wonder

Our aim in this Kata was to give a word to Vicky the robot and she will check if its a valid word or not. Then if she doesn't recognize this word from her memory then she will store the word for future use in her memory and thank you for helping her learn that word otherwise if she already knows the word then she will tell you that she already knows the given word.

I initialized an array in Robot function as this.arr so that its value can be used down the line in the object of Robot class. In learnWord function I initialized a variable res in which I will store vicky's response. Vicky has some responses from which we know that she already knows those words so I initialized those words in an array knowledge. 

I split the input word using the space delimiter and then check that the length of this array is equal to 1 and the word is not empty. If the above two conditions are satisfied then I change the given word to lower case in order to avoid case conflict. Using regex I check if the given word contains only alphabets and no numbers or special characters. If the above condition is true then the index of given word is checked in the this.ans array and knowledge array and if the index in both the cases is -1 then I push the given word to this.ans array and put vicky's response as thank you and return the res.

If the above condition is not satisfied and the word is already present in this.arr or knowledge then I put vicky's response as "I already know this word" and output the response. In all the other cases vicky's response would be that the given word is invalid.

Here is a link to the code: Kata Day 33

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

Happy coding. :)

Sunday, 27 December 2015

Kata Day 32: Length of the line segment


Problem Description: Find the length between 2 co-ordinates. The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:

(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]

The function must return the answer rounded to 2 decimal places in the form of a string.

lengthOfLine([ [ 0 , 0 ] , [ 5, -7 ] ]); => "8.60"
If the 2 given co-ordinates are the same, the returned length should be "0.00"

Level:7kyu

Link to Kata: Length of the line segment

Our old school distance theorem in form of a Kata. We have to find distance between two points which are given as two arrays. I initialized the four coordinates as x1,y1,x2,y2 from the input arrays and then applied the distance formula by using the Math functions of Javascript. I then converted the value which I got after applying distance formula to a string and parsed it to 2 decimal places.

In the end I returned the parsed value as the solution to this Kata.

Here is a link to the code: Kata Day 32

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

Happy coding. :)

Kata Day 31: Array comparator:


Problem Description: You have two arrays in this kata, every array contain only unique elements. Your task is to calculate number of elements in first array which also are in second array.

Level:7kyu

Link to Kata: Array comparator

This was a simple Kata. We have to find intersection of one array compared to another array. I initialized a variable "count" equal to 0. I looped through the given array and it can be any array as both the given arrays are of same length. Then I checked index of every element of first array in the second array and if the index returned was not equal to -1 then I incremented the count by 1.

In the end once the loop is finished I output the value of count as the solution to this Kata.

Here is a link to the code: Kata Day 31

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

I finished 1 month of coding!!

Happy coding. :)

Friday, 25 December 2015

Kata Day 30: Character Counter


Problem Description: You are going to be given a word. Your job will be to make sure that each character in that word has the exact same number of occurrences. You will return true if it is valid, or false if it is not.

For example:

"abcabc" is a valid word because 'a' appears twice, 'b' appears twice, and'c' appears twice.
"abcabcd" is NOT a valid word because 'a' appears twice, 'b' appears twice, 'c' appears twice, but 'd' only appears 
once!
"123abc!" is a valid word because all of the characters only appear once in the word.

For this kata, capitals are considered the same as lowercase letters. Therefore: 'A' == 'a' .

Input

A string (no spaces) containing [a-z],[A-Z],[0-9] and common symbols. The length will be 0 < string < 100.

Output

true if the word is a valid word, or false if the word is not valid.

Level:7kyu

Link to Kata: Character Counter

Merry Christmas everyone!!

This turned out to be a lengthy Kata if we go through the brute force approach. If we use the regex then the solution can be simplified but I used the brute force approach to solve this. 

First I used few functions to convert the complete input string to lower case and then split each character of the string and stored them in an array. Then I sorted the array using .sort() function. I declared two empty arrays as "check" and "mid_ans". I looped through the array in which I have stored the characters of our input string and pushed the characters on each index to "mid_ans" array if the character on the looping index is same as the next character in the array. Otherwise I push the "mid_ans" array to "check" array and then reinitialized the "mid_ans" array to an empty array. 

Once I was done looping through the entire array  I initialized a variable c to a value of 1 and a target variable to the length of first element of "check" array. I looped through the entire check array from index 1 and checked if the length of every sub-array. If any of these lengths is not equal to the target variable I changed the value of c to 0 and break out of the loop. 

Based on the value of c I returned true or false as our answer for this Kata.

Here is a link to the code: Kata Day 30


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


Happy coding. :)

Thursday, 24 December 2015

Kata Day 29: Count the digit


Problem Description: Take an integer n (n >= 0) and a digit d (0 <= d <= 9) as an integer. Square all numbers k (0 <= k <= n) between 0 and n. Count the numbers of digits d used in the writing of all the k**2. Call nb_dig (or nbDig or ...) the function taking n and d as parameters and returning this count.

Examples:

n = 10, d = 1, the k*k are 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
We are using the digit 1 in 1, 16, 81, 100. The total count is then 4.

nb_dig(25, 1):
1, 4, 9, 10, 11, 12, 13, 14, 19, 21 squared are 1, 16, 81, 100, 121, 144, 169, 196, 361, 441
so there are 11 digits `1` for the squares of numbers between 0 and 25.
Note that 121 has twice the digit 1.

Level:7kyu

Link to Kata: Count the digit

My approach while solving this Kata was to go through string method rather than integer method. I first of all converted the integer which we have to count i.e. "d" to a string and assigned the string value to a new variable "str_d". We assign a variable count to a value of 0.

Then we start a loop where we go from the given input "n" to 0. In each step we assign the value of square of "n" in string format to a variable num. Then we find the count of "str_d" in num by using split method of Javascript and assign it to a variable matchCount. We add the value of matchCount to count and decrease the value of n by 1 in each looping step.

In the end we return the value of count as our answer. 

Here is a link to the code: Kata Day 29

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


Happy coding. :)

Kata Day 28: Number Pairs


Problem Description: In this Kata the aim is to compare each pair of integers from 2 arrays, and return a new array of large numbers.

Note: Both arrays have the same dimensions.

Example:

let arr1 = [13, 64, 15, 17, 88];
let arr2 = [23, 14, 53, 17, 80];
getLargerNumbers(arr1, arr2); // Returns [23, 64, 53, 17, 88]

Level:7kyu

Link to Kata: Number Pairs

It is a pretty easy Kata where we have to find maximum of two arrays at the same index. We initialize an empty array "ans". Then we loop from 0 to one less than the length of input array. Then we compare the element at the looping index from both the input arrays and then we push the bigger element to the "ans" array.

Once we are done looping through the array then we will return the "ans" array as our answer. 

Here is a link to the code: Kata Day 28

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


Happy coding. :)

Tuesday, 22 December 2015

Kata Day 27: Reversing Fun


Problem Description: You are going to be given a string. Your job is to return that string in a certain order that I
will explain below:

Let's say you start with this: 012345

The first thing you do is reverse it:543210
Then you will take the string from the 1st position and reverse it again:501234
Then you will take the string from the 2nd position and reverse it again:504321
Then you will take the string from the 3rd position and reverse it again:504123

Continue this pattern until you have done every single position, and then you will return the string you have created. For this particular number, you would return:504132

Input:

A string of length 1 - 1000

Output:

A correctly reordered string.

Level:7kyu

Link to Kata: Reversing Fun

This Kata asks us to reverse a given string keeping every index of the string as a base. So I first reversed the string using the first character of the string. Then I looped through the string from index 1 to the end of the string and divided the string in two parts. The first part being a sub string of original string from index 0 to just before the index of loop. The next part was a sub string of original string from index of the loop to the end of the string. I reversed the second sub string and merged it with the first string to redefine the input string. I repeated the process for every index of the string.

Once the looping is done I return the redefined input string as the output for the function. 

Here is a link to the code: Kata Day 27

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


Happy coding. :)

Monday, 21 December 2015

Kata Day 26: Finding Remainder Without Using '%' Operator


Problem Description: You have to write a function remainder which takes two arguments : D(Dividend) and d(Divisor) and returns the remainder when D(Dividend) is divided by d(Divisor).

Assumptions:

The Arguments will always be an Integer.
D(Dividend) will always be greater than or equal to d(Divisor).
Note:

Make sure that the implemented remainder function works exactly same as the Modulus Operator(%) does i.e. m % 0 = NaN  ∀ m ∈ ℕ the only difference would be that here 'NaN' will be a string.

Level:7kyu

Link to Kata: Remainder without modulus

In this Kata we have to find the remainder when a number D(Dividend) is divided by d(Divisor) but without using a modulus operator. We know that when we are using a modulus operator what we are actually doing is subtracting the divisor from the dividend for a number of times. So for example if we have to find remainder of 13 divided by 2 we would subtract 2 from 13 6 times and then we will be left with 1 as our remainder as we cannot subtract 2 any further.

In this Kata we check that if the divisor is 0 then we would just return a string "NaN" avoiding the division by zero. Else we would subtract product of Math.floor(D/d)  and d from D.

So here if D=13 and d=2 then D/d comes out to be 6.5 and after using Math.floor() function it comes out to be 6 which we multiply by 2 and get a product as 12 and subtract this product from 13 and get our remainder as 1.

Here is a link to the code: Kata Day 26

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


Happy coding. :)


Kata Day 25: Ghostbusters


Problem Description: Oh no! Ghosts have reportedly swarmed the city. It's your job to get rid of them and save the day!

In this kata, strings represent buildings while whitespaces within those strings represent ghosts.

So what are you waiting for? Return the building(string) without any ghosts(whitespaces)!

Example:

ghostBusters("Sky scra per");
Should return:

"Skyscraper"
If the building contains no ghosts, return the string:

"You just wanted my autograph didn't you?"

Level:7kyu

Link to Kata: Ghostbusters 

In this Kata we have to remove the white spaces from the given string. We have to keep a check if the given string contains white spaces or not. If it doesn't then we return a string as reply - "You just wanted my autograph didn't you?"

We start by checking the index of white space in the given string by using Javascript's .indexOf() function. If it is equal to -1 then we return our reply otherwise we use replace function of regex and replace all the white spaces with no spaces.

In the end we return our string from which we have removed the white spaces.

Here is a link to the code: Kata Day 25

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


Happy coding. :)

Saturday, 19 December 2015

Kata Day 24: Mumbling


Problem Description: This time no story, no theory. The examples below show you how to write function accum:

Examples:

accum("abcd") --> "A-Bb-Ccc-Dddd"
accum("RqaEzty") --> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") --> "C-Ww-Aaa-Tttt"

The parameter of accum is a string which includes only letters from a..z and A..Z.

Level:7kyu

Link to kata: Mumbling

For the above Kata we first split the given string into an array of characters by Javascript .split() function. Then we declare a variable "ans" initialized to an empty string. We loop through every character of an array starting with index 0. We add a string of character of array in uppercase form and then we add the lowercase form of same character the same number of times the index. After this we check if the index has reached the end of the array or not. If it has not then we add a '-' to the "ans" string otherwise we just come out of the loop.

In the end we return the "ans" variable as our output for the function. 

Here is a link to the code: Kata Day 24

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


Happy coding. :)

Friday, 18 December 2015

Kata Day 23: Maximum Length Difference


Problem Description: You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.

Find max(abs(length(x) − length(y)))

If a1 or a2 are empty return -1 in each language except in Haskell where you will return Nothing.

Example:

s1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
s2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(s1, s2) --> 13

Level:7kyu

Link to kata: Maximum length difference

In this Kata we are given two arrays with a variable number of strings. We have to find the maximum difference of length of two strings from both the arrays. We start solving this by initializing 2 empty arrays. We start to loop through the first input array and store the length of each string in the first initialized array. We do the same for second input array and store the length of each string in second initialized array.

Once we have got length of all the strings of two input arrays in our two new arrays we sort those two arrays in ascending order using Javascript prototype so that we can have length of strings from shortest to the longest string. Then we find the absolute difference of first element of first array and last element of second array. We also find the absolute difference of last element of first array and first element of second array. 

Then we return the maximum of these two differences using Javascript Math.max function as our output. 

Here is a link to the code: Kata Day 23

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


Happy coding. :)

Thursday, 17 December 2015

Kata Day 22: Finding factors


Problem Description: Create a function that takes a number and finds the factors of it, listing them in descending order in an array.

If the parameter is not an integer or less than 1, return -1. In C# return an empty array.

For Example: factors(54) should return [54, 27, 18, 9, 6, 3, 2, 1]

Level:7kyu

Link to kata: Finding Factors

A very basic problem in form of a Kata which can be used to understand how different operators work in Javscript. We are given a number and we have to write a function which can find the factors of that number and list them in decreasing order. We have to also verify that whether the given number is an integer or not.

We first of all parse the given number with base 10 and check if the given number and its parsed version are equal. We also check if its greater than or equal to 1. If the above condition is satisfied then we pass the given input integer to a function called "factor". 

In the function factor we initialize an array "ans". If the given integer is equal to 1 then we just return 1 and that is the end of our function. In other cases we run through a while loop from 1 to a value equal to half that of the input. If our input is divisible by the looping index then we push it to the "ans" array. At last we push the input to "ans" array because every number is a factor of itself. 

Once we are done looping through the while loop we output the value of "ans" in reverse order by using Array.reverse() function.

Here is a link to the code: Kata Day 22


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


Happy coding. :)