Problem Description: Write a function insertDash(num) that will insert dashes ('-') between each two odd numbers in num.
For example: if num is 454793 the output should be 4547-9-3.
Don't count zero as an odd number.
Level: 7kyu
Link to Kata: Insert Dashes
In this Kata we have to insert a dash between two odd numbers. I initially converted the input argument num to a string and then used a while loop to go through every element of the string one by one. I initialized a variable "ans" as an empty string.
While looping through each element of the string we check that whether that element when converted to an integer leaves a remainder of 1 when divided by 2 (Condition for odd number) and also if the element which is present next to it when converted to an integer leaves a remainder of 1 when divided by 2. If these two condition satisfies then we would add the element which was present on the counter index followed by a "-" to the "ans" variable otherwise we would just add the element to the variable.
We will also have to keep a check on the counter variable so that we do not check for the element present next to counter when we reach the end of the string.
Once the above procedure is done through the loop we would be able to get the desired result.
Here is a link to the code: Kata Day 6
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment