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();
}
}
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.
Examples
[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11 (the only odd number)
[160, 3, 1719, 19, 11, 13, -21]
Should return: 160 (the only even number)
static int find(int[] integers) throws Exception{
List<Integer> even = new ArrayList<Integer>();
List<Integer> odd = new ArrayList<Integer>();
for (int i : integers) {
if(i%2==0) {
even.add(i);
}else {
odd.add(i);
};
};
return even.size() == 1 ? even.get(0) : odd.get(0);
}
보면서 감탄한 가장 많은 추천을 받은 풀이
public static int find(int[] integers) {
// Since we are warned the array may be very large, we should avoid counting values any more than we need to.
// We only need the first 3 integers to determine whether we are chasing odds or evens.
// So, take the first 3 integers and compute the value of Math.abs(i) % 2 on each of them.
// It will be 0 for even numbers and 1 for odd numbers.
// Now, add them. If sum is 0 or 1, then we are chasing odds. If sum is 2 or 3, then we are chasing evens.
int sum = Arrays.stream(integers).limit(3).map(i -> Math.abs(i) % 2).sum();
int mod = (sum == 0 || sum == 1) ? 1 : 0;
return Arrays.stream(integers).parallel() // call parallel to get as much bang for the buck on a "large" array
.filter(n -> Math.abs(n) % 2 == mod).findFirst().getAsInt();
}
Freddy has a really fat left pinky finger, and every time Freddy tries to type an A, he accidentally hits the CapsLock key!
Given a string that Freddy wants to type, emulate the keyboard misses where each A supposedly pressed is replaced with CapsLock, and return the string that Freddy actually types. It doesn't matter if the A in the string is capitalized or not. When CapsLock is enabled, capitalization is reversed, but punctuation is not affected.
Examples:
"The quick brown fox jumps over the lazy dog."
-> "The quick brown fox jumps over the lZY DOG."
"The end of the institution, maintenance, and administration of government, is to secure the existence of the body politic, to protect it, and to furnish the individuals who compose it with the power of enjoying in safety and tranquillity their natural rights, and the blessings of life: and whenever these great objects are not obtained, the people have a right to alter the government, and to take measures necessary for their safety, prosperity and happiness."
-> "The end of the institution, mINTENnce, ND dministrTION OF GOVERNMENT, IS TO SECURE THE EXISTENCE OF THE BODY POLITIC, TO PROTECT IT, nd to furnish the individuLS WHO COMPOSE IT WITH THE POWER OF ENJOYING IN Sfety ND TRnquillity their nTURl rights, ND THE BLESSINGS OF LIFE: nd whenever these greT OBJECTS re not obtINED, THE PEOPLE Hve RIGHT TO lter the government, ND TO Tke meSURES NECESSry for their sFETY, PROSPERITY nd hPPINESS."
"aAaaaaAaaaAAaAa"
-> ""
If the given string is null, return null.
If the given string is "", the answer should be evident.
Happy coding!
public class Kata {
public static String fatFingers(String str) {
if (str == null) return null;
boolean capsLock = false;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == 'A' || c == 'a') {
capsLock = capsLock ? false : true;
}
else if (capsLock) {
if (Character.isUpperCase(c))
chars[i] = Character.toLowerCase(c);
else if (Character.isLowerCase(c))
chars[i] = Character.toUpperCase(c);
}
}
String result = new String(chars);
result = result.replaceAll("[Aa]","");
return result;
}
}
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.
Examples:
// returns "theStealthWarrior"
toCamelCase("the-stealth-warrior")
// returns "TheStealthWarrior"
toCamelCase("The_Stealth_Warrior")
오늘은 그나마 빨리 풀었다. 5랑 4레벨 차이가 어마어마한듯 ㅠ
import java.lang.StringBuilder;
import java.util.StringTokenizer;
class Solution{
static String toCamelCase(String s){
StringTokenizer tokenizer = new StringTokenizer(s,"-|_|");
StringBuilder result = new StringBuilder();
int index = 0;
while(tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if(index ==0) {
result.append(token);
}else {
result.append(token.substring(0, 1).toUpperCase() + token.substring(1));
}
index++;
}
return result.toString();
}
}
Create a simple calculator that given a string of operators (+ - * and /) and numbers separated by spaces returns the value of that expression
Example:
Calculator.evaluate("2 / 2 + 3 * 4 - 6") // => 7
Remember about the order of operations! Multiplications and divisions have a higher priority and should be performed left-to-right. Additions and subtractions have a lower priority and should also be performed left-to-right.
A format for expressing an ordered list of integers is to use a comma separated list of either
individual integers
or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example ("12, 13, 15-17")
Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.
For example, Penny drinks the third can of cola and the queue will look like this:
Write a program that will return the name of the person who will drink the n-th cola.
Note that in the very beginning the queue looks like that:
Sheldon, Leonard, Penny, Rajesh, Howard
##Input
The input data consist of an array which contains at least 1 name, and single integer n.
(1 ≤ n ≤ 1000000000).
##Output / Examples Return the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.