diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-20 14:32:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-20 14:32:04 +0100 |
| commit | ab95c3676616e5cbb5dc76d08b01f6e6e0b00208 (patch) | |
| tree | aefdaffbf21aca96b922adb8b05155dda000ba48 | |
| parent | cb623b63ed56eda731e6c042e0863f01b4f56a14 (diff) | |
| parent | 161fd262dd71d153e0bf6c7997677bca1b82b62b (diff) | |
| download | perlweeklychallenge-club-ab95c3676616e5cbb5dc76d08b01f6e6e0b00208.tar.gz perlweeklychallenge-club-ab95c3676616e5cbb5dc76d08b01f6e6e0b00208.tar.bz2 perlweeklychallenge-club-ab95c3676616e5cbb5dc76d08b01f6e6e0b00208.zip | |
Merge pull request #8732 from packy/master
Challenge 235 solutions by Packy Anderson
| -rw-r--r-- | challenge-235/packy-anderson/README.md | 124 | ||||
| -rw-r--r-- | challenge-235/packy-anderson/java/Ch1.java | 86 | ||||
| -rw-r--r-- | challenge-235/packy-anderson/java/Ch2.java | 42 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/perl/ch-1.pl | 57 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/python/ch-1.py | 48 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/raku/ch-1.raku | 52 | ||||
| -rwxr-xr-x | challenge-235/packy-anderson/raku/ch-2.raku | 29 |
9 files changed, 407 insertions, 91 deletions
diff --git a/challenge-235/packy-anderson/README.md b/challenge-235/packy-anderson/README.md index 73332e5ada..f957013c00 100644 --- a/challenge-235/packy-anderson/README.md +++ b/challenge-235/packy-anderson/README.md @@ -8,16 +8,20 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @words = ("java", "javascript", "julia") -Output: ("j", "a") +Input: @ints = (0, 2, 9, 4, 6) +Output: true Example 2: -Input: @words = ("bella", "label", "roller") -Output: ("e", "l", "l") +Input: @ints = (5, 1, 3, 2) +Output: false Example 3: -Input: @words = ("cool", "lock", "cook") -Output: ("c", "o") +Input: @ints = (2, 2, 3) +Output: true + +Example 4 from James Curtis-Smith: +Input: @ints = (1, 2, 3, 4, 1, 2, 3) +Output: false ``` * [Task 2](perl/ch-2.pl) @@ -26,49 +30,16 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: @ints = (4, 4, 2, 4, 3) -Output: 3 - -(0, 2, 4) because 4 != 2 != 3 -(1, 2, 4) because 4 != 2 != 3 -(2, 3, 4) because 2 != 4 != 3 +Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +Output: (1, 0, 0, 2, 3, 0, 0, 4) Example 2: -Input: @ints = (1, 1, 1, 1, 1) -Output: 0 +Input: @ints = (1, 2, 3) +Output: (1, 2, 3) Example 3: -Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) -Output: 28 - -(0, 1, 2) because 4 != 7 != 1 -(0, 1, 3) because 4 != 7 != 10 -(0, 1, 6) because 4 != 7 != 1 -(0, 1, 7) because 4 != 7 != 1 -(0, 2, 3) because 4 != 1 != 10 -(0, 2, 4) because 4 != 1 != 7 -(0, 3, 4) because 4 != 10 != 7 -(0, 3, 6) because 4 != 10 != 1 -(0, 3, 7) because 4 != 10 != 1 -(0, 4, 6) because 4 != 7 != 1 -(0, 4, 7) because 4 != 7 != 1 -(1, 2, 3) because 7 != 1 != 10 -(1, 2, 5) because 7 != 1 != 4 -(1, 3, 5) because 7 != 10 != 4 -(1, 3, 6) because 7 != 10 != 1 -(1, 3, 7) because 7 != 10 != 1 -(1, 5, 6) because 7 != 4 != 1 -(1, 5, 7) because 7 != 4 != 1 -(2, 3, 4) because 1 != 10 != 7 -(2, 3, 5) because 1 != 10 != 4 -(2, 4, 5) because 1 != 7 != 4 -(3, 4, 5) because 10 != 7 != 4 -(3, 4, 6) because 10 != 7 != 1 -(3, 4, 7) because 10 != 7 != 1 -(3, 5, 6) because 10 != 4 != 1 -(3, 5, 7) because 10 != 4 != 1 -(4, 5, 6) because 7 != 4 != 1 -(4, 5, 7) because 7 != 4 != 1 +Input: @ints = (0, 3, 0, 4, 5) +Output: (0, 0, 3, 0, 0) ``` ## Raku @@ -79,16 +50,20 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @words = ("java", "javascript", "julia") -Output: ("j", "a") +Input: @ints = (0, 2, 9, 4, 6) +Output: true Example 2: -Input: @words = ("bella", "label", "roller") -Output: ("e", "l", "l") +Input: @ints = (5, 1, 3, 2) +Output: false Example 3: -Input: @words = ("cool", "lock", "cook") -Output: ("c", "o") +Input: @ints = (2, 2, 3) +Output: true + +Example 4 from James Curtis-Smith: +Input: @ints = (1, 2, 3, 4, 1, 2, 3) +Output: false ``` * [Task 2](raku/ch-2.raku) @@ -97,49 +72,16 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: @ints = (4, 4, 2, 4, 3) -Output: 3 - -(0, 2, 4) because 4 != 2 != 3 -(1, 2, 4) because 4 != 2 != 3 -(2, 3, 4) because 2 != 4 != 3 +Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +Output: (1, 0, 0, 2, 3, 0, 0, 4) Example 2: -Input: @ints = (1, 1, 1, 1, 1) -Output: 0 +Input: @ints = (1, 2, 3) +Output: (1, 2, 3) Example 3: -Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) -Output: 28 - -(0, 1, 2) because 4 != 7 != 1 -(0, 1, 3) because 4 != 7 != 10 -(0, 1, 6) because 4 != 7 != 1 -(0, 1, 7) because 4 != 7 != 1 -(0, 2, 3) because 4 != 1 != 10 -(0, 2, 4) because 4 != 1 != 7 -(0, 3, 4) because 4 != 10 != 7 -(0, 3, 6) because 4 != 10 != 1 -(0, 3, 7) because 4 != 10 != 1 -(0, 4, 6) because 4 != 7 != 1 -(0, 4, 7) because 4 != 7 != 1 -(1, 2, 3) because 7 != 1 != 10 -(1, 2, 5) because 7 != 1 != 4 -(1, 3, 5) because 7 != 10 != 4 -(1, 3, 6) because 7 != 10 != 1 -(1, 3, 7) because 7 != 10 != 1 -(1, 5, 6) because 7 != 4 != 1 -(1, 5, 7) because 7 != 4 != 1 -(2, 3, 4) because 1 != 10 != 7 -(2, 3, 5) because 1 != 10 != 4 -(2, 4, 5) because 1 != 7 != 4 -(3, 4, 5) because 10 != 7 != 4 -(3, 4, 6) because 10 != 7 != 1 -(3, 4, 7) because 10 != 7 != 1 -(3, 5, 6) because 10 != 4 != 1 -(3, 5, 7) because 10 != 4 != 1 -(4, 5, 6) because 7 != 4 != 1 -(4, 5, 7) because 7 != 4 != 1 +Input: @ints = (0, 3, 0, 4, 5) +Output: (0, 0, 3, 0, 0) ``` ## Guest Language: Python @@ -152,4 +94,4 @@ Output: 28 ## Blog Post -[Perl Weekly Challenge: Common, but Unequal, Triplet Characters](https://packy.dardan.com/2023/09/12/perl-weekly-challenge-common-but-unequal-triplet-characters/) +[Perl Weekly Challenge: Remove and Duplicate, Challenge Edition](https://packy.dardan.com/2023/09/18/perl-weekly-challenge-remove-and-duplicate-challenge-edition/) diff --git a/challenge-235/packy-anderson/java/Ch1.java b/challenge-235/packy-anderson/java/Ch1.java new file mode 100644 index 0000000000..a0d05407d9 --- /dev/null +++ b/challenge-235/packy-anderson/java/Ch1.java @@ -0,0 +1,86 @@ +import java.util.Arrays; +import java.util.stream.Collectors; + +public class Ch1 { + 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 boolean isStrictlyIncreasing(int[] ints) { + // get the first integer from the list + int last_int = ints[0]; + + // note that we start with element 1, because + // we've already put the value of the 0th + // element into last_int + for (int i = 1; i < ints.length; i++) { + if (ints[i] <= last_int) { + return false; + } + last_int = ints[i]; + } + return true; + } + + public static boolean isStrictlyIncreasingExceptOne(int[] ints) { + // the list cannot be strictly increasing unless + // there are at least two items in it + if (ints.length <= 1) { + return false; + } + + // if it's strictly increasing without removing + // an item, it fails the test + if (isStrictlyIncreasing(ints)) { + return false; + } + + for (int i = 1; i < ints.length; i++) { + if (ints[i] <= ints[i-1]) { + // make a new list to hold the list + // with one value removed + int[] newlist = new int[ints.length - 1]; + // copy over all but the (i-1)th element + for (int j = 0; j < ints.length; j++) { + if (j == i - 1) { + continue; + } + if (j < i - 1) { + newlist[j] = ints[j]; + } + else { + newlist[j-1] = ints[j]; + } + } + // now test this new list to see + // if it's strictly increasing + return isStrictlyIncreasing(newlist); + } + } + return false; + } + + + public static void solution(int[] ints) { + System.out.println("Input: @ints = (" + joined(ints) + ")"); + boolean output = isStrictlyIncreasingExceptOne(ints); + System.out.println("Output: " + output); + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(new int[] {0, 2, 9, 4, 6}); + + System.out.println("\nExample 2:"); + solution(new int[] {5, 1, 3, 2}); + + System.out.println("\nExample 3:"); + solution(new int[] {2, 2, 3}); + + System.out.println("\nExample 4 from James Curtis-Smith:"); + solution(new int[] {1,2,3,4,1,2,3}); + } +}
\ No newline at end of file diff --git a/challenge-235/packy-anderson/java/Ch2.java b/challenge-235/packy-anderson/java/Ch2.java new file mode 100644 index 0000000000..07611d47a5 --- /dev/null +++ b/challenge-235/packy-anderson/java/Ch2.java @@ -0,0 +1,42 @@ +import java.util.Arrays; +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[] duplicateZeros(int[] ints) { + for (int i = 0; i < ints.length; i++) { + if (ints[i] == 0) { + // shift all the values in the array to the right by one + for (int j = ints.length - 1; j > i; j--) { + ints[j] = ints[j - 1]; + } + ints[i + 1] = 0; // insert a new 0 + i++; // skip over the 0 we added! + } + } + return ints; + } + + public static void solution(int[] ints) { + System.out.println("Input: @ints = (" + joined(ints) + ")"); + ints = duplicateZeros(ints); + System.out.println("Output: (" + joined(ints) + ")"); + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(new int[] {1, 0, 2, 3, 0, 4, 5, 0}); + + System.out.println("\nExample 2:"); + solution(new int[] {1, 2, 3}); + + System.out.println("\nExample 3:"); + solution(new int[] {0, 3, 0, 4, 5}); + } +} diff --git a/challenge-235/packy-anderson/perl/ch-1.pl b/challenge-235/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..85b2f69e1a --- /dev/null +++ b/challenge-235/packy-anderson/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +use v5.38; + +sub isStrictlyIncreasing { + my @ints = @_; + # get the first integer from the list + my $last_int = shift @ints; + # test to make sure each subsequent int is greater + foreach my $this_int ( @ints ) { + return 0 if $this_int <= $last_int; + $last_int = $this_int; + } + return 1; +} + +sub isStrictlyIncreasingExceptOne { + my @ints = @_; + + # the list cannot be strictly increasing unless + # there are at least two items in it + return 0 if @ints <= 1; + + # if it's strictly increasing without removing + # an item, it fails the test + return 0 if isStrictlyIncreasing(@ints); + + # loop over the list by index + for (my $i = 1; $i <= $#ints; $i++) { + if ($ints[$i] <= $ints[$i - 1]) { + # remove the bigger item from the list + splice(@ints, $i-1, 1); + # after removing the element, is + # the list strictly increasing? + return isStrictlyIncreasing(@ints); + } + } +} + +sub solution { + my @ints = @_; + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my $output = isStrictlyIncreasingExceptOne(@ints); + say 'Output: ' . ($output ? 'true' : 'false'); +} + +say "Example 1:"; +solution(0, 2, 9, 4, 6); + +say "\nExample 2:"; +solution(5, 1, 3, 2); + +say "\nExample 3:"; +solution(2, 2, 3); + +say "\nExample 4 from James Curtis-Smith:"; +solution(1,2,3,4,1,2,3);
\ No newline at end of file diff --git a/challenge-235/packy-anderson/perl/ch-2.pl b/challenge-235/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..376353d31f --- /dev/null +++ b/challenge-235/packy-anderson/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use v5.38; + +sub duplicateZeros { + my @ints = @_; + for (my $i = 0; $i < scalar(@ints); $i++) { + if ($ints[$i] == 0) { + splice(@ints, $i+1, 0, 0); # insert a 0 at $i+1 + splice(@ints, -1); # pop the last element off @ints + $i++; # skip over the 0 we added! + } + } + return @ints; +} + +sub solution { + my @ints = @_; + say 'Input: @ints = (' . join(', ', @ints) . ')'; + @ints = duplicateZeros(@ints); + say 'Output: (' . join(', ', @ints) . ')'; +} + +say "Example 1:"; +solution(1, 0, 2, 3, 0, 4, 5, 0); + +say "\nExample 2:"; +solution(1, 2, 3); + +say "\nExample 3:"; +solution(0, 3, 0, 4, 5);
\ No newline at end of file diff --git a/challenge-235/packy-anderson/python/ch-1.py b/challenge-235/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..5152ea9092 --- /dev/null +++ b/challenge-235/packy-anderson/python/ch-1.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +def isStrictlyIncreasing(ints): + intlist = ", ".join([ str(i) for i in ints ]) + # get the first integer from the list + last_int = ints[0] + + for this_int in ints[1:]: + if this_int <= last_int: + return False + last_int = this_int + return True + +def isStrictlyIncreasingExceptOne(ints): + # the list cannot be strictly increasing unless + # there are at least two items in it + if len(ints) <= 1: + return False + + # if it's strictly increasing without removing + # an item, it fails the test + if isStrictlyIncreasing(ints): + return False + + length = len(ints) + for i in range(1, length-1): + if ints[i] <= ints[i - 1]: + print(f'removed {ints[i - 1]} at {i-1}') + ints.pop(i - 1) + return isStrictlyIncreasing(ints) + +def solution(ints): + intlist = ", ".join([ str(i) for i in ints ]) + print(f'Input: @ints = ({ intlist })') + output = isStrictlyIncreasingExceptOne(ints) + print(f'Output: {output}') + +print("Example 1:") +solution([0, 2, 9, 4, 6]) + +print("\nExample 2:") +solution([5, 1, 3, 2]) + +print("\nExample 3:") +solution([2, 2, 3]) + +print("\nExample 4 from James Curtis-Smith:") +solution([1,2,3,4,1,2,3])
\ No newline at end of file diff --git a/challenge-235/packy-anderson/python/ch-2.py b/challenge-235/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..fe63f8bea3 --- /dev/null +++ b/challenge-235/packy-anderson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def duplicateZeros(ints): + length = len(ints) + skip_me = -1 + for i in range(0, length): + if skip_me == i: + continue + if ints[i] == 0: + ints.insert(i+1, 0) # insert a 0 at i+1 + ints.pop(-1) # pop the last element off ints + skip_me = i+1 # skip over the 0 we added! + return ints + +def solution(ints): + intlist = ", ".join([ str(i) for i in ints ]) + print(f'Input: @ints = ({ intlist })') + ints = duplicateZeros(ints) + intlist = ", ".join([ str(i) for i in ints ]) + print(f'Output: ({ intlist })'); + +print("Example 1:") +solution([1, 0, 2, 3, 0, 4, 5, 0]) + +print("\nExample 2:") +solution([1, 2, 3]) + +print("\nExample 3:") +solution([0, 3, 0, 4, 5])
\ No newline at end of file diff --git a/challenge-235/packy-anderson/raku/ch-1.raku b/challenge-235/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..8247392201 --- /dev/null +++ b/challenge-235/packy-anderson/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku + +use v6; + +sub isStrictlyIncreasing(*@ints where ($_.all ~~ Int)) { + # get the first integer from the list + my $last_int = shift @ints; + for @ints -> $this_int { + return 0 if $this_int <= $last_int; + $last_int = $this_int; + } + return 1; +} + +sub isStrictlyIncreasingExceptOne(*@ints where ($_.all ~~ Int)) { + # the list cannot be strictly increasing unless + # there are at least two items in it + return 0 if @ints <= 1; + + # if it's strictly increasing without removing + # an item, it fails the test + return 0 if isStrictlyIncreasing(@ints); + + # loop over the list by index + loop (my $i = 1; $i < @ints.elems; $i++) { + if (@ints[$i] <= @ints[$i - 1]) { + # remove the bigger item from the list + @ints.splice($i-1, 1); + # after removing the element, is + # the list strictly increasing? + return isStrictlyIncreasing(@ints); + } + } +} + +sub solution(*@ints where ($_.all ~~ Int)) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my $output = isStrictlyIncreasingExceptOne(@ints); + say 'Output: ' ~ ($output ?? 'true' !! 'false'); +} + +say "Example 1:"; +solution(0, 2, 9, 4, 6); + +say "\nExample 2:"; +solution(5, 1, 3, 2); + +say "\nExample 3:"; +solution(2, 2, 3); + +say "\nExample 4 from James Curtis-Smith:"; +solution(1,2,3,4,1,2,3);
\ No newline at end of file diff --git a/challenge-235/packy-anderson/raku/ch-2.raku b/challenge-235/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..960f6f2a6f --- /dev/null +++ b/challenge-235/packy-anderson/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +use v6; + +sub duplicateZeros(*@ints where ($_.all ~~ Int)) { + loop (my $i = 0; $i < @ints.elems; $i++) { + if (@ints[$i] == 0) { + @ints.splice($i+1, 0, 0); # insert a 0 at $i+1 + @ints.splice(*-1); # pop the last element off @ints + $i++; # skip over the 0 we added! + } + } + return @ints; +} + +sub solution(*@ints where ($_.all ~~ Int)) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + @ints = duplicateZeros(@ints); + say 'Output: (' ~ @ints.join(', ') ~ ')'; +} + +say "Example 1:"; +solution(1, 0, 2, 3, 0, 4, 5, 0); + +say "\nExample 2:"; +solution(1, 2, 3); + +say "\nExample 3:"; +solution(0, 3, 0, 4, 5);
\ No newline at end of file |
