diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-31 22:09:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-31 22:09:38 +0000 |
| commit | 25a52bf6274e16d817e5da5598012201b1d23c29 (patch) | |
| tree | 042a06684f8d6d187209577e1a95985c6e442fbb /challenge-097 | |
| parent | 5b9b43e4ca099224e5a080485625b9ff135881ed (diff) | |
| parent | 471ab70e3e9e4ba65a158c2e2286bc404cef8927 (diff) | |
| download | perlweeklychallenge-club-25a52bf6274e16d817e5da5598012201b1d23c29.tar.gz perlweeklychallenge-club-25a52bf6274e16d817e5da5598012201b1d23c29.tar.bz2 perlweeklychallenge-club-25a52bf6274e16d817e5da5598012201b1d23c29.zip | |
Merge pull request #3432 from kaicb97/branch-for-challenge-097
perl solution for week 97
Diffstat (limited to 'challenge-097')
| -rwxr-xr-x | challenge-097/kai-burgdorf/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-097/kai-burgdorf/perl/ch-2.pl | 106 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-097/kai-burgdorf/perl/ch-1.pl b/challenge-097/kai-burgdorf/perl/ch-1.pl new file mode 100755 index 0000000000..52dec58b8c --- /dev/null +++ b/challenge-097/kai-burgdorf/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $S = $ARGV[0] || ""; +my $N = $ARGV[1] || 0; + +$N = $N%26; + +my @chars = split //, $S; + +foreach(@chars) { + + my $original_ascii = ord($_); + my $shifted_ascii = $original_ascii-$N; + + if(($original_ascii < 91 && $shifted_ascii < 65) or ($original_ascii >= 97 && $shifted_ascii < 97)) { + $shifted_ascii += 26; + } + + print chr($shifted_ascii); +} + +print "\n"; diff --git a/challenge-097/kai-burgdorf/perl/ch-2.pl b/challenge-097/kai-burgdorf/perl/ch-2.pl new file mode 100755 index 0000000000..f7efeadad9 --- /dev/null +++ b/challenge-097/kai-burgdorf/perl/ch-2.pl @@ -0,0 +1,106 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Algorithm::Permute; + +my $B = $ARGV[0] || "101100101"; +my $S = $ARGV[1] || 3; + +die "(length of $B) " . ( length $B ) . " % $S = " . ( ( length $B ) % $S ) . ", must be 0" + if ( ( length $B ) % $S != 0 ); + +my @substrings = get_sequences($B); +my $permutations = get_permutations($S); +my %result = %{ get_number_of_flips( $permutations, \@substrings ) }; + +print_output( $B, $S, \%result, \@substrings ); + +sub get_number_of_flips { + my ( $p, $s ) = @_; + + my @sequences = @$s; + + my $shortest_distance; + my $shortest_target = $sequences[0]; + + while ( my @res = $p->next ) { + my $sum = 0; + + foreach (@sequences) { + my $bits = join "", @res; + $sum += hemming_distance( $_, $bits ); + } + + if ( !$shortest_distance || $sum < $shortest_distance ) { + $shortest_distance = $sum; + $shortest_target = join "", @res; + } + } + + return { target => $shortest_target, flips => $shortest_distance }; +} + +sub get_sequences { + my ($b) = @_; + + my @digits = split //, $b; + + my @substrings; + while (@digits) { + my $sequence; + for ( my $i = 0 ; $i < $S ; $i++ ) { + $sequence .= shift @digits; + } + push @substrings, $sequence; + } + + return @substrings; +} + +sub get_permutations { + my ($number_of_digits) = @_; + + my @tmp; + for ( 1 .. $number_of_digits ) { + push @tmp, 0; + push @tmp, 1; + } + + return Algorithm::Permute->new( \@tmp, $number_of_digits ); +} + +sub hemming_distance { + my ( $first, $second ) = @_; + + my @a = split "", $first; + my @b = split "", $second; + + my $distance = 0; + + my $i = 0; + foreach (@a) { + if ( $a[$i] ne $b[$i] ) { + $distance++; + } + $i++; + } + + return $distance; +} + +sub print_output { + my ( $b, $s, $result, $substrings ) = @_; + + print "Input: \$B = $b, \$S = $s\nOutput: $result->{flips}\n\nBinary Substrings:\n"; + + @substrings = @$substrings; + + foreach (@substrings) { + my $distance = hemming_distance( $_, $result->{target} ); + print "\"$_\": $distance flip "; + print "to make it $result->{target}" if ( $distance > 0 ); + print "\n"; + } +} |
