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"
public class Tortoise {
public static int[] race(int v1, int v2, int g) {
if(v2 -v2 < 0)
return null;
double d = Double.valueOf(g)/(v2-v1);
int hours = (int) d;
int minutes = (int) (d * 60) % 60;
int seconds = (int) (d * (60*60)) % 60;
return new int [] {hours,minutes,seconds};
}
}
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).
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class JomoPipi {
public static String numericals(String s) {
Map<String, AtomicInteger> map = new HashMap<>();
StringBuilder result = new StringBuilder();
for (String element :s.split("")) {
int temp;
if(map.containsKey(element)){
temp=map.get(element).incrementAndGet();
}else{
temp = 1;
map.put(element, new AtomicInteger(1));
}
result.append(temp);
}
return result.toString();
}
}
var moveZeros = function (arr) {
var result = [];
var zeros = [];
for(var i = 0 ; i < arr.length ; i ++){
if(arr[i] === 0)
zeros.push(arr[i]);
else
result.push(arr[i]);
}
return result.concat(zeros);
}
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"
function solution(input, markers){
var comments = input.split('\n');
for (var i in markers) {
for (var j in comments) {
var line = null;
var idx = comments[j].indexOf(markers[i]);
if (idx >= 0) {
comments[j] = comments[j].substring(0, idx).trim();
}
}
}
return comments.join('\n');
}
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!
}
}
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.
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].
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.
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.
import java.util.Arrays;
public class Max {
public static int sequence(int[] arr) {
int result = 0;
for(int i=0; i< arr.length ; i++ ) {
for(int j=i+1; j< arr.length+1; j++) {
int sum = Arrays.stream(Arrays.copyOfRange(arr, i, j)).sum();
if(sum > result)
result = sum;
}
}
return result;
}
}
public class Max {
public static int sequence(int[] arr) {
int cur = 0, max = 0;
for (int i : arr) {
cur = Math.max(0, cur + i);
max = Math.max(max, cur);
}
return max;
}
}