diff options
| -rw-r--r-- | challenge-250/solathian/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-250/solathian/ch-2.pl | 35 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-250/solathian/ch-1.pl b/challenge-250/solathian/ch-1.pl new file mode 100644 index 0000000000..229535f1b1 --- /dev/null +++ b/challenge-250/solathian/ch-1.pl @@ -0,0 +1,31 @@ +#!usr/bin/perl +use v5.38; +use builtin qw(indexed); +no warnings "experimental"; + + +# Challenge 250 - 1 - Smallest Index +# You are given an array of integers, @ints. +# Write a script to find the smallest index i such that i mod 10 == $ints[i] otherwise return -1. + + +small(0,1,2); # 0 +small(4, 3, 2, 1); # 2 +small(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); # -1 + +sub small(@array) +{ + my $returnVal = -1; + + foreach my ($index, $elem) (indexed @array) + { + if($index % 10 == $elem) + { + $returnVal = $index; + last; + } + } + + say $returnVal; + return $returnVal; +}
\ No newline at end of file diff --git a/challenge-250/solathian/ch-2.pl b/challenge-250/solathian/ch-2.pl new file mode 100644 index 0000000000..052fffe081 --- /dev/null +++ b/challenge-250/solathian/ch-2.pl @@ -0,0 +1,35 @@ +#!usr/bin/perl +use v5.38; + +# Challenge 250 - 2 - Alphanumeric String Value +# You are given an array of alphanumeric strings. +# Write a script to return the maximum value of alphanumeric string in the given array. +# The value of alphanumeric string can be defined as + +# a) The numeric representation of the string in base 10 if it is made up of digits only. +# b) otherwise the length of the string + + +asv("perl", "2", "000", "python", "r4ku"); +asv("001", "1", "000", "0007"); + + +sub asv(@list) +{ + my $max = 0; + + foreach my $elem (@list) + { + # if it only has numbers + if($elem =~ m"^\d+$") + { + $max = eval $elem if( $elem > $max ); + } + else + { + $max = length $elem if( length $elem > $max ); + } + } + + say $max; +}
\ No newline at end of file |
