#77 Tortoise racing


Two tortoises named A and B must run a race. A starts with an average speed of 720 feet per hour. Young B knows she runs faster than A, and furthermore has not finished her cabbage.


When she starts, at last, she can see that A has a 70 feet lead but B's speed is 850 feet per hour. How long will it take B to catch A?


More generally: given two speeds v1 (A's speed, integer > 0) and v2 (B's speed, integer > 0) and a lead g (integer > 0) how long will it take B to catch A?


The result will be an array [hour, min, sec] which is the time needed in hours, minutes and seconds (round down to the nearest second) or a string in some languages.


If v1 >= v2 then return nil, nothing, null, None or {-1, -1, -1} for C++, C, Go, Nim, [] for Kotlin or "-1 -1 -1".


Examples:

(form of the result depends on the language)


race(720, 850, 70) => [0, 32, 18] or "0 32 18"

race(80, 91, 37)   => [3, 21, 49] or "3 21 49"




 #75 Numericals of a String


You are given an input string.


For each symbol in the string if it's the first character occurence, replace it with a '1', else replace it with the amount of times you've already seen it...


But will your code be performant enough?


Examples:

input   =  "Hello, World!"

result  =  "1112111121311"


input   =  "aaaaaaaaaaaa"

result  =  "123456789101112"

Note: there will be no int domain overflow (character occurences will be less than 2 billion).




#72 Moving Zeros To The End


Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.


moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]


#64 Strip Comments


Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.


Example:


Given an input string of:


apples, pears # and bananas

grapes

bananas !apples

The output expected would be:


apples, pears

grapes

bananas

The code would be called like so:


var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])

// result should == "apples, pears\ngrapes\nbananas"




#60 Two Sum


Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in an array like so: [index1, index2].


For the purposes of this kata, some tests may have multiple answers; any valid solutions will be accepted.


The input will always be valid 

(numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).



public class Solution
{
    public static int[] twoSum(final int[] numbers, int target)
    {
      for(int i = 0; i < numbers.length;i++ ) {
        for(int j = i + 1; j < numbers.length;j++) {
          if(numbers[i] + numbers[j] == target) {
              return new int []{i,j};
            }
          }
        }
        return null; // Do your magic!
    }
}

#58 My Languages 


Your task


Given a list of languages and your respective test results, return the list of languages where your test score is at least 60, in descending order of the results.


Note: There will be no duplicate values.


Examples

{"Java" => 10, "Ruby" => 80, "Python" => 65}  --> ["Ruby", "Python"]

{"Hindi" => 60, "Dutch" => 93, "Greek" => 71} --> ["Dutch", "Greek", "Hindi"]

{"C++" => 50, "ASM" => 10, "Haskell" => 20}   --> []




#57 Frequency Analysis With Buckets


Description:


Given an input array (arr) of positive integers, the objective is to return an output array where each index represents the amount of times an element appeared (frequency) in the input array.


More specifically, the element at each index of the output array will be an array (bucket) containing integers that appeared index-amount-of-times.


Otherwise, slot nulls (JavaScript, Java), nils (Ruby), or NULL's (C/C++) where appropriate. A valid array will always be provided.


If an array of [1,2,3,4,4,5,5,5] is passed in, the expected output should be: [null, [1,2,3], [4], [5], null, null, null, null, null].


Explanation:


// bucketize(arr) ======> outputArray

bucketize({1,2,3,4,4,5,5,5}) ======> {null, {1,2,3}, {4}, {5}, null, null, null, null, null}

An element cannot appear 0 times, so a null is placed at outputArray[0]. The elements 1, 2, and 3 appear once. This is why they are located at outputArray[1]. Notice the elements are grouped together in an array and sorted in ascending order. The element 4 appears twice. This is why it is located at outputArray[2]. The element 5 appears three times. This is why it is located at outputArray[3].


Although an integer could have possibly appeared four, five, six, seven, or eight times, this is not the case for this particular example. This is the reason why the elements at outputArray[4], outputArray[5], outputArray[6], outputArray[7], and outputArray[8] are all null values.


Examples:


bucketize({2,2,4,4,6,6,9,9,9,9}) ===> {null, null, {2,4,6}, null, {9}, null, null, null, null, null, null};

bucketize({3,3,3,3,2}) =============> {null, {2}, null, null, {3}, null};

bucketize({5,5,5,5,5}) =============> {null, null, null, null, null, {5}};

bucketize({77,3,40,40,40}) =========> {null, {3,77}, null, {40}, null, null};

bucketize({16,7,5,3,6,23}) =========> {null, {3,5,6,7,16,23}, null, null, null, null, null};



 #56 Maximum subarray sum


The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:


Max.sequence(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});

// should be 6: {4, -1, 2, 1}

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.


Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.



+ Recent posts