diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-08 13:56:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-08 13:56:54 +0100 |
| commit | 02aa2cddcb560fe9843129749f849975f80c8c2d (patch) | |
| tree | 5e4ec2686185e31412cde9d5b8dd09cf367da6c7 /challenge-237/packy-anderson/java | |
| parent | 61e2a29a7e3c0100cbf1560dab538a9fb8e78a66 (diff) | |
| parent | 2b419446ab484d93efadd0271caa060960fe08c8 (diff) | |
| download | perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.tar.gz perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.tar.bz2 perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.zip | |
Merge pull request #8813 from packy/challenge-237
Challenge 237 solutions by Packy Anderson
Diffstat (limited to 'challenge-237/packy-anderson/java')
| -rw-r--r-- | challenge-237/packy-anderson/java/Ch1.java | 108 | ||||
| -rw-r--r-- | challenge-237/packy-anderson/java/Ch2.java | 121 |
2 files changed, 229 insertions, 0 deletions
diff --git a/challenge-237/packy-anderson/java/Ch1.java b/challenge-237/packy-anderson/java/Ch1.java new file mode 100644 index 0000000000..2a9ab8dc49 --- /dev/null +++ b/challenge-237/packy-anderson/java/Ch1.java @@ -0,0 +1,108 @@ +import java.util.Calendar; +import java.text.SimpleDateFormat; + +class seizeTheDay { + public int day; + public String description; + + public seizeTheDay( + int year, + int month, + int weekday_of_month, + int day_of_week + ) { + // object for the first day of the specified month + Calendar t = Calendar.getInstance(); + t.set(year, month - 1, 1); + + // find the FIRST instance of the desired day of the week + while (t.get(Calendar.DAY_OF_WEEK) != day_of_week+1) { + t.add(Calendar.DATE, 1); + } + + String month_str = + new SimpleDateFormat("MMM").format(t.getTime()); + String dow_str = + new SimpleDateFormat("EEE").format(t.getTime()); + int count = 1; + + // now roll forward through the month until the desired + // weekday of the month + while ( + // we're still in the desired month + t.get(Calendar.MONTH) == month - 1 + && + // we haven't reached the desired weekday of the month + count < weekday_of_month + ) { + t.add(Calendar.DATE, 7); + count++; + } + if (t.get(Calendar.MONTH) != month - 1) { + this.day = 0; + this.description = String.format( + "There isn't a %s %s in %s %d", + this.ord_suffix(weekday_of_month), + dow_str, + month_str, + year + ); + } + else { + this.day = t.get(Calendar.DATE); + this.description = String.format( + "The %s %s of %s %d is the %s", + this.ord_suffix(weekday_of_month), + dow_str, + month_str, + year, + this.ord_suffix(this.day) + ); + } + } + + private String ord_suffix(int num) { + // quick function to add an ordinal suffix + // to a number + if (num == 11 || num == 12 | num == 13) { + return num + "th"; + } + else { + switch (num % 10) { + case 1: return num + "st"; + case 2: return num + "nd"; + case 3: return num + "rd"; + default: return num + "th"; + } + } + } +} + +public class Ch1 { + public static void solution( + int year, + int month, + int weekday_of_month, + int day_of_week + ) { + String format = "Input: Year = %d, Month = %d, " + + "Weekday of month = %d, day of week = %d"; + System.out.println(String.format( + format, year, month, weekday_of_month, day_of_week + )); + seizeTheDay d = new seizeTheDay(year, month, weekday_of_month, day_of_week); + System.out.println(String.format("Output: %d", d.day)); + System.out.println(String.format("\n%s", d.description)); + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(2024, 4, 3, 2); + + System.out.println("\nExample 2:"); + solution(2025, 10, 2, 4); + + System.out.println("\nExample 3:"); + solution(2026, 8, 5, 3); + } +} diff --git a/challenge-237/packy-anderson/java/Ch2.java b/challenge-237/packy-anderson/java/Ch2.java new file mode 100644 index 0000000000..4768d8d7ec --- /dev/null +++ b/challenge-237/packy-anderson/java/Ch2.java @@ -0,0 +1,121 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +public class Ch2 { + public static String joined(int[] ints) { + // we're using it more than once, make it a method + return Arrays.stream(ints) + .mapToObj(String::valueOf) + .collect(Collectors.joining(", ")); + } + + public static int greatness(int[] nums, int[] perm) { + // determine the "greatness" of a permutation + // relative to the original array; accepts two + // arrays to do the comparison + int greatness_num = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < perm[i]) { + greatness_num++; + } + } + return greatness_num; + } + + public static int[] greatestPermutation(int[] nums) { + // first, count up how many of each num we have + HashMap<Integer, Integer> num_count = + new HashMap<Integer, Integer>(); + for (int i = 0; i < nums.length; i++) { + num_count.put( + nums[i], + num_count.getOrDefault(nums[i], 0) + 1 + ); + } + + // make a list of the available numbers + // to put in a permutation + List<Integer> available = + new ArrayList<>(num_count.keySet()); + Collections.sort(available); + + // now, build a permutation that maximizes "greatness" + List<Integer> perm = new ArrayList<>(); + for (Integer num : nums) { + // default to the smallest available number + int num_to_add = available.get(0); + for (int i = 0; i < available.size(); i++) { + int this_num = available.get(i); + if (num < this_num) { + num_to_add = this_num; + break; + } + } + perm.add(num_to_add); + + // decrement the count of that number available + num_count.put( + num_to_add, + num_count.get(num_to_add) - 1 + ); + + // if there are no more of that number, remove it + // from available list + if ( num_count.get(num_to_add) == 0 ) { + // filter array to not include $num + int size = available.size(); + for (int i = 1; i < size; i++) { + int this_num = available.get(i); + if (num_to_add == this_num) { + available.remove(i); + break; + } + } + } + } + + // because we built the permutations in a List, + // convert the list to an int array for return + int[] perm_return = new int[perm.size()]; + for (int i = 0; i < perm.size(); i++) { + perm_return[i] = perm.get(i); + } + return perm_return; + } + + public static void solution(int[] nums) { + System.out.println(String.format( + "Input: @nums = (%s)", joined(nums) + )); + int[] greatest = greatestPermutation(nums); + int greatness_num = greatness(nums, greatest); + System.out.println(String.format( + "Output: %d", greatness_num + )); + System.out.println(String.format( + "\nOne possible permutation: (%s)", joined(greatest) + )); + System.out.println(String.format( + "which returns %d greatness as below:", greatness_num + )); + for (int i = 0; i < nums.length; i++) { + if (nums[i] < greatest[i]) { + System.out.println(String.format( + "nums[%d] < perm[%d]", i, i + )); + } + } + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(new int[] {1, 3, 5, 2, 1, 3, 1}); + + System.out.println("\nExample 2:"); + solution(new int[] {1, 2, 3, 4}); + } +} |
