From 350f495b09a4b6b6db8506533e2dccc68d9fc3f4 Mon Sep 17 00:00:00 2001 From: DevSanti12 Date: Thu, 25 Jul 2024 23:56:51 -0500 Subject: added perl challenges for #279 --- challenge-279/santiago-leyva/perl/ch-01.pl | 46 ++++++++++++++++++++++++++++++ challenge-279/santiago-leyva/perl/ch-02.pl | 46 ++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 challenge-279/santiago-leyva/perl/ch-01.pl create mode 100644 challenge-279/santiago-leyva/perl/ch-02.pl diff --git a/challenge-279/santiago-leyva/perl/ch-01.pl b/challenge-279/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..51f8683eb8 --- /dev/null +++ b/challenge-279/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,46 @@ +=begin + +You are given two arrays, @letters and @weights. + +Write a script to sort the given array @letters based on the @weights. + +Example 1 +Input: @letters = ('R', 'E', 'P', 'L') + @weights = (3, 2, 1, 4) +Output: PERL +Example 2 +Input: @letters = ('A', 'U', 'R', 'K') + @weights = (2, 4, 1, 3) +Output: RAKU +Example 3 +Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') + @weights = (5, 4, 2, 6, 1, 3) +Output: PYTHON + +=cut + +use strict; +use Data::Dumper; + +my @letters = (['R', 'E', 'P', 'L'],['A', 'U', 'R', 'K'],['O', 'H', 'Y', 'N', 'P', 'T']); +my @weights = ([3, 2, 1, 4],[2, 4, 1, 3],[5, 4, 2, 6, 1, 3]); + +for(0..(scalar @weights)-1){ + sortArray($letters[$_],$weights[$_]); +} + +sub sortArray { + my $let = shift; + my $wgt = shift; + my @letters_ = @$let; + my @weights_ = @$wgt; + my %map; + my $n = scalar(@weights_); + foreach my $i (0..$n){ + $map{$weights_[$i]} = $letters_[$i]; + } + foreach(sort(keys %map)){ + print $map{$_}; + } + print "\n"; +} \ No newline at end of file diff --git a/challenge-279/santiago-leyva/perl/ch-02.pl b/challenge-279/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..13c9e64f08 --- /dev/null +++ b/challenge-279/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,46 @@ +=begin + +You are given a string, $str. + +Write a script to split the given string into two containing exactly same number of vowels and return true if you can otherwise false. + +Example 1 +Input: $str = "perl" +Ouput: false + +Example 2 +Input: $str = "book" +Ouput: true +Two possible strings "bo" and "ok" containing exactly one vowel each. + +Example 3 +Input: $str = "good morning" +Ouput: true +Two possible strings "good " and "morning" containing two vowels each or "good m" and "orning" containing two vowels each. + +=cut +use strict; +use warnings; +use Data::Dumper; + +my @strings = (["perl"],["book"],["good morning"]); + +foreach(@strings){ + print vowelCheck($_)."\n"; +} + +sub vowelCheck { + my $string_ = shift; + my $string = @$string_[0]; + my @A = split("",$string); + my @vowels; + + for(my $i=0;$i<(scalar @A); $i++){ + if($A[$i] =~ /[aeiouAEIOU]/){ + push @vowels,$A[$i]; + } + } + + return "True" if(scalar @vowels % 2 == 0); + return "False" if(scalar @vowels % 2 != 0); +} \ No newline at end of file -- cgit