From a6233090f6700d2c4b34d30c9e50c6b113b722d2 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Thu, 12 Oct 2023 13:16:43 +0330 Subject: TWC238 --- challenge-238/deadmarshal/java/Ch1.java | 20 ++++++++++++++ challenge-238/deadmarshal/java/Ch2.java | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 challenge-238/deadmarshal/java/Ch1.java create mode 100644 challenge-238/deadmarshal/java/Ch2.java (limited to 'challenge-238/deadmarshal/java') 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 list1 = new ArrayList<>(List.of(15,99,1,34)); + ArrayList 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 list) { + list.sort(new Comparator() { + @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); + }}); + } +} + -- cgit