diff options
| author | DevSanti12 <santiagoleyva2013@gmail.com> | 2024-07-22 00:21:41 -0500 |
|---|---|---|
| committer | DevSanti12 <santiagoleyva2013@gmail.com> | 2024-07-22 00:21:41 -0500 |
| commit | f5a4ca161927ab97014f95e1854269feea4cb396 (patch) | |
| tree | 5dd06a43880ce475aba064e73717f24d0414fa88 | |
| parent | f59c17748d25a6f16a7e64cc3568ae4190f9f3fb (diff) | |
| download | perlweeklychallenge-club-f5a4ca161927ab97014f95e1854269feea4cb396.tar.gz perlweeklychallenge-club-f5a4ca161927ab97014f95e1854269feea4cb396.tar.bz2 perlweeklychallenge-club-f5a4ca161927ab97014f95e1854269feea4cb396.zip | |
added challenges for week 278, a bit late lol
| -rw-r--r-- | challenge-278/santiago-leyva/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-278/santiago-leyva/perl/ch-2.pl | 51 |
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-278/santiago-leyva/perl/ch-1.pl b/challenge-278/santiago-leyva/perl/ch-1.pl new file mode 100644 index 0000000000..79453e73a9 --- /dev/null +++ b/challenge-278/santiago-leyva/perl/ch-1.pl @@ -0,0 +1,43 @@ +=begin +You are given a shuffle string, $str. + +Write a script to return the sorted string. + +** A string is shuffled by appending word position to each word. + +Input: $str = "and2 Raku3 cousins5 Perl1 are4" +Output: "Perl and Raku are cousins" + +Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7" +Output: "Python is the most popular guest language" + +Input: $str = "Challenge3 The1 Weekly2" +Output: "The Weekly Challenge" + +=cut + +use strict; +use warnings; + +my @strings = ("and2 Raku3 cousins5 Perl1 are4", + "guest6 Python1 most4 the3 popular5 is2 language7", + "Challenge3 The1 Weekly2"); + +foreach(@strings){ + wordSort($_); +} + +sub wordSort { + + my $str = shift; + my @array = split(m/\s+/,$str); + my @sort; + + foreach(@array){ + my ($word,$num) = $_ =~ /(\w+)(\d+)/g; + @sort[$num-1] = $word; + } + + print join(" ",@sort)."\n"; + +}
\ No newline at end of file diff --git a/challenge-278/santiago-leyva/perl/ch-2.pl b/challenge-278/santiago-leyva/perl/ch-2.pl new file mode 100644 index 0000000000..b8c191aa43 --- /dev/null +++ b/challenge-278/santiago-leyva/perl/ch-2.pl @@ -0,0 +1,51 @@ +=begin +You are given a word, $word and a character, $char. + +Write a script to replace the substring up to and including $char with its characters sorted alphabetically. If the $char doesn’t exist then DON'T do anything. + +Example 1 +Input: $str = "challenge", $char = "e" +Ouput: "acehllnge" +Example 2 +Input: $str = "programming", $char = "a" +Ouput: "agoprrmming" +Example 3 +Input: $str = "champion", $char = "b" +Ouput: "champion" + +=cut + +use strict; +use warnings; +use Data::Dumper; + +my @words = (["challenge","e"], + ["programming","a"], + ["champion","b"] + ); + +foreach my $i (@words){ + replaceSubstr($i); +} + +sub replaceSubstr { + + my $input = $_[0]; + my @str = @$input; + my @word = split("",$str[0]); + my @sorted; + my $char = $str[1]; + my $i = 0; + my $flag = 0; + while(($i < (scalar @word))){ + if($word[$i] eq $char){ $flag = 1; last;} + push @sorted, $word[$i]; + $i++; + } + @sorted = sort @sorted; + if($flag == 1){ + splice(@word,0,$i,@sorted); + } + print join("",@word)."\n"; + +}
\ No newline at end of file |
