TASK #1: Sort Letters
You are given two arrays, @letters and @weights.
Write a script to sort the given array @letters based on the @weights.
#!/usr/bin/perl use strict; use warnings; sub sort_letters { my @result; for (my $i = 0; $i < scalar(@{$_[0]}); $i++) { # construct @result by assigning values to elements in a non-sequential order $result[$_[1]->[$i]-1] = $_[0]->[$i]; } return(join("", @result, "\n")); } # Tests my @letters; my @weights; # Example 1 @letters = ('R', 'E', 'P', 'L'); @weights = (3, 2, 1, 4); print(sort_letters(\@letters, \@weights), "\n");# Output: PERL # Example 2 @letters = ('A', 'U', 'R', 'K'); @weights = (2, 4, 1, 3); print(sort_letters(\@letters, \@weights), "\n");# Output: RAKU # Example 3 @letters = ('O', 'H', 'Y', 'N', 'P', 'T'); @weights = (5, 4, 2, 6, 1, 3); print(sort_letters(\@letters, \@weights), "\n");# Output: PYTHON
#!/usr/bin/perl use strict; use warnings; sub split_string { # this task can be solved in an easy way: # if the number of vowels is even, the string can be split as required # extra test if the string contains at least one vowel ($_[0] =~ (tr/aeiou//) && $_[0] =~ (tr/aeiou//) % 2 == 0) ? return("true") : return("false"); } # Tests my $str; # Example 1 $str = "perl"; print(split_string($str), "\n");# Output: false # Example 2 $str = "book"; print(split_string($str), "\n");# Output: true # Example 3 $str = "good morning"; print(split_string($str), "\n");# Output: true # Example 4 $str = "bcdfghjklmnpqrstvwxyz"; print(split_string($str), "\n");# Output: false