diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-10 21:22:05 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-10 21:22:05 +0100 |
| commit | 65d163bc9208ec056bf376d0b4802b4b18fad599 (patch) | |
| tree | 65e8e6c839107f26832d92fb5a6b187ecfe1d562 /challenge-055 | |
| parent | 1d50a0e5a8b5d70058b677ea19cc4a294f1aba32 (diff) | |
| download | perlweeklychallenge-club-65d163bc9208ec056bf376d0b4802b4b18fad599.tar.gz perlweeklychallenge-club-65d163bc9208ec056bf376d0b4802b4b18fad599.tar.bz2 perlweeklychallenge-club-65d163bc9208ec056bf376d0b4802b4b18fad599.zip | |
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-055')
| -rw-r--r-- | challenge-055/javier-luque/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-055/javier-luque/perl/ch-1.pl | 90 | ||||
| -rw-r--r-- | challenge-055/javier-luque/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-055/javier-luque/raku/ch-1.p6 | 80 | ||||
| -rw-r--r-- | challenge-055/javier-luque/raku/ch-2.p6 | 17 |
5 files changed, 215 insertions, 0 deletions
diff --git a/challenge-055/javier-luque/blog.txt b/challenge-055/javier-luque/blog.txt new file mode 100644 index 0000000000..56430d807c --- /dev/null +++ b/challenge-055/javier-luque/blog.txt @@ -0,0 +1 @@ +https://perlchallenges.wordpress.com/2020/04/06/perl-weekly-challenge-055/ diff --git a/challenge-055/javier-luque/perl/ch-1.pl b/challenge-055/javier-luque/perl/ch-1.pl new file mode 100644 index 0000000000..c195f61949 --- /dev/null +++ b/challenge-055/javier-luque/perl/ch-1.pl @@ -0,0 +1,90 @@ +#!/usr/bin/perl +# Test: ./ch-1.pl 2 3 +use strict; +use warnings; +use feature qw /say/; + +my $b = $ARGV[0]; +my $n = $ARGV[1]; + +# Calculate $n if not supplied +$n = int(log($b) / log(2)) + 1 + unless($n); + +# Lets keep it to 1 to 32 bits and +# throw out bits outside of our range +$n = 32 if ($n > 32); +$n = 1 if ($n < 1); +my $mask = (2 ** $n - 1); +$b = $b & $mask; + +# Print the original number +say "ORIGINAL: " . sprintf("%0${n}B", $b) . "\n"; + +# Store the solution; +my %solutions; +my $longest_soluton = 0; + +# flip algorithm +for my $l (0 .. $n -1) { + for my $r ($l .. $n - 1) { + # We create two bit masks. + # The first mask is what we use to flip the bits + # and the second mask is what we use to keep the + # original builts then add up the flipped bits + # with the kept bits + + # Flip Mask + my $flip_mask = 0; + for my $i ($l .. $r) { + $flip_mask += (2 ** ($n - $i - 1) ); + } + + # Calculate the keep mask + my $keep_mask = (~ $flip_mask) & $mask; + + # Flip the relevant bits and calculate kept bits + my $flipped_bits = ~ ($b & $flip_mask) & $flip_mask; + my $kept_bits = $b & $keep_mask; + + # Add the bits outside the flipped bit + my $flipped_number = $flipped_bits + $kept_bits; + + # Now store the number of ones + my $length = calculate_true_bits($flipped_number); + + # Store the solutions + $solutions{$length} = [] + unless ($solutions{$length}); + + push @{$solutions{$length}}, { + L => $l, + R => $r, + number => $flipped_number + }; + + # Length of the longest solution + $longest_soluton = $length + if ($longest_soluton < $length); + } +} + +# Print the solutions +say "SOLUTIONS length($longest_soluton):"; +for my $solution (@{$solutions{$longest_soluton}}) { + say 'L: ' . $solution->{L} . + ' R: ' . $solution->{R} . + ' number: ' . sprintf("%0${n}B", $solution->{number}); +} + +# Calculate the number of true bits +sub calculate_true_bits { + my $number = shift; + my $count = 0; + + do { + $count++ if ($number & 1); + } while ($number = $number >> 1); + + return $count; +} diff --git a/challenge-055/javier-luque/perl/ch-2.pl b/challenge-055/javier-luque/perl/ch-2.pl new file mode 100644 index 0000000000..0658a75b6d --- /dev/null +++ b/challenge-055/javier-luque/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +# Test: ./ch-1.pl 4 +use strict; +use warnings; +use feature qw /say/; +use Algorithm::Combinatorics qw(permutations); + +my $n = $ARGV[0]; +my @array = (1 .. $n); + +# Permutations +my $iter = permutations(\@array); + +# Loop through each combination +while (my $p = $iter->next) { + my $is_wave = 1; + + for (my $i = 1; $i < scalar(@$p); $i++) { + if ( $i % 2 == 1 && $p->[$i] >= $p->[$i - 1] || + $i % 2 == 0 && $p->[$i] <= $p->[$i - 1] ) {; + $is_wave = 0; + last; + } + } + + say join ' ', @$p if ($is_wave); +} diff --git a/challenge-055/javier-luque/raku/ch-1.p6 b/challenge-055/javier-luque/raku/ch-1.p6 new file mode 100644 index 0000000000..5fb8b9d065 --- /dev/null +++ b/challenge-055/javier-luque/raku/ch-1.p6 @@ -0,0 +1,80 @@ +# Test: perl6 ch-1.p6 2 3 + +multi MAIN(Int $b where * > 0) { + my $n = ((log($b) / log(2)) + 1).Int; + $n = 32 if ($n > 32); + MAIN($b, $n); +} + +multi MAIN(Int $a where * > 0 , Int $n where 0 < * < 32) { + # Throw out bits outside of our range + my $mask = (2 ** $n - 1); + my $b = $a +& $mask; + + # Original binary + say "ORIGINAL: " ~ sprintf("%0*b", $n, $b) ~ "\n"; + + # Store the solution; + my %solutions; + my $longest_solution = 0; + + # flip algorithm + for (0 .. $n -1) -> $l { + for ($l .. $n - 1) -> $r { + # Flip Mask + my $flip_mask = 0; + for ($l .. $r) -> $i { + $flip_mask += (2 ** ($n - $i - 1) ); + } + + # Calculate the keep mask + my $keep_mask = (+^ $flip_mask) +& $mask; + + # Flip the relevant bits and calculate kept bits + my $flipped_bits = +^ ($b +& $flip_mask) +& $flip_mask; + my $kept_bits = $b +& $keep_mask; + + # Add the bits outside the flipped bit + my $flipped_number = $flipped_bits + $kept_bits; + + # Now store the number of ones + my $length = calculate-true-bits($flipped_number); + + # Store the solutions + %solutions{$length} = [] + unless (%solutions{$length}); + + my %solution = ( + L => $l, + R => $r, + number => $flipped_number + ); + + %solutions{$length}.push(%solution); + + # Length of the longest solution + $longest_solution = $length + if ($longest_solution < $length); + } + } + + say "SOLUTIONS length($longest_solution):"; + for (@(%solutions{$longest_solution})) -> $solution { + say 'L: ' ~ $solution{'L'} ~ + ' R: ' ~ $solution{'R'} ~ + ' Number: ' ~ + sprintf("%0*b", $n, $solution{'number'}); + } +} + +# Calculate the number of true bits +sub calculate-true-bits(Int $n is copy) { + my $count = 0; + + repeat { + $count++ if ($n +& 1); + } while ($n = $n +> 1); + + return $count; +} + diff --git a/challenge-055/javier-luque/raku/ch-2.p6 b/challenge-055/javier-luque/raku/ch-2.p6 new file mode 100644 index 0000000000..e45180bb93 --- /dev/null +++ b/challenge-055/javier-luque/raku/ch-2.p6 @@ -0,0 +1,17 @@ +# Test: perl6 ch-2.p6 4 +sub MAIN(Int $n where * > 0) { + [1 .. $n].permutations.grep({is-wave($_)}).join("\n").say; +} + +# Is the array a wave +sub is-wave(@n) { + my $is_wave = True; + loop (my $i = 1; $i < @n.elems; $i++) { + if ( $i % 2 == 1 && @n[$i] >= @n[$i - 1] || + $i % 2 == 0 && @n[$i] <= @n[$i - 1] ) {; + $is_wave = False; + last; + } + } + return $is_wave; +} |
