From 98dad63518b32c2e05f8934440b5d10cb697b9af Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 4 Sep 2022 19:27:37 +0100 Subject: - Added solutions by Steven Wilson. --- challenge-180/steven-wilson/perl/ch-01.pl | 33 ------------------------------- challenge-180/steven-wilson/perl/ch-02.pl | 28 -------------------------- challenge-180/steven-wilson/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++ challenge-180/steven-wilson/perl/ch-2.pl | 28 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 61 deletions(-) delete mode 100644 challenge-180/steven-wilson/perl/ch-01.pl delete mode 100644 challenge-180/steven-wilson/perl/ch-02.pl create mode 100644 challenge-180/steven-wilson/perl/ch-1.pl create mode 100644 challenge-180/steven-wilson/perl/ch-2.pl (limited to 'challenge-180') diff --git a/challenge-180/steven-wilson/perl/ch-01.pl b/challenge-180/steven-wilson/perl/ch-01.pl deleted file mode 100644 index 60bf86968e..0000000000 --- a/challenge-180/steven-wilson/perl/ch-01.pl +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env perl -# Week 180 Task 1 -# Write a script to find out the first unique character in the given string -# and print its index (0-based). - -use strict; -use warnings; -use Test::More; - -ok(first_unique_character("Perl Weekly Challenge") == 0, "First"); -ok(first_unique_character("Long Live Perl") == 1, "Second"); -ok(first_unique_character("LOLOLx") == 5, "Last"); -ok(!defined(first_unique_character("LOLO")), "None"); -done_testing(); - -sub first_unique_character { - my $s = shift; - my $len = length $s; - my $index = 0; - while ( $index < $len) { - my $c = substr $s, $index, 1; - my $number = () = $s =~ /$c/gi; - if ( $number == 1){ - last; - } - $index++; - if ($index == $len) { - undef $index; - last; - } - } - return $index; -} \ No newline at end of file diff --git a/challenge-180/steven-wilson/perl/ch-02.pl b/challenge-180/steven-wilson/perl/ch-02.pl deleted file mode 100644 index f68652e646..0000000000 --- a/challenge-180/steven-wilson/perl/ch-02.pl +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env perl -# Week 180 Task 2 -# You are given list of numbers, @n and an integer $i. -# Write a script to trim the given list where element is less than or equal to -# the given integer. - -use strict; -use warnings; -use Test::More; - -my @n_t1 = ( 1, 4, 2, 3, 5 ); -my $i_t1 = 3; -my @r_t1 = ( 4, 5 ); - -my @n_t2 = ( 9, 0, 6, 2, 3, 8, 5 ); -my $i_t2 = 4; -my @r_t2 = ( 9, 6, 8, 5 ); - -is_deeply( trim_list( $i_t1, \@n_t1 ), \@r_t1 ); -is_deeply( trim_list( $i_t2, \@n_t2 ), \@r_t2 ); -done_testing(); - -sub trim_list { - my $i = shift; - my $n_ref = shift; - my @r = grep { $_ > $i } @{$n_ref}; - return \@r; -} diff --git a/challenge-180/steven-wilson/perl/ch-1.pl b/challenge-180/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..60bf86968e --- /dev/null +++ b/challenge-180/steven-wilson/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +# Week 180 Task 1 +# Write a script to find out the first unique character in the given string +# and print its index (0-based). + +use strict; +use warnings; +use Test::More; + +ok(first_unique_character("Perl Weekly Challenge") == 0, "First"); +ok(first_unique_character("Long Live Perl") == 1, "Second"); +ok(first_unique_character("LOLOLx") == 5, "Last"); +ok(!defined(first_unique_character("LOLO")), "None"); +done_testing(); + +sub first_unique_character { + my $s = shift; + my $len = length $s; + my $index = 0; + while ( $index < $len) { + my $c = substr $s, $index, 1; + my $number = () = $s =~ /$c/gi; + if ( $number == 1){ + last; + } + $index++; + if ($index == $len) { + undef $index; + last; + } + } + return $index; +} \ No newline at end of file diff --git a/challenge-180/steven-wilson/perl/ch-2.pl b/challenge-180/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..f68652e646 --- /dev/null +++ b/challenge-180/steven-wilson/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +# Week 180 Task 2 +# You are given list of numbers, @n and an integer $i. +# Write a script to trim the given list where element is less than or equal to +# the given integer. + +use strict; +use warnings; +use Test::More; + +my @n_t1 = ( 1, 4, 2, 3, 5 ); +my $i_t1 = 3; +my @r_t1 = ( 4, 5 ); + +my @n_t2 = ( 9, 0, 6, 2, 3, 8, 5 ); +my $i_t2 = 4; +my @r_t2 = ( 9, 6, 8, 5 ); + +is_deeply( trim_list( $i_t1, \@n_t1 ), \@r_t1 ); +is_deeply( trim_list( $i_t2, \@n_t2 ), \@r_t2 ); +done_testing(); + +sub trim_list { + my $i = shift; + my $n_ref = shift; + my @r = grep { $_ > $i } @{$n_ref}; + return \@r; +} -- cgit