diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-07 13:13:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-07 13:13:07 +0000 |
| commit | 06422ed09504a519eb58e639fe08543d06d86bf4 (patch) | |
| tree | 2d582d0d25bde1294518be228369424bb09234e8 | |
| parent | caf1c1198cd37dcdc0f0c72077b434345000f684 (diff) | |
| parent | 73d65b3bbde08c84da1259a5e8f4c32234031981 (diff) | |
| download | perlweeklychallenge-club-06422ed09504a519eb58e639fe08543d06d86bf4.tar.gz perlweeklychallenge-club-06422ed09504a519eb58e639fe08543d06d86bf4.tar.bz2 perlweeklychallenge-club-06422ed09504a519eb58e639fe08543d06d86bf4.zip | |
Merge pull request #11536 from jacoby/master
DAJ 307
| -rw-r--r-- | challenge-306/dave-jacoby/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-306/dave-jacoby/perl/ch-2.pl | 49 | ||||
| -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 |
6 files changed, 239 insertions, 0 deletions
diff --git a/challenge-306/dave-jacoby/perl/ch-1.pl b/challenge-306/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..901dffcdff --- /dev/null +++ b/challenge-306/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 2, 5, 3, 6, 4 ], + [ 1, 3 ], +); + +for my $example (@examples) { + my $ints = join ', ', $example->@*; + my $output = odd_sum( $example->@* ); + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub odd_sum(@ints) { + my $output; + for my $l ( map { $_ * 2 - 1 } 1 .. $#ints ) { + next if $l > scalar @ints; + for my $i ( 0 .. $#ints ) { + my $end = -1 + $i + $l; + next unless defined $ints[$end]; + my @slice = @ints[ $i .. $end ]; + $output += sum0(@slice); + } + } + return $output; +} diff --git a/challenge-306/dave-jacoby/perl/ch-2.pl b/challenge-306/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..fed124329b --- /dev/null +++ b/challenge-306/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ first }; + +my @examples = ( + + [ 3, 8, 5, 2, 9, 2 ], + [ 3, 2, 5 ], +); + +for my $example (@examples) { + my $ints = join ', ', $example->@*; + my $output = last_element( $example->@* ); + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub last_element(@ints) { + while ( scalar @ints > 1 ) { + my ( $y, $x ) = sort { $b <=> $a } @ints; + if ( $x == $y ) { + # there's ugly enough that, if I was to rewrite this, + # I would separate this into a function, probably. + # As a one-off, it isn't worth it. + my $ix = first { $ints[$_] == $x } 0 .. $#ints; + $ints[$ix] = undef; + @ints = grep { defined } @ints; + my $iy = first { $ints[$_] == $y } 0 .. $#ints; + $ints[$iy] = undef; + @ints = grep { defined } @ints; + } + else { + my $yx = $y - $x; + my $iy = first { $ints[$_] == $y } 0 .. $#ints; + $ints[$iy] = undef; + @ints = grep { defined } @ints; + my $ix = first { $ints[$_] == $x } 0 .. $#ints; + $ints[$ix] = $yx; + @ints = grep { defined } @ints; + } + } + return scalar @ints; +} 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() |
