diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-24 02:19:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-24 02:19:23 +0100 |
| commit | eb0c760df758c0575002986bd23ff22cb51a7895 (patch) | |
| tree | b54c8c79b3c961bcff30cd06466c77cf4f4cf62f | |
| parent | 9444788bca2a3fa0d5dba2f70a1cb0758c235a01 (diff) | |
| parent | f78fa81eaa6f660416bdce124af5e0346a8d8618 (diff) | |
| download | perlweeklychallenge-club-eb0c760df758c0575002986bd23ff22cb51a7895.tar.gz perlweeklychallenge-club-eb0c760df758c0575002986bd23ff22cb51a7895.tar.bz2 perlweeklychallenge-club-eb0c760df758c0575002986bd23ff22cb51a7895.zip | |
Merge pull request #8429 from adamcrussell/challenge-226
initial commit
| -rw-r--r-- | challenge-226/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-226/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-226/adam-russell/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-226/adam-russell/perl/ch-2.pl | 21 | ||||
| -rw-r--r-- | challenge-226/adam-russell/prolog/ch-1.p | 2 | ||||
| -rw-r--r-- | challenge-226/adam-russell/prolog/ch-2.p | 14 |
6 files changed, 56 insertions, 0 deletions
diff --git a/challenge-226/adam-russell/blog.txt b/challenge-226/adam-russell/blog.txt new file mode 100644 index 0000000000..5b65eb08bc --- /dev/null +++ b/challenge-226/adam-russell/blog.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/perl/2023/07/23
\ No newline at end of file diff --git a/challenge-226/adam-russell/blog1.txt b/challenge-226/adam-russell/blog1.txt new file mode 100644 index 0000000000..3185c08d51 --- /dev/null +++ b/challenge-226/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/prolog/2023/07/23
\ No newline at end of file diff --git a/challenge-226/adam-russell/perl/ch-1.pl b/challenge-226/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..e9bcb5a792 --- /dev/null +++ b/challenge-226/adam-russell/perl/ch-1.pl @@ -0,0 +1,17 @@ +use v5.38; +## +# You are given a string and an array of indices of same length as string. +# Write a script to return the string after re-arranging the indices in the correct order. +## +sub shuffle_string{ + my($s, $indices) = @_; + my @s = split(//, $s); + my @t; + do { $t[$_] = shift @s } for @{$indices}; + return join(q//, @t); +} + +MAIN:{ + say shuffle_string(q/lacelengh/, [3, 2, 0, 5, 4, 8, 6, 7, 1]); + say shuffle_string(q/rulepark/, [4, 7, 3, 1, 0, 5, 2, 6]); +}
\ No newline at end of file diff --git a/challenge-226/adam-russell/perl/ch-2.pl b/challenge-226/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..640df82bb4 --- /dev/null +++ b/challenge-226/adam-russell/perl/ch-2.pl @@ -0,0 +1,21 @@ +use v5.38; +## +# You are given an array of non-negative integers, @ints. +# Write a script to return the minimum number of operations to make every element equal +# zero. +## +sub zero_array{ + my $operations = 0; + do{ + return $operations if 0 == unpack(q/%32I*/, pack(q/I*/, @_)); + my $minimum = (sort { $a <=> $b } grep { $_ > 0 } @_)[0]; + @_ = map { $_ > 0 ? $_ - $minimum : 0 } @_; + $operations++; + } for @_; +} + +MAIN:{ + say zero_array 1, 5, 0, 3, 5; + say zero_array 0; + say zero_array 2, 1, 4, 0, 3 +}
\ No newline at end of file diff --git a/challenge-226/adam-russell/prolog/ch-1.p b/challenge-226/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..c944978abd --- /dev/null +++ b/challenge-226/adam-russell/prolog/ch-1.p @@ -0,0 +1,2 @@ +letter_shuffle(Shuffled, Letter, Index):- + nth(Index, Shuffled, Letter).
\ No newline at end of file diff --git a/challenge-226/adam-russell/prolog/ch-2.p b/challenge-226/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..65102ac1a0 --- /dev/null +++ b/challenge-226/adam-russell/prolog/ch-2.p @@ -0,0 +1,14 @@ +subtract_minimum(Minimum, X, Y):- + Y is X - Minimum. + +zero_array(Numbers, Operations):- + delete(Numbers, 0, NumbersNoZero), + zero_array(NumbersNoZero, 0, Operations). +zero_array([], Operations, Operations). +zero_array(Numbers, OperationsCounter, Operations):- + delete(Numbers, 0, NumbersNoZero), + min_list(NumbersNoZero, Minimum), + maplist(subtract_minimum(Minimum), NumbersNoZero, NumbersSubtracted), + succ(OperationsCounter, OperationsCounterNext), + delete(NumbersSubtracted, 0, NumbersSubtractedNoZero), + zero_array(NumbersSubtractedNoZero, OperationsCounterNext, Operations).
\ No newline at end of file |
