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.
A common exercise, when you're learning a new language, is to make a guessing game. It's a great way to learn control structures, IO, the works.
This is taking the guessing game to a whole new level. This time, you're the one playing the guessing game. And the guessing game is Math.random().
The task is really simple. You make a guess, Math.random() does it's thing, and if you're right 5 times out of 5, you win!
Hint: You guess first.
import java.lang.reflect.*;
import java.util.Random;
public class Psychic {
public static double guess() {
try {
Field field = Class.forName("java.lang.Math$RandomNumberGeneratorHolder")
.getDeclaredField("randomNumberGenerator");
field.setAccessible(true);
Random random = (Random) field.get(null);
random.setSeed(0);
} catch (Exception e) {
}
return new Random(0).nextDouble();
}
}
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
Courtesy of ProjectEuler.net
import java.util.stream.IntStream;
public class Solution {
public int solution(int number) {
//TODO: Code stuff here
return IntStream.range(1, number).filter(x-> x%3==0 || x%5==0 ).sum();
}
}
Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it's invalid.
This Kata is similar to the Valid Parentheses Kata, but introduces new characters: brackets [], and curly braces {}. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.
What is considered Valid?
A string of braces is considered valid if all braces are matched with the correct brace.
Examples
"(){}[]" => True
"([{}])" => True
"(}" => False
"[(])" => False
"[({})](]" => False
import java.util.Stack;
public class BraceChecker {
public boolean isPair(String a, String b) {
return a.equals("[") && b.equals("]") || a.equals("{") && b.equals("}") || a.equals("(") && b.equals(")");
}
public boolean isValid(String braces) {
// Add code here
String [] items = braces.split("");
Stack>String< stack = new Stack>String<();
for (String item : items) {
if(item.equals("[")||item.equals("{")||item.equals("(")) {
stack.push(item);
}else {
if(stack.isEmpty() || !isPair(stack.pop(),item))
return false;
}
}
return stack.isEmpty();
}
}
We are tracking down our rogue agent Matthew Knight A.K.A. Roy Miller and he travels from places to places to avoid being tracked. Each of his travels are based on a list of itineraries in an unusual or incorrect order. The task is to determine the routes he will take in his every journey. You are given an array of routes of his itineraries. List down only the places where he will go in correct order based on his itineraries.
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;
}
}