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
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CountingDuplicates {
public static int duplicateCount(String text) {
// Write your code here
return (int) text.toLowerCase().chars().mapToObj(x->(char) x)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream()
.filter(x -> x.getValue() >1).count();
}
public static int duplicateCount2(String text) {
// Write your code here
Set<Character> count = new HashSet<Character>();
text = text.toLowerCase();
for (char element : text.toCharArray()) {
if(text.indexOf(element) != text.lastIndexOf(element)) count.add(element);
}
return count.size();
}
}
Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?
Task
Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
A perfect power is a classification of positive integers:
In mathematics, a perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, n is a perfect power if there exist natural numbers m > 1, and k > 1 such that mk = n.
Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, NULL, None or your language's equivalent.
Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.
Examples
isPerfectPower(4) => new int[]{2,2}
isPerfectPower(5) => null
isPerfectPower(8) => new int[]{2,3}
isPerfectPower(9) => new int[]{3,2}
public class PerfectPower {
public static int[] isPerfectPower(int n) {
for (int i = 2; i < Math.log(n) / Math.log(2) + 1; i++) {
for (int j = 2; Math.pow(j, i) <= n; j++) {
if (Math.pow(j, i) == n) {
return new int[]{j, i};
}
}
}
return null;
}
}
A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code of 3, 4, 5 or more capitals letters. The first letter of a letter is the capital letter of the book category. In the bookseller's stocklist each code is followed by a space and a positive integer n (int n> = 0) which indicates the quantity of books in this code in stock.
For example an extract of one of the stocklists could be:
L = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"] or ....
You will be given a stocklist (L) and a list of categories in capital letters.
M = {"A", "B", "C", "W"}
or
M = ["A", "B", "C", "W"] or ...
and your task is to find all the books of L with codes belonging to each category.
For the lists L and M, you have to return the string (in Haskell / Clojure a list of pairs):
(A: 20) - (B: 114) - (C: 50) - (W: 0)
"BKWRK" and "BTSQZ", 50 corresponding to "CDXEF" and 0 to category 'W', where A, B, C and W are the categories, since there is no code beginning with W.
If L or M are empty return string is "" (Clojure should return an empty array instead).
import java.util.ArrayList;
import java.util.List;
public class StockList {
// 1st parameter is the stocklist (L in example),
// 2nd parameter is list of categories (M in example)
public static String stockSummary(String[] lstOfArt, String[] lstOf1stLetter) {
// your code here
if(lstOfArt.length== 0 || lstOf1stLetter.length == 0) return "";
List<String> result = new ArrayList<String>();
for (String letter : lstOf1stLetter) {
int temp = 0;
for (String art : lstOfArt) {
if(art.startsWith(letter)) temp += Integer.valueOf(art.split(" ")[1]);
}
result.add("("+letter+" : "+temp+")");
}
return String.join(" - ", result);
}
}
Given a string of words, you need to find the highest scoring word.
Each letter of a word scores points according to it's position in the alphabet: a = 1, b = 2, c = 3 etc.
You need to return the highest scoring word as a string.
If two words score the same, return the word that appears earliest in the original string.
All letters will be lowercase and all inputs will be valid.
import java.util.Arrays;
public class HighestScoringWord {
private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String high(String s) {
int high = Integer.MIN_VALUE;
String result = "";
for (String item : s.split(" ")) {
int sum = Arrays.stream(item.split("")).mapToInt(x -> ALPHABET.indexOf(x)+1).sum();
if(high < sum) {
high = sum;
result = item;
}
}
return result;
}
}
import java.util.*;
public class Kata {
public static String high(String s) {
return Arrays.stream(s.split(" "))
.max(Comparator.comparingInt(
a -> a.chars().map(i -> i - 96).sum()
)).get();
}
}
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
The function has two input variables:
customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
The function should return an integer, the total time required.
EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:
There is only ONE queue, and
The order of the queue NEVER changes, and
Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
The diagram on the wiki page I linked to at the bottom of the description may be useful.
So, for example:
queueTime([5,3,4], 1)
// should return 12
// because when n=1, the total time is just the sum of the times
queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the
// queue finish before the 1st person has finished.
queueTime([2,3,10], 2)
// should return 12
public static int solveSuperMarketQueue(int[] customers, int n) {
int [] result = new int [n];
for (int i : customers) {
Arrays.sort(result);
result[0] += i;
}
return Arrays.stream(result).max().getAsInt();
}
You will be given a number and you will need to return it as a string in Expanded Form. For example:
Kata.expandedForm(12); # Should return "10 + 2"
Kata.expandedForm(42); # Should return "40 + 2"
Kata.expandedForm(70304); # Should return "70000 + 300 + 4"
NOTE: All numbers will be whole numbers greater than 0.
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Kata
{
public static String expandedForm(int num){
List<String> result = new ArrayList<String>();
while(num!=0){
int length = String.valueOf(num).length();
int cutoff = new BigDecimal(num).setScale(-length+1, BigDecimal.ROUND_DOWN).intValue();
num -= cutoff;
result.add(String.valueOf(cutoff));
}
return result.stream().collect(Collectors.joining(" + "));
}
}
The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. An "Avengers" ticket costs 25 dollars.
Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.
Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
Return YES, if Vasya can sell a ticket to each person and give the change with the bills he has at hand at that moment. Otherwise return NO.
###Examples:
// *** Java ***
Line.Tickets(new int[] {25, 25, 50}) // => YES
Line.Tickets(new int []{25, 100})
// => NO. Vasya will not have enough money to give change to 100 dollars
public static String Tickets(int[] peopleInLine) {
//Your code is here...
int dollars25 = 0;
int dollars50 = 0;
for (int i : peopleInLine) {
if(i==25) {
dollars25++;
}else if(i==50) {
dollars25--;
dollars50++;
}else {
if(dollars50>0) {
dollars50--;
dollars25--;
}else {
dollars25 -=3;
}
}
if (dollars25<0 || dollars50<0) return "NO";
}
return "YES";
}
Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.
Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.
For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".
Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.
Input
The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters
Output
Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.