aboutsummaryrefslogtreecommitdiff
path: root/challenge-238/deadmarshal/java/Ch2.java
diff options
context:
space:
mode:
authordeadmarshal <adeadmarshal@gmail.com>2023-10-12 13:16:43 +0330
committerdeadmarshal <adeadmarshal@gmail.com>2023-10-12 13:16:43 +0330
commita6233090f6700d2c4b34d30c9e50c6b113b722d2 (patch)
tree9b0578ac04be9e99bb41354c2f715dac218873a0 /challenge-238/deadmarshal/java/Ch2.java
parent3143f9657ea324e7588d575d67c35eb28bc276f3 (diff)
downloadperlweeklychallenge-club-a6233090f6700d2c4b34d30c9e50c6b113b722d2.tar.gz
perlweeklychallenge-club-a6233090f6700d2c4b34d30c9e50c6b113b722d2.tar.bz2
perlweeklychallenge-club-a6233090f6700d2c4b34d30c9e50c6b113b722d2.zip
TWC238
Diffstat (limited to 'challenge-238/deadmarshal/java/Ch2.java')
-rw-r--r--challenge-238/deadmarshal/java/Ch2.java46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-238/deadmarshal/java/Ch2.java b/challenge-238/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..0a71d431a4
--- /dev/null
+++ b/challenge-238/deadmarshal/java/Ch2.java
@@ -0,0 +1,46 @@
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+public class Ch2 {
+ public static void main(String[] args) {
+ ArrayList<Integer> list1 = new ArrayList<>(List.of(15,99,1,34));
+ ArrayList<Integer> list2 = new ArrayList<>(List.of(50,25,33,22));
+ persistence_sort(list1);
+ persistence_sort(list2);
+ System.out.println(list1);
+ System.out.println(list2);
+ }
+
+ private static int product(int n) {
+ int prod = 1;
+ while(n > 0)
+ {
+ prod *= n % 10;
+ n /= 10;
+ }
+ return prod;
+ }
+
+ private static int helper(int n) {
+ int sum = 0;
+ while(n >= 10)
+ {
+ sum++;
+ n = product(n);
+ }
+ return sum;
+ }
+
+ private static void persistence_sort(List<Integer> list) {
+ list.sort(new Comparator<Integer>() {
+ @Override
+ public int compare(Integer a, Integer b) {
+ int ha = helper(a);
+ int hb = helper(b);
+ return ha == hb ? Integer.compare(a,b) :
+ Integer.compare(ha,hb);
+ }});
+ }
+}
+