We need a reusable program to encode/decode text to unicode value as represented in Java and vice-versa.
Encoder:
Input
"hola"
Output
"\u0068\u006f\u006c\u0061"
Decoder:
Input
"\u0068\u006f\u006c\u0061"
Output
"hola"
Observations:
Please note that unicode values have always a back slash \ the letter u and 4 hexadecimal digits corresponding to the value in the unicode character set.
Codewars has some trouble showing non ASCII characters so perhaps you should try it locally first.
import java.io.UnsupportedEncodingException;
public class JavaUnicodeEncoder {
public static String decode(final String input) throws UnsupportedEncodingException {
String[] arr = input.replace("\\","").split("u");
StringBuilder result = new StringBuilder();
for(String str :arr){
if(str.length()!=0) {
int hex = Integer.parseInt(str, 16);
result.append((char)hex);
}
}
return result.toString();
}
public static String encode(String input){
StringBuilder builder = new StringBuilder();
for (char c : input.toCharArray()) {
builder.append("\\u" + Integer.toHexString(c | 0x10000).substring(1));
};
return builder.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.
This problem takes its name by arguably the most important event in the life of the ancient historian Josephus: according to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege.
Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist: they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act).
Well, Josephus and another man were the last two and, as we now know every detail of the story, you may have correctly guessed that they didn't exactly follow through the original idea.
You are now to create a function that returns a Josephus permutation, taking as parameters the initial array/list of items to be permuted as if they were in a circle and counted out every k places until none remained.
Tips and notes: it helps to start counting from 1 up to n, instead of the usual range 0..n-1; k will always be >=1.
For example, with n=7 and k=3 josephus(7,3) should act this way.
[1,2,3,4,5,6,7] - initial sequence
[1,2,4,5,6,7] => 3 is counted out and goes into the result [3]
[1,2,4,5,7] => 6 is counted out and goes into the result [3,6]
[1,4,5,7] => 2 is counted out and goes into the result [3,6,2]
[1,4,5] => 7 is counted out and goes into the result [3,6,2,7]
[1,4] => 5 is counted out and goes into the result [3,6,2,7,5]
[4] => 1 is counted out and goes into the result [3,6,2,7,5,1]
[] => 4 is counted out and goes into the result [3,6,2,7,5,1,4]
So our final result is: josephus([1,2,3,4,5,6,7],3)==[3,6,2,7,5,1,4]
public static <T> List<T> josephusPermutation(final List<T> items, final int k) {
int current = 0;
List<T> result= new ArrayList<T>();
while (items.size() > 0) {
current = (current-1+k)%items.size();
/*
current+=k-1;
while(current > = items.size()) {
current -= items.size();
}
*/
result.add(items.remove(current));
}
return result;
}
# Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!
The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this kata. What's the use of saying "Eureka"? Because this sum gives the same number.
In effect: 89 = 8^1 + 9^2
The next number in having this property is 135.
See this property again: 135 = 1^1 + 3^2 + 5^3
We need a function to collect these numbers, that may receive two integers a, b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above.