aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2023-09-13 00:11:32 -0400
committerPacky Anderson <packy@cpan.org>2023-09-13 00:11:32 -0400
commit2390cb3375065c3ecf8ac1d3a8321337979144a1 (patch)
tree6bc9036ed0cd274a2a5d41a9f28bee867167aaa5
parentf14045cef30c4f226d9e8dab79005d63dca7515a (diff)
downloadperlweeklychallenge-club-2390cb3375065c3ecf8ac1d3a8321337979144a1.tar.gz
perlweeklychallenge-club-2390cb3375065c3ecf8ac1d3a8321337979144a1.tar.bz2
perlweeklychallenge-club-2390cb3375065c3ecf8ac1d3a8321337979144a1.zip
Add challenge 234 solutions in Java
-rw-r--r--challenge-234/packy-anderson/java/Ch1.java81
-rw-r--r--challenge-234/packy-anderson/java/Ch2.java48
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-234/packy-anderson/java/Ch1.java b/challenge-234/packy-anderson/java/Ch1.java
new file mode 100644
index 0000000000..70bb98fa43
--- /dev/null
+++ b/challenge-234/packy-anderson/java/Ch1.java
@@ -0,0 +1,81 @@
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
+// then press Enter. You can now see whitespace characters in your code.
+public class Ch1 {
+ public static HashMap<Character, Integer> charFrequency(String word) {
+ HashMap<Character, Integer> freq = new HashMap<Character, Integer>();
+ for (int i=0; i < word.length(); i++) {
+ char c = word.charAt(i);
+ freq.put(c, freq.getOrDefault(c,0)+1);
+ }
+ return freq;
+ }
+
+ public static ArrayList<String> commonCharacters(String[] words) {
+ // get the character frequencies for each word in words
+ ArrayList<HashMap<Character, Integer>> freq =
+ new ArrayList<HashMap<Character, Integer>>();
+ for (String word : words) {
+ freq.add(charFrequency(word));
+ }
+ // grab the character frequency map for the first word
+ HashMap<Character, Integer> first = freq.remove(0);
+ // now check the characters in the first word against
+ // the characters in all the subsequent words
+ Set<Character> keys = new HashSet<>(first.keySet());
+ for (Character c : keys) {
+ for (HashMap<Character, Integer> subsequent : freq) {
+ if (! subsequent.containsKey(c)) {
+ // this character isn't in subsequent words,
+ // so let's remove it from the frequency map
+ // of the first word
+ first.remove(c);
+ }
+ else {
+ // the character IS in subsequent words,
+ // so let's set the frequency count to be
+ // the minimum count found in those words
+ first.put(c, Math.min(first.get(c), subsequent.get(c)));
+ }
+ }
+ }
+ // now we generate a list of characters in the order they
+ // appear in the first word
+ ArrayList<String> output = new ArrayList<String>();
+ // once again, loop over the characters in the first word
+ for (int i=0; i < words[0].length(); i++) {
+ Character c = words[0].charAt(i);
+ if (first.containsKey(c)) {
+ if (first.get(c) > 1) {
+ first.put(c, first.get(c) - 1);
+ }
+ else {
+ first.remove(c);
+ }
+ output.add("" + c);
+ }
+ }
+ return output;
+ }
+
+ public static void solution(String[] words) {
+ System.out.println("Input: @words = (\"" + String.join("\", \"", words) + "\")");
+ ArrayList<String> output = commonCharacters(words);
+ System.out.println("Output: (\"" + String.join("\", \"", output) + "\")");
+ }
+
+ public static void main(String[] args) {
+ System.out.println("Example 1:");
+ solution(new String[] {"java", "javascript", "julia"});
+
+ System.out.println("\nExample 2:");
+ solution(new String[] {"bella", "label", "roller"});
+
+ System.out.println("\nExample 3:");
+ solution(new String[] {"cool", "lock", "cook"});
+ }
+} \ No newline at end of file
diff --git a/challenge-234/packy-anderson/java/Ch2.java b/challenge-234/packy-anderson/java/Ch2.java
new file mode 100644
index 0000000000..ec53d291f7
--- /dev/null
+++ b/challenge-234/packy-anderson/java/Ch2.java
@@ -0,0 +1,48 @@
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.stream.Collectors;
+
+public class Ch2 {
+ public static ArrayList<int[]> findTriplets(int[] ints) {
+ ArrayList<int[]> solutions = new ArrayList<int[]>();
+ for (int i = 0; i < ints.length - 2; i++) {
+ for (int j = i + 1; j < ints.length - 1; j++) {
+ for (int k = j + 1; k < ints.length; k++) {
+ if (ints[i] != ints[j] &&
+ ints[j] != ints[k] &&
+ ints[i] != ints[k]) {
+ int triplet[] = {i, j, k};
+ solutions.add( triplet );
+ }
+ }
+ }
+ }
+ return solutions;
+ }
+
+ public static void solution(int[] ints) {
+ String joined = Arrays.stream(ints)
+ .mapToObj(String::valueOf)
+ .collect(Collectors.joining(", "));
+ System.out.println("Input: @ints = (" + joined + ")");
+ ArrayList<int[]> solutions = findTriplets(ints);
+ System.out.println(String.format("Output: %1$d", solutions.size()));
+ for (int[] solution : solutions) {
+ System.out.println(String.format(
+ "(%1$d, %2$d, %3$d) because %4$d != %5$d != %6$d",
+ solution[0], solution[1], solution[2],
+ ints[solution[0]], ints[solution[1]], ints[solution[2]]
+ ));
+ }
+ }
+ public static void main(String[] args) {
+ System.out.println("Example 1:");
+ solution(new int[] {4, 4, 2, 4, 3});
+
+ System.out.println("\nExample 2:");
+ solution(new int[] {1, 1, 1, 1, 1});
+
+ System.out.println("\nExample 3:");
+ solution(new int[] {4, 7, 1, 10, 7, 4, 1, 1});
+ }
+} \ No newline at end of file