diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-09-04 21:37:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-04 21:37:21 +0100 |
| commit | 0566bf490691a435f62f1db31316a063c8b600ae (patch) | |
| tree | af0c844338d80522aa0844247b38f4d09ce877f0 | |
| parent | a5bbae20d74b8668b234bfa9d6080058d5f0e344 (diff) | |
| parent | 399235c024d7103edb1b9a5ceb86b31193ec1811 (diff) | |
| download | perlweeklychallenge-club-0566bf490691a435f62f1db31316a063c8b600ae.tar.gz perlweeklychallenge-club-0566bf490691a435f62f1db31316a063c8b600ae.tar.bz2 perlweeklychallenge-club-0566bf490691a435f62f1db31316a063c8b600ae.zip | |
Merge pull request #6695 from adamcrussell/challenge-180
initial commit
| -rw-r--r-- | challenge-180/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-180/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-180/adam-russell/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-180/adam-russell/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-180/adam-russell/prolog/ch-1.p | 21 | ||||
| -rw-r--r-- | challenge-180/adam-russell/prolog/ch-2.p | 10 |
6 files changed, 90 insertions, 0 deletions
diff --git a/challenge-180/adam-russell/blog.txt b/challenge-180/adam-russell/blog.txt new file mode 100644 index 0000000000..3d8f10dcad --- /dev/null +++ b/challenge-180/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/09/04
\ No newline at end of file diff --git a/challenge-180/adam-russell/blog1.txt b/challenge-180/adam-russell/blog1.txt new file mode 100644 index 0000000000..f786ed74cc --- /dev/null +++ b/challenge-180/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/09/04
\ No newline at end of file diff --git a/challenge-180/adam-russell/perl/ch-1.pl b/challenge-180/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..96c7080b0d --- /dev/null +++ b/challenge-180/adam-russell/perl/ch-1.pl @@ -0,0 +1,17 @@ +use v5.36; +use strict; +use warnings; +## +# You are given a string, $s. Write a script to find out the first unique character in +# the given string and print its index (0-based). +## +sub index_first_unique{ + my($s) = @_; + my @s = split(//, $s); + map {my $i = $_; my $c = $s[$i]; return $_ if 1 == grep {$c eq $_ } @s } 0 .. @s - 1; +} + +MAIN:{ + say index_first_unique(q/Perl Weekly Challenge/); + say index_first_unique(q/Long Live Perl/); +}
\ No newline at end of file diff --git a/challenge-180/adam-russell/perl/ch-2.pl b/challenge-180/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..d8f5d77b94 --- /dev/null +++ b/challenge-180/adam-russell/perl/ch-2.pl @@ -0,0 +1,40 @@ +use v5.36; +use strict; +use warnings; +## +# You are given list of numbers, @n and an integer $i. Write a script to trim the given +# list when an element is less than or equal to the given integer. +## +sub trimmer{ + my($i) = @_; + return sub{ + my($x) = @_; + return $x if $x > $i; + } +} + +sub trim_list_r{ + my($n, $trimmer, $trimmed) = @_; + $trimmed = [] unless $trimmed; + return @$trimmed if @$n == 0; + my $x = pop @$n; + $x = $trimmer->($x); + unshift @$trimmed, $x if $x; + trim_list_r($n, $trimmer, $trimmed); +} + +sub trim_list{ + my($n, $i) = @_; + my $trimmer = trimmer($i); + return trim_list_r($n, $trimmer); +} + +MAIN:{ + my(@n, $i); + $i = 3; + @n = (1, 4, 2, 3, 5); + say join(", ", trim_list(\@n, $i)); + $i = 4; + @n = (9, 0, 6, 2, 3, 8, 5); + say join(", ", trim_list(\@n, $i)); +}
\ No newline at end of file diff --git a/challenge-180/adam-russell/prolog/ch-1.p b/challenge-180/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..33ab763c9b --- /dev/null +++ b/challenge-180/adam-russell/prolog/ch-1.p @@ -0,0 +1,21 @@ +index_first_unique(Words, IndexUnique):- + index_first_unique(Words, 0, IndexUnique). +index_first_unique(String, I, IndexUnique):- + succ(I, Index), + length(String, Length), + nth(Index, String, Character), + delete(String, Character, Deleted), + length(Deleted, LengthDeleted), + LengthDifference is Length - LengthDeleted, + LengthDifference == 1, !, + IndexUnique = I. +index_first_unique(String, I, IndexUnique):- + succ(I, Index), + length(String, Length), + nth(Index, String, Character), + delete(String, Character, Deleted), + length(Deleted, LengthDeleted), + LengthDifference is Length - LengthDeleted, + \+ LengthDifference == 1, + succ(I, X), + index_first_unique(String, X, IndexUnique).
\ No newline at end of file diff --git a/challenge-180/adam-russell/prolog/ch-2.p b/challenge-180/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..f16b3ec65e --- /dev/null +++ b/challenge-180/adam-russell/prolog/ch-2.p @@ -0,0 +1,10 @@ +trimmer(X, Y, Z):- + X < Y, + Z = Y. +trimmer(X, Y, Z):- + X >= Y, + Z = 'trimmed'. + +trim_list(Numbers, I, TrimmedList):- + maplist(trimmer(I), Numbers, PartialTrimmedList), + delete(PartialTrimmedList, 'trimmed', TrimmedList). |
