Write a function to find if a number is lucky or not. If the sum of all digits is 0 or multiple of 9 then the number is lucky.
1892376 => 1+8+9+2+3+7+6 = 36. 36 is divisble by 9, hence number is lucky.
Function will return true for lucky numbers and false for others.
집에 너무 늦게 들어온데다가 이번주내내 잠을 못자서 너무 피곤한 관계로 낮은 레벨을 선택해서 품
어뷰징에 대해 고민이 조금 있긴 하지만, 한번 안하면 두번 안하는건 일도 아니기 때문이 쉬운문제라도 풀기로 한다.
import java.util.stream.Stream;
public class LuckyNumber {
public static boolean isLucky(long n) {
// is n lucky?
int result = Stream.of(String.valueOf(n).split("")).mapToInt(Integer::valueOf).sum();
return result % 9 == 0 || result == 0;
}
}
주어진 문제를 정직하게 풀었는데 가장 추천을 많이 받은 답변은 정말 간결하다. 쉬운문제도 많이 고민해볼 수 있도록해야 겠다.
public class LuckyNumber {
public static boolean isLucky(long n) {
return n % 9 == 0;
}
}
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;
}
}