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};
}
}
Given an array of one's and zero's convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
Examples:
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11
However, the arrays can have varying lengths, not just limited to 4.
import java.util.List;
import java.util.stream.Collectors;
public class BinaryArrayToNumber {
public static int ConvertBinaryArrayToInt(List<Integer> binary) {
// Your Code
return Integer.parseInt(String.join("",binary.stream().map(String::valueOf).collect(Collectors.joining())), 2);
}
}
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();
}
}
You wrote all your unit test names in camelCase. But some of your colleagues have troubles reading these long test names. So you make a compromise to switch to underscore separation.
To make these changes fast you wrote a class to translate a camelCase name into an underscore separated name.
Implement the ToUnderscore() method.
Example:
"ThisIsAUnitTest" => "This_Is_A_Unit_Test"
But of course there are always special cases...
You also have some calculation tests. Make sure the results don't get splitted by underscores. So only add an underscore in front of the first number.
Also Some people already used underscore names in their tests. You don't want to change them. But if they are not splitted correct you should adjust them.
Some of your colleagues mark their tests with a leading and trailing underscore. Don't remove this.
And of course you should handle empty strings to avoid unnecessary errors. Just return an empty string then.
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
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);
}
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
function tripledouble(num1, num2) {
num1 = String(num1);
num2 = String(num2);
var triples = [];
for (var i =0 ; i < num1.length - 2 ; i++ ){
if ((num1.charAt(i) == num1.charAt(i+1)) && (num1.charAt(i) == num1.charAt(i+2))) {
triples.push(num1.charAt(i)+num1.charAt(i+1) + num1.charAt(i+2));
}
}
for(var i =0; i < triples.length; i++){
var double = triples[i].substring(0,2);
if(num2.indexOf(double) != -1)
return 1;
}
return 0;
}
function tripledouble(num1, num2) {
for (let i = 0; i < 10; i++) {
if (new RegExp(`${i}{3}`).test(num1) && new RegExp(`${i}{2}`).test(num2)) {
return 1;
}
}
return 0;
}
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.
! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)
##Examples :
iqTest("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
iqTest("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
function iqTest(numbers){
var arr = numbers.split(" ");
var sum = 0;
var mod = 0;
var result = 0;
for(var i = 0; i < 3; i++){
sum += Math.abs(arr[i]) % 2;
}
mod = (sum === 0 || sum === 1) ? 1 : 0;
arr.some(function(val, idx){
if(Math.abs(val) %2 === mod){
result = idx;
return true;
}
});
return result+1
}
My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.
I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?
When two numbers have the same "weight", let us class them as if they were strings and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9) it comes before as a string.
All numbers in the list are positive numbers and the list can be empty.
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class WeightSort {
public static int getWeight(String strng) {
return Arrays.stream(strng.split("")).mapToInt(Integer::parseInt).sum();
}
public static String orderWeight(String strng) {
return Arrays.stream(strng.split(" "))
.sorted(Comparator.comparing(WeightSort::getWeight).thenComparing(String::compareTo))
.collect(Collectors.joining(" "));
}
}
You are working for NoVirus Security Solutions and they ask you to make a scanner that scans a file inputted by the user with the function scanFile(File,VirusDB) that takes a File and a VirusDB object and return whether a file is safe or not. Remember: the searches need to be non-Case-Sensitive
Your class also has the function setScanIntensity(int) which changes the scan intensity. This will only receive values 0, 1, 2 or 3. This has been done for you.
The scan intensity determines the arrays from the database that will be used. i.e.:
scanIntensity 0 means off(every file is considered safe)
scanIntensity 1 means that only the array intensity1Signatures will be used
scanIntensity 2 means that the arrays intensity1Signatures and intensity2Signatures will be used
scanIntensity 3 means that all 3 arrays will be used
Outputs
The outputs should be: "Filename is safe" or "Filename is not safe" (Filename is the name of the file that you can get with file.getName() )
File Class
class File{
private String name;
private String data;
public File(String name,String data){
this.name = name;
this.data = data;
}
//used in output
public String getName(){
return this.name;
}
//the String that you need to scan.
public String getData(){
return this.data;
}
}
VirusDB Class
class VirusDB{
private String[] intensity1Signatures;
private String[] intensity2Signatures;
private String[] intensity3Signatures;
public VirusDB(String[] intensity1Signatures,String[] intensity2Signatures,String[] intensity3Signatures){
this.intensity1Signatures = intensity1Signatures;
this.intensity2Signatures = intensity2Signatures;
this.intensity3Signatures = intensity3Signatures;
}
public String[] getSignatures(int arrayNum){
switch (arrayNum){
case 1:return this.intensity1Signatures;
case 2:return this.intensity2Signatures;
case 3:return this.intensity3Signatures;
default:return new String[0];
}
}
}
public class AntiVirus{
private int scanIntensity = 0;
//this method is ready for you.
public void setScanIntensity(int level){
scanIntensity = level;
}
//write this method.
public String scanFile(File file,VirusDB database){
String data = file.getData().toLowerCase();
for(int i = 0; i <= scanIntensity; i ++){
String [] signatures = database.getSignatures(i);
for (String signature : signatures) {
if(data.contains(signature))
return file.getName() + " is not safe";
}
}
return file.getName() + " is safe";
}
}