aboutsummaryrefslogtreecommitdiff
path: root/challenge-238/deadmarshal/java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-238/deadmarshal/java')
-rw-r--r--challenge-238/deadmarshal/java/Ch1.java20
-rw-r--r--challenge-238/deadmarshal/java/Ch2.java46
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-238/deadmarshal/java/Ch1.java b/challenge-238/deadmarshal/java/Ch1.java
new file mode 100644
index 0000000000..43b8f25e2b
--- /dev/null
+++ b/challenge-238/deadmarshal/java/Ch1.java
@@ -0,0 +1,20 @@
+public class Ch1 {
+ public static void main(String[] args) {
+ int[] arr1 = {1,2,3,4,5};
+ int[] arr2 = {1,1,1,1,1};
+ int[] arr3 = {0,-1,1,2};
+ running_sum(arr1);
+ running_sum(arr2);
+ running_sum(arr3);
+ }
+
+ private static void running_sum(int[] arr) {
+ int sum = 0;
+ for(var e : arr){
+ sum += e;
+ System.out.printf("%d ",sum);
+ }
+ System.out.println();
+ }
+}
+
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);
+ }});
+ }
+}
+