aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-02 19:49:27 +0100
committerGitHub <noreply@github.com>2020-10-02 19:49:27 +0100
commit95b3ea7ad7a2b3e2a7438e3af8dfd91b82529a93 (patch)
tree8c2453fca72f1212703a1d21515e0a43b6d73c30
parent24d8b6971eeeb1966a91789f93e03ad45245afff (diff)
parentb6cea1ad82f93cb3cd212d4743639b2a583a777c (diff)
downloadperlweeklychallenge-club-95b3ea7ad7a2b3e2a7438e3af8dfd91b82529a93.tar.gz
perlweeklychallenge-club-95b3ea7ad7a2b3e2a7438e3af8dfd91b82529a93.tar.bz2
perlweeklychallenge-club-95b3ea7ad7a2b3e2a7438e3af8dfd91b82529a93.zip
Merge pull request #2431 from oWnOIzRi/week80
add solution for week 80 task 1
-rw-r--r--challenge-080/steven-wilson/perl/ch-1.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-080/steven-wilson/perl/ch-1.pl b/challenge-080/steven-wilson/perl/ch-1.pl
new file mode 100644
index 0000000000..af4bd07338
--- /dev/null
+++ b/challenge-080/steven-wilson/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+# Solution by Steven Wilson 2nd Oct 2020.
+
+# TASK #1 › Smallest Positive Number
+# Submitted by: Mohammad S Anwar
+#
+# You are given unsorted list of integers @N.
+#
+# Write a script to find out the smallest positive number missing.
+# Example 1:
+#
+# Input: @N = (5, 2, -2, 0)
+# Output: 1
+#
+# Example 2:
+#
+# Input: @N = (1, 8, -1)
+# Output: 2
+#
+# Example 3:
+#
+# Input: @N = (2, 0, -1)
+# Output: 1
+
+use strict;
+use warnings;
+use feature qw/ say /;
+use Test::More;
+use List::MoreUtils qw/ firstidx /;
+
+my @N1_t = ( 5, 2, -2, 0 );
+my @N2_t = ( 1, 8, -1 );
+my @N3_t = ( 2, 0, -1 );
+my @N4_t = ( 5, 2, -2, 0, 1, 3, 4, 6 );
+my @N5_t = ();
+ok( get_smallest_positive_number_missing( \@N1_t ) == 1, "1 missing" );
+ok( get_smallest_positive_number_missing( \@N2_t ) == 2, "1 present" );
+ok( get_smallest_positive_number_missing( \@N3_t ) == 1, "1 missing" );
+ok( get_smallest_positive_number_missing( \@N4_t ) == 7, "larger than max" );
+ok( get_smallest_positive_number_missing( \@N5_t ) == 1, "empty list" );
+done_testing();
+
+sub get_smallest_positive_number_missing {
+ my $unsorted_list_ref = shift;
+ my @sorted_list = sort { $a <=> $b } @{$unsorted_list_ref};
+ my $index = firstidx { $_ == 1 } @sorted_list;
+ my $answer = 1;
+ if ( $index >= 0 ) {
+ my $counter = 1;
+ while ( defined $sorted_list[ $index + ( $counter - 1 ) ]
+ && $sorted_list[ $index + ( $counter - 1 ) ] == ($counter) )
+ {
+ $counter++;
+ }
+ $answer = $counter;
+ }
+ return $answer;
+}
+