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. :)
No comments:
Post a Comment