diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-17 17:05:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-17 17:05:35 +0100 |
| commit | ec4b720a8f17f7e3e3af5870cdf9e1eefa258f1f (patch) | |
| tree | 34dd3c6f2fdcd69f53eb9b2e553a0178eade2a98 /challenge-160 | |
| parent | b1729074f0ff73db75f43b1cdccd1a3011d9ee97 (diff) | |
| parent | 7a07384b5d0a9ed20a37c2db720e2173a792013e (diff) | |
| download | perlweeklychallenge-club-ec4b720a8f17f7e3e3af5870cdf9e1eefa258f1f.tar.gz perlweeklychallenge-club-ec4b720a8f17f7e3e3af5870cdf9e1eefa258f1f.tar.bz2 perlweeklychallenge-club-ec4b720a8f17f7e3e3af5870cdf9e1eefa258f1f.zip | |
Merge pull request #5952 from E7-87-83/newt
Week 160
Diffstat (limited to 'challenge-160')
| -rw-r--r-- | challenge-160/cheok-yin-fung/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-160/cheok-yin-fung/perl/ch-2.pl | 109 |
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-160/cheok-yin-fung/perl/ch-1.pl b/challenge-160/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..b4fdf7471d --- /dev/null +++ b/challenge-160/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +# The Weekly Challenge 160 +# Task 1 Four is Magic +use v5.22.0; +use warnings; + + +fim($ARGV[0]) if defined($ARGV[0]); + + + +sub fim { + my @num = ("zero", "one", "two", "three", "four", "five", + "six", "seven", "eight", "nine"); + my $spell = "four is magic."; + my $c = $_[0]; + if ($c == 4) { + print ucfirst($spell), "\n"; + return; + } + $num[$c] = ucfirst($num[$c]); + while ($c != 4) { + print $num[$c]; + $c = length $num[$c]; + print " is $num[$c], "; + } + print "$spell\n"; +} + + + +=pod Testing + +for (1..9) { + fim($_); + print "\n"; +} diff --git a/challenge-160/cheok-yin-fung/perl/ch-2.pl b/challenge-160/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..8c03ee08a5 --- /dev/null +++ b/challenge-160/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,109 @@ +#!/usr/bin/perl +# The Weekly Challenge 160 +# Task 2 Equilibrium Index +use v5.22.0; +use warnings; +use List::Util qw/sum/; + + +say ei(@ARGV) if defined($ARGV[0]); + + + +sub ei { + my @n = @_; + my $ind = int $#n/2; + my $previous_ind = $ind; + my $new_ind = $ind; + my $hint_lower = sum(@n[0..$ind-1]); + my $hint_upper = sum(@n[$ind+1..$#n]); + my $step = 0; + my $expired_ind = $ind; + my $guessed_step = 0; + + my $success = $hint_lower == $hint_upper; + return $ind if $success; + + + while (!$success) { + return -1 if $ind < 1 or $ind > $#n-1; + + my $cur_val = $n[$ind]; + + # Modify the two portions + if ($step == +1) { + $hint_lower = $hint_lower + $n[$expired_ind]; + $hint_upper = $hint_upper - $cur_val; + } + elsif ($step == -1) { + $hint_lower = $hint_lower - $cur_val; + $hint_upper = $hint_upper + $n[$expired_ind]; + } + + + # Decide which direction going to move + if ($hint_lower+$cur_val < $hint_upper) { + $new_ind = $ind+1; + } + elsif ($hint_lower > $hint_upper+$cur_val) { + $new_ind = $ind-1; + } + elsif ($hint_lower+$cur_val == $hint_upper) { + $new_ind = $ind+$step; + } + elsif ($hint_lower == $hint_upper+$cur_val) { + $new_ind = $ind+$step; + } + + else { + if ($hint_lower+$cur_val > $hint_upper) { + if ($hint_lower+$cur_val >= $hint_upper-$n[$ind+1]) { + $new_ind = $ind+1; + } + else { + $new_ind = $ind-1; + } + } + if ($hint_lower < $hint_upper+$cur_val) { + $new_ind = $ind-1; + } + + if ($hint_lower == $hint_upper+$cur_val + or + $hint_lower+$cur_val == $hint_upper) + { + $new_ind = $ind+$step; # follow the previous direction + } + } + + # Prepare for the next loop block, or, stop + $step = $new_ind-$ind; + $success = $hint_lower == $hint_upper; + return $ind if $success; + return -1 if $guessed_step == -$step; # No back and fro! + $guessed_step = $step; + ($expired_ind, $ind) = ($ind, $new_ind); + } +} + + + +use Test::More tests=>50; +say "TESTING:"; +my @n_temp; +for my $case (1..50) { + $n_temp[$_] = 1 + int rand(40) for (0..31); + ok ei_simple(@n_temp) == ei(@n_temp); +} +done_testing(); + + + +sub ei_simple { + my @seq = @_; + for (1..$#seq-1) { + return $_ if sum(@seq[0..$_-1]) == sum(@seq[$_+1..$#seq]); + } + return -1; +} + |
