diff options
| -rw-r--r-- | challenge-083/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-083/adam-russell/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-083/adam-russell/perl/ch-2.pl | 59 |
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-083/adam-russell/blog.txt b/challenge-083/adam-russell/blog.txt new file mode 100644 index 0000000000..214391c548 --- /dev/null +++ b/challenge-083/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2020/10/25#pwc083 diff --git a/challenge-083/adam-russell/perl/ch-1.pl b/challenge-083/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..87b36cc61e --- /dev/null +++ b/challenge-083/adam-russell/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# You are given a string $S with 3 or more words. +# Write a script to find the length of the string +# except the first and last words ignoring whitespace. +## +sub count_most_words{ + my ($s) = @_; + my $count = 0; + my @a = split(/\s/, $s); + map {$count += tr/a-zA-Z//d} @a[1 .. (@a - 2)]; + return $count; +} + +MAIN:{ + my $S; + $S = "The Weekly Challenge"; + print "$S --> " . count_most_words($S) . "\n"; + $S = "The purpose of our lives is to be happy"; + print "$S --> " . count_most_words($S) . "\n"; +}
\ No newline at end of file diff --git a/challenge-083/adam-russell/perl/ch-2.pl b/challenge-083/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..3d6e73e6df --- /dev/null +++ b/challenge-083/adam-russell/perl/ch-2.pl @@ -0,0 +1,59 @@ +use strict; +use warnings; +## +# You are given an array @A of positive numbers. +# Write a script to flip the sign of some members +# of the given array so that the sum of the all +# members is minimum non-negative. +## +sub try_all_flips{ + my(@a) = @_; + my @minimum = (undef, undef, []); + for my $i (0 .. (2**(@a) - 1)){ + my $b = sprintf("%0" . @a . "b", $i); + my @b = split(//, $b); + my $flip_count = 0; + map {$flip_count++ if $_ == 1} @b; + my @f; + for my $i (0 .. (@b - 1)){ + if($b[$i] == 1){ + push @f, (-1) * $a[$i]; + } + else{ + push @f, $a[$i]; + } + } + my $sum = unpack("%32I*", pack("I*", @f)); + if(!defined($minimum[0]) || ($sum <= $minimum[0] && $sum >= 0)){ + if(defined($minimum[0]) && $sum == $minimum[0] && $flip_count < $minimum[1]){ + $minimum[0] = $sum; + $minimum[1] = $flip_count; + $minimum[2] = \@f; + } + elsif(!defined($minimum[0])){ + $minimum[0] = $sum; + $minimum[1] = $flip_count; + $minimum[2] = \@f; + } + elsif($sum < $minimum[0]){ + $minimum[0] = $sum; + $minimum[1] = $flip_count; + $minimum[2] = \@f; + } + } + } + return @minimum; +} + +MAIN:{ + my @A; + my @minimum; + @A = (3, 10, 8); + @minimum = try_all_flips(@A); + print "[". join(", ", @A) . "] --> "; + print " [". join(", ", @{$minimum[2]}) . "] = " . $minimum[0] ."\n"; + @A = (12, 2, 10); + @minimum = try_all_flips(@A); + print "[". join(", ", @A) . "] --> "; + print " [". join(", ", @{$minimum[2]}) . "] = " . $minimum[0] ."\n"; +}
\ No newline at end of file |
