diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-10 19:42:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-10 19:42:20 +0100 |
| commit | 369b7d61bf09e0a973cf120d669dd07b5300a71b (patch) | |
| tree | 4b8b49a582b812a68e23484a542bacc6ef0b3bb7 | |
| parent | 1a14099d8fc40b7cf6c522c3c120059ed396c5a6 (diff) | |
| parent | e38c47dfecc6854b829238b5a8abaad40f11a00f (diff) | |
| download | perlweeklychallenge-club-369b7d61bf09e0a973cf120d669dd07b5300a71b.tar.gz perlweeklychallenge-club-369b7d61bf09e0a973cf120d669dd07b5300a71b.tar.bz2 perlweeklychallenge-club-369b7d61bf09e0a973cf120d669dd07b5300a71b.zip | |
Merge pull request #1547 from kolcon/chal_055_LK
Task 1 Perl LK
| -rw-r--r-- | challenge-055/lubos-kolouch/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-055/lubos-kolouch/perl/ch-2.pl | 50 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-055/lubos-kolouch/perl/ch-1.pl b/challenge-055/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..72c81ba0e0 --- /dev/null +++ b/challenge-055/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,42 @@ +##!/usr/bin/env perl + +use strict; +use warnings; +use Data::Dumper; +use feature qw/say/; + +sub process_all_flips { + my $binary = shift; + my %all_results; + + my $max = 0; + + my @numbers = split //, $binary; + + for my $l_count ( 0 .. scalar(@numbers)-1 ) { + for my $r_count ( $l_count .. scalar(@numbers)-1 ) { + my $inverted = do_flip( $l_count, $r_count, $binary ); + my $ones_count = () = $inverted =~ /1/gi; + push @{ $all_results{$ones_count} }, [ $l_count, $r_count ]; + $max = $ones_count if $ones_count > $max; + } + } + + return $all_results{$max}; +} + +sub do_flip { + + my ( $l_count, $r_count, $input ) = @_; + + my @binary = split //, $input; + + for my $str_pos ( $l_count .. $r_count ) { + $binary[$str_pos] = $binary[$str_pos] == 1 ? 0:1; + } + + return join "", @binary; +} + +my $result = process_all_flips("010"); +warn Dumper \$result; diff --git a/challenge-055/lubos-kolouch/perl/ch-2.pl b/challenge-055/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..d49b674046 --- /dev/null +++ b/challenge-055/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,50 @@ +##!/usr/bin/env perl + +use strict; +use warnings; +use Math::Combinatorics; +use Data::Dumper; +use feature qw/say/; + +sub process_all_waves { + + my $arr_ref = shift; + + my @result; + + my $combinat = Math::Combinatorics->new( + count => scalar(@$arr_ref), + data => [@$arr_ref], + ); + + while ( my @permu = $combinat->next_permutation ) { + push @result, \@permu if check_wave( \@permu ); + } + + return \@result; +} + +sub check_wave { + my $perm_ref = shift; + + # 1 = greater, -1 = smaller + my $direction = 1; + + for ( 0 .. scalar(@$perm_ref) - 2 ) { + if ( $direction == 1 ) { + # stop if wave is broken + return 0 if $$perm_ref[$_] < $$perm_ref[ $_ + 1 ]; + } + else { + return 0 if $$perm_ref[$_] > $$perm_ref[ $_ + 1 ]; + } + + $direction *= -1; + } + + return 1; +} + +my $all_waves = process_all_waves( [ 1, 2, 3, 4 ] ); + +warn Dumper \$all_waves; |
