Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
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();
}
}
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;
}
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);
}
}
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;
}
}
When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:
1, 10, 9, 12, 3, 4.
Then the whole pattern repeats.
Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.
From now on the sequence is stationary and the remainder of 1234567 by 13 is the same as the remainder of 87 by 13: 9
Call thirt the function which processes this sequence of operations on an integer n (>=0). thirt will return the stationary number.
thirt(1234567) calculates 178, then 87, then 87 and returns 87.
thirt(321) calculates 48, 48 and returns 48
class Thirteen {
static long [] patterns = new long [] {1, 10, 9, 12, 3, 4} ;
public static long thirt(long n) {
long curr = n;
long result = 0;
String [] temp = new StringBuilder(String.valueOf(n)).reverse().toString().split("");
for (int i = 0; i < temp.length; i++) {
result += Long.valueOf(temp[i]) * patterns[i%patterns.length];
}
return result == curr ? result : thirt(result);
}
}