diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-05-13 12:35:45 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-05-13 12:35:45 -0400 |
| commit | b26ca6c4fefa7dfec87855ee69e723105fcf9584 (patch) | |
| tree | a9f2b780bc562e8b1b2d353788b1fffa40553f10 | |
| parent | 02e309b3d451fff60404eb5ab3e539056f99ce0d (diff) | |
| download | perlweeklychallenge-club-b26ca6c4fefa7dfec87855ee69e723105fcf9584.tar.gz perlweeklychallenge-club-b26ca6c4fefa7dfec87855ee69e723105fcf9584.tar.bz2 perlweeklychallenge-club-b26ca6c4fefa7dfec87855ee69e723105fcf9584.zip | |
Week 269
| -rw-r--r-- | challenge-269/zapwai/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-269/zapwai/perl/ch-2.pl | 22 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-269/zapwai/perl/ch-1.pl b/challenge-269/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..6cd59d6f32 --- /dev/null +++ b/challenge-269/zapwai/perl/ch-1.pl @@ -0,0 +1,43 @@ +use v5.38; +my @ints = (1, 2, 3, 4, 5); +my @ints2 = (2, 3, 8, 16); +my @ints3 = (1, 3, 5, 7, 9); +proc(@ints); +proc(@ints2); +proc(@ints3); + +sub truth(@ints) { + my $N = @ints; + for my $i (0 .. 2**$N - 1) { + my $d = sprintf '%b', "$i"; + $d = '0' x ($N - length $d) . $d; + my @D = split "", $d; + my @current_list; + for my $j (0 .. $#D) { + if (1 eq $D[$j]) { + push @current_list, $ints[$j]; + } + } + next if (1 >= @current_list); + my $tally = 0; + $tally = $tally | $_ for (@current_list); + my $x = sprintf "%b", $tally; + my $last_bin_dig = substr $x, -1; + if ($last_bin_dig eq '0') { + print join(", ", @current_list), " -> $x\t"; + return 1; + } + } + return 0; +} + +sub proc(@ints) { + say "Input: \@ints = (", join(", ", @ints), ")"; + print "Output: "; + if (truth(@ints)) { + say "true"; + } else { + say "false"; + } +} + diff --git a/challenge-269/zapwai/perl/ch-2.pl b/challenge-269/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..afba39448c --- /dev/null +++ b/challenge-269/zapwai/perl/ch-2.pl @@ -0,0 +1,22 @@ +use v5.38; +my @ints = (2, 1, 3, 4, 5); +my @ints2 = (3,2,4); +my @ints3 = (5, 4, 3, 8); +proc(@ints); +proc(@ints2); +proc(@ints3); + +sub proc(@ints) { + say "Input: \@ints = (", join(", ", @ints), ")"; + my @arr1 = (shift @ints); + my @arr2 = (shift @ints); + do { + my $x = shift @ints; + if ($arr1[$#arr1] > $arr2[$#arr2]) { + push @arr1, $x; + } else { + push @arr2, $x; + } + } while (@ints > 0); + say "Output: \@arr1 = (", join(", ", @arr1), ") \@arr2 = (", join(", ", @arr2), ")"; +} |
