diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-02-06 22:15:01 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2025-02-06 22:15:01 -0500 |
| commit | 73d65b3bbde08c84da1259a5e8f4c32234031981 (patch) | |
| tree | a39e0d4ea956a5ad62915b4618918edd9c652df5 | |
| parent | c504cdab4acc696ebcd16f0e411c7b75b61f23f7 (diff) | |
| download | perlweeklychallenge-club-73d65b3bbde08c84da1259a5e8f4c32234031981.tar.gz perlweeklychallenge-club-73d65b3bbde08c84da1259a5e8f4c32234031981.tar.bz2 perlweeklychallenge-club-73d65b3bbde08c84da1259a5e8f4c32234031981.zip | |
DAJ 307
| -rw-r--r-- | challenge-307/dave-jacoby/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-307/dave-jacoby/perl/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-307/dave-jacoby/perl/ch-2.pl | 37 | ||||
| -rw-r--r-- | challenge-307/dave-jacoby/perl/ch-2.py | 35 |
4 files changed, 154 insertions, 0 deletions
diff --git a/challenge-307/dave-jacoby/perl/ch-1.pl b/challenge-307/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..f84583ea06 --- /dev/null +++ b/challenge-307/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 5, 2, 4, 3, 1 ], + [ 1, 2, 1, 1, 3 ], + [ 3, 1, 3, 2, 3 ], +); + +for my $example (@examples) { + my $ints = join ', ', $example->@*; + my @output = check_order( $example->@* ); + my $output = join ', ', @output; + say <<"END"; + Input: \@ints = ($ints) + Output: ($)output +END +} + +sub check_order (@ints) { + my @sort = sort { $a <=> $b } @ints; + my @output = grep { $ints[$_] ne $sort[$_] } 0 .. $#ints; + return @output; +} diff --git a/challenge-307/dave-jacoby/perl/ch-1.py b/challenge-307/dave-jacoby/perl/ch-1.py new file mode 100644 index 0000000000..373baa040e --- /dev/null +++ b/challenge-307/dave-jacoby/perl/ch-1.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +from itertools import permutations + + +def main(): + examples = [ + { + "x": 5, + "y": 3, + "str": ["10", "0001", "111001", "1", "0"], + }, + { + "x": 1, + "y": 1, + "str": ["10", "1", "0"], + }, + ] + for e in examples: + x = e["x"] + y = e["y"] + str = e["str"] + output = ones_and_zeros(e) + print(f" Input: str = {str}") + print(f" x = {x}") + print(f" y = {y}") + print(f" Output: {output}") + print("") + + +def ones_and_zeros(e): + x = e["x"] + y = e["y"] + str = e["str"] + l = 1 + len(str) + o = [] + sizes = [*range(1, l)] + sizes.reverse() + for s in sizes: + str1 = e["str"] + ps = permutations(str1, s) + for p in ps: + pstr = "".join(x for x in p) + cx = pstr.count("0") + cy = pstr.count("1") + if cx == x and cy == y: + o.append(s) + return o[0] + + +if __name__ == "__main__": + main() diff --git a/challenge-307/dave-jacoby/perl/ch-2.pl b/challenge-307/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..d2535bd059 --- /dev/null +++ b/challenge-307/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ first }; + +my @examples = ( + + [ "acca", "dog", "god", "perl", "repl" ], + [ "abba", "baba", "aabb", "ab", "ab" ], +); + +for my $example (@examples) { + my $words = join ', ', map {qq{"$_"}} $example->@*; + my $output = find_anagrams( $example->@* ); + say <<"END"; + Input: \@words = ($words) + Output: $output +END +} + +sub find_anagrams(@words) { + for my $i ( 0 .. $#words ) { + next unless defined $words[$i]; + my $w = $words[$i]; + my $x = join '', sort split //, $w; + for my $j ( $i+1 .. $#words ) { + my $y = join '', sort split //, $words[$j]; + next unless $x eq $y; + $words[$j] = undef; + } + } + @words = grep { defined } @words; + return scalar @words; +} diff --git a/challenge-307/dave-jacoby/perl/ch-2.py b/challenge-307/dave-jacoby/perl/ch-2.py new file mode 100644 index 0000000000..e8eac1566b --- /dev/null +++ b/challenge-307/dave-jacoby/perl/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + + +def main(): + examples = [ + [-3, 2, -3, 4, 2], + [1, 2], + [1, -2, -3], + ] + for e in examples: + output = step_by_step(e) + print(f" Input: string = {e}") + print(f" Output: {output}") + print("") + + +def step_by_step(ints): + max = 20 + o = -1 + for i in [*range(1, max)]: + n = i + flag1 = 1 + for v in ints: + if flag1: + n += v + if n < 1: + flag1 = 0 + if flag1: + if o == -1: + o = i + return o + + +if __name__ == "__main__": + main() |
