From a16e084cd7b30c01b53ba846a263207f99d1d9c6 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 10 Apr 2020 19:35:17 +0200 Subject: Task 1 Perl LK --- challenge-055/lubos-kolouch/perl/ch-1.pl | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-055/lubos-kolouch/perl/ch-1.pl 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; -- cgit From e38c47dfecc6854b829238b5a8abaad40f11a00f Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 10 Apr 2020 19:57:31 +0200 Subject: Task 2 Perl LK --- challenge-055/lubos-kolouch/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-055/lubos-kolouch/perl/ch-2.pl 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; -- cgit