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";
}
}
My grandfather always predicted how old people would get, and right before he passed away he revealed his secret!
In honor of my grandfather's memory we will write a function using his formula!
Take a list of ages when each of your great-grandparent died.
Multiply each number by itself.
Add them all together.
Take the square root of the result.
Divide by two.
Example
predictAge(65, 60, 75, 55, 60, 63, 64, 45) === 86
Note: the result should be rounded down to the nearest integer.
import java.util.Arrays;
public class PredictYourAge {
public static int predictAge(int ... ages) {
// your code goes here
return (int) (Math.sqrt(Arrays.stream(ages).boxed().mapToInt(x -> x*x).sum())/2);
}
}
You are given a string of words (x), for each word within the string you need to turn the word 'inside out'. By this I mean the internal letters will move out, and the external letters move toward the centre.
If the word is even length, all letters will move. If the length is odd, you are expected to leave the 'middle' letter of the word where it is.
An example should clarify:
'taxi' would become 'atix' 'taxis' would become 'atxsi'
function insideOut(x){
var result = [];
for(var item of x.split(" ")){
var len = item.length;
if(len>3){
var middle = Math.floor(len/2);
if(len%2 == 0){
item = item.substring(0,middle).split("").reverse().join("").concat( item.substring(middle,len).split("").reverse("").join(""))
}else{
item = item.substring(0,middle).split("").reverse().join("").concat(item.substring(middle,middle+1)).concat( item.substring(middle+1,len).split("").reverse("").join(""))
}
}
result.push(item);
}
return result.join(" ");
}
Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').
Examples:
solution('abc') // should return ['ab', 'c_']
solution('abcdef') // should return ['ab', 'cd', 'ef']
function solution(str){
var result = [];
while(str.length >1){
result.push(str.substring(0,2))
str = str.substring(2,str.length);
}
if(str.length !== 0)
result.push(str+"_")
return result;
}
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');
}