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;
}
}
Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros.
팩토리얼을 구하고 0의 갯수를 세면... 타임 초과가 난다 이틀 연속 타임 초과
public static int zeros(int n) {
if(n==0) return 0;
BigInteger ten = new BigInteger("10");
BigInteger fact = Stream.iterate(BigInteger.ONE, x->x.add(BigInteger.ONE)).limit(n).reduce(BigInteger::multiply).get();
int trail = 0;
while(fact.divideAndRemainder(ten)[1] == BigInteger.ZERO) {
fact = fact.divide(ten);
trail++;
}
return trail;
}
시간 고려해서 한 풀이
public static int zeros(int n) {
int trail = 0;
int i = 1;
double temp = 0d;
do {
temp = n / Math.pow(5, i++);
trail += Math.floor(temp);
}while(temp > 0);
return trail;
}
The year is 1214. One night, Pope Innocent III awakens to find the the archangel Gabriel floating before him. Gabriel thunders to the pope:
Gather all of the learned men in Pisa, especially Leonardo Fibonacci. In order for the crusades in the holy lands to be successful, these men must calculate the millionth number in Fibonacci's recurrence. Fail to do this, and your armies will never reclaim the holy land. It is His will.
The angel then vanishes in an explosion of white light.
Pope Innocent III sits in his bed in awe. How much is a million? he thinks to himself. He never was very good at math.
He tries writing the number down, but because everyone in Europe is still using Roman numerals at this moment in history, he cannot represent this number. If he only knew about the invention of zero, it might make this sort of thing easier.
He decides to go back to bed. He consoles himself, The Lord would never challenge me thus; this must have been some deceit by the devil. A pretty horrendous nightmare, to be sure.
Pope Innocent III's armies would go on to conquer Constantinople (now Istanbul), but they would never reclaim the holy land as he desired.
In this kata you will have to calculate fib(n) where:
fib(0) := 0
fib(1) := 1
fin(n + 2) := fib(n + 1) + fib(n)
Write an algorithm that can handle n where 1000000 ≤ n ≤ 1500000.
Your algorithm must output the exact integer answer, to full precision. Also, it must correctly handle negative numbers as input.
HINT I: Can you rearrange the equation fib(n + 2) = fib(n + 1) + fib(n) to find fib(n) if you already know fin(n + 1) and fib(n + 2)? Use this to reason what value fib has to have for negative values.
HINT II: See http://mitpress.mit.edu/sicp/chapter1/node15.html
타임오버 나왔다. 대칭이용해서 음수 까지는 잘처리했는데 피보나치 구하는 방식자체가 틀린것 같다.
import java.math.BigInteger;
import java.util.stream.Stream;
public class Fibonacci {
public static BigInteger fib(BigInteger n) {
// ...
if(n.signum() == 0) {
return BigInteger.ZERO;
}
BigInteger result = Stream.iterate(new BigInteger []{ BigInteger.ONE, BigInteger.ONE }, p-> new BigInteger[]{ p[1], p[0].add(p[1]) }).limit(n.abs().longValue()).reduce((a, b) -> b).get()[0];
if(n.signum() == -1 && n.abs().mod(new BigInteger("2")).equals(BigInteger.ZERO)) return result.negate();
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();
}
}
Here's a seemingly simple challenge. We're giving you a class called bagel, exactly as it appears below. All it really does is return an int, specifically 3.
public class Bagel {
public final int getValue() {
return 3;
}
}
The catch? For the solution, we're testing that the result is equal to 4. But as a little hint, the solution to the this Kata is exactly the same as the example test cases.
통과조건...
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class BagelTest {
@Test
public void testBagel() {
Bagel bagel = BagelSolver.getBagel();
assertEquals(
bagel.getValue() == 4,
java.lang.Boolean.TRUE
);
}
}
...별짓을 다했다 리플랙션 , 프록시, bci
설마이걸까 했는데 -.- 으음 빡친다.
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class BagelSolver {
public static Bagel getBagel() {
try {
Field f = Boolean.class.getField("TRUE");
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(f, f.getModifiers() & ~Modifier.FINAL);
f.set(null, false);
} catch (Exception e) {
// TODO: handle exception
}
return new Bagel();
}
}
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.