var moveZeros = function (arr) {
var result = [];
var zeros = [];
for(var i = 0 ; i < arr.length ; i ++){
if(arr[i] === 0)
zeros.push(arr[i]);
else
result.push(arr[i]);
}
return result.concat(zeros);
}
My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.
I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?
When two numbers have the same "weight", let us class them as if they were strings and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9) it comes before as a string.
All numbers in the list are positive numbers and the list can be empty.
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class WeightSort {
public static int getWeight(String strng) {
return Arrays.stream(strng.split("")).mapToInt(Integer::parseInt).sum();
}
public static String orderWeight(String strng) {
return Arrays.stream(strng.split(" "))
.sorted(Comparator.comparing(WeightSort::getWeight).thenComparing(String::compareTo))
.collect(Collectors.joining(" "));
}
}
When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said
Description:
Give you two arrays arr1 and arr2. They have the same length(length>=2). The elements of two arrays always be integer.
Sort arr1 according to the ascending order of arr2; Sort arr2 according to the ascending order of arr1. Description is not easy to understand, for example:
arr1=[5,4,3,2,1],
arr2=[6,5,7,8,9]
Let us try to sorting arr1. First, we need know the ascending order of arr2:
[5,6,7,8,9]
We can see, after sort arr2 to ascending order, some elements' index are changed:
unsort arr2 ascending order arr2
[6,5,7,8,9]---> [5,6,7,8,9]
index0(6) ---> index1
index1(5) ---> index0
index2(7) index2(no change)
index3(8) index3(no change)
index4(9) index4(no change)
So, wo need according to these changes to sorting arr1:
unsort arr1 sorted arr1
[5,4,3,2,1]---> [4,5,3,2,1]
index0(5) ---> index1
index1(4) ---> index0
index2(3) index2(no change)
index3(2) index3(no change)
index4(1) index4(no change)
So: sorted arr1= [4,5,3,2,1]
And then, we sorting arr2 with the same process:
unsort arr1 ascending order arr1
[5,4,3,2,1]---> [1,2,3,4,5]
index0(5) ---> index4
index1(4) ---> index3
index2(3) index2(no change)
index3(2) ---> index1
index4(1) ---> index0
unsort arr2 sorted arr2
[6,5,7,8,9]---> [9,8,7,5,6]
index0(6) ---> index4
index1(5) ---> index3
index2(7) index2(no change)
index3(8) ---> index1
index4(9) ---> index0
So: sorted arr2= [9,8,7,5,6]
Finally, returns the sorted arrays by a 2D array: [sorted arr1, sorted arr2]
Note: In ascending order sorting process(not the final sort), if some elements have same value, sort them according to their index; You can modify the original array, but I advise you not to do that. ;-)
Some Examples and explain
sortArrays([5,4,3,2,1],[6,5,7,8,9]) should return
[[4,5,3,2,1],[9,8,7,5,6]]
sortArrays([2,1,3,4,5],[5,6,7,8,9]) should return
[[2,1,3,4,5],[6,5,7,8,9]]
sortArrays([5,6,9,2,6,5],[3,6,7,4,8,1]) should return
[[5,5,2,6,9,6],[4,3,1,6,8,7]]
import java.util.Comparator;
import java.util.stream.IntStream;
public class ArraySorter {
private static int [] sort(int [] target, int [] sortby ) {
return IntStream.range(0, target.length).boxed()
.sorted(Comparator.comparing(i->sortby[i]))
.mapToInt(i->target[i]).toArray();
}
public static int[][] sortTwoArrays(int[] arr1, int[] arr2) {
return new int [][] {sort(arr1,arr2),sort(arr2,arr1)};
}
}
Here is a function that takes your string, concatenates the even-indexed chars to the front, odd-indexed chars to the back.
Examples
s = "Wow Example!"
result = "WwEapeo xml!"
s = "I'm JomoPipi"
result = "ImJm ii' ooPp"
The Task:
return the result of the string after applying the function to it n times.
example where s = "qwertyuio" and n = 2:
after 1 iteration s = "qetuowryi"
after 2 iterations s = "qtorieuwy"
return "qtorieuwy"
Note
There are 50000 random tests where
the string contains between 50 and 200 chars
n is greater than a billion
so be ready to optimize.
public class JomoPipi {
public static String sort(String s) {
StringBuilder odd = new StringBuilder();
StringBuilder even = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (i % 2 == 0) {
odd.append(s.charAt(i));
} else {
even.append(s.charAt(i));
}
}
return odd.append(even.toString()).toString();
}
public static String jumbledString(String s, long n) {
String origin = s;
int cycle = 1;
for (int i = 0; i < n; i++) {
s = sort(s);
if (s.equals(origin)) {
cycle = i;
break;
}
}
n %= cycle;
if (n == 0)
return s;
n += 1;
for (int i = 0; i < n; i++) {
origin = sort(origin);
}
return origin;
}
}
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n, p will always be given as strictly positive integers.
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
import java.util.stream.IntStream;
public class DigPow {
public static long digPow(int n, int p) {
// your code
int input = n;
int result = 0;
p += String.valueOf(n).length()-1;
while (n != 0) {
result += Math.pow((n % 10), p--);
n /= 10;
}
if(result%input != 0)
return -1;
return result/input;
}
public static long digPow2(int n, int p) {
// your code
int[] digits = String.valueOf(n).chars().map(Character::getNumericValue).toArray();
int sum = IntStream.range(0, digits.length).map(i -> (int) Math.pow(digits[i], i + p)).sum();
return sum%n != 0 ? -1 : sum/n;
}
}
When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:
1, 10, 9, 12, 3, 4.
Then the whole pattern repeats.
Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.
From now on the sequence is stationary and the remainder of 1234567 by 13 is the same as the remainder of 87 by 13: 9
Call thirt the function which processes this sequence of operations on an integer n (>=0). thirt will return the stationary number.
thirt(1234567) calculates 178, then 87, then 87 and returns 87.
thirt(321) calculates 48, 48 and returns 48
class Thirteen {
static long [] patterns = new long [] {1, 10, 9, 12, 3, 4} ;
public static long thirt(long n) {
long curr = n;
long result = 0;
String [] temp = new StringBuilder(String.valueOf(n)).reverse().toString().split("");
for (int i = 0; i < temp.length; i++) {
result += Long.valueOf(temp[i]) * patterns[i%patterns.length];
}
return result == curr ? result : thirt(result);
}
}
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CountingDuplicates {
public static int duplicateCount(String text) {
// Write your code here
return (int) text.toLowerCase().chars().mapToObj(x->(char) x)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream()
.filter(x -> x.getValue() >1).count();
}
public static int duplicateCount2(String text) {
// Write your code here
Set<Character> count = new HashSet<Character>();
text = text.toLowerCase();
for (char element : text.toCharArray()) {
if(text.indexOf(element) != text.lastIndexOf(element)) count.add(element);
}
return count.size();
}
}
Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?
Task
Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code of 3, 4, 5 or more capitals letters. The first letter of a letter is the capital letter of the book category. In the bookseller's stocklist each code is followed by a space and a positive integer n (int n> = 0) which indicates the quantity of books in this code in stock.
For example an extract of one of the stocklists could be:
L = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"] or ....
You will be given a stocklist (L) and a list of categories in capital letters.
M = {"A", "B", "C", "W"}
or
M = ["A", "B", "C", "W"] or ...
and your task is to find all the books of L with codes belonging to each category.
For the lists L and M, you have to return the string (in Haskell / Clojure a list of pairs):
(A: 20) - (B: 114) - (C: 50) - (W: 0)
"BKWRK" and "BTSQZ", 50 corresponding to "CDXEF" and 0 to category 'W', where A, B, C and W are the categories, since there is no code beginning with W.
If L or M are empty return string is "" (Clojure should return an empty array instead).
import java.util.ArrayList;
import java.util.List;
public class StockList {
// 1st parameter is the stocklist (L in example),
// 2nd parameter is list of categories (M in example)
public static String stockSummary(String[] lstOfArt, String[] lstOf1stLetter) {
// your code here
if(lstOfArt.length== 0 || lstOf1stLetter.length == 0) return "";
List<String> result = new ArrayList<String>();
for (String letter : lstOf1stLetter) {
int temp = 0;
for (String art : lstOfArt) {
if(art.startsWith(letter)) temp += Integer.valueOf(art.split(" ")[1]);
}
result.add("("+letter+" : "+temp+")");
}
return String.join(" - ", result);
}
}