1. What is the longest word in the string 'If music be the food of love play on.'
$text = "If music be the food of love play on.";
@words = (split( /\s+/, $text));
@words_sort = sort ({length($a) <=> length($b)} @words);
print("Longest word: $words_sort[-1]\n");
print("\n" . "-" x 35, "\n");
2. Find the intersection of two arrays with the numbers 1,2,3,4,5 and 3,4,5,6,7.
4. Pass this list ab, abc, abcd, abcde, abcdef, abcdefg, abcdefgh as an anonymous array to a function that determines how many items have an even number of characters.
sub count_even_words {
scalar( grep { !(length($_) % 2) } @{$_[0]} );
}
print("Number of even character items: " . count_even_words([qw(ab abc abcd abcde abcdef abcdefg abcdefgh)]) . "\n");
5. Pass this list 12, 123, 1234, 12345, 123456, 1234567, 12345678 as an anonymous array to a function that determines how many items have an even number of digits.
sub count_even_digits {
scalar( grep { !(split('',$_) % 2) } @{$_[0]} );
}
print("Number of even digits: " . count_even_digits([qw(12, 123, 1234, 12345, 123456, 1234567, 12345678)]) . "\n");
6. Given a hash where keys and values are numbers. Find all keys where key or value > 50
7. Rolling a 6-sided dice 20 times results in the following data: 5 1 2 1 3 3 1 4 3 1 2 1 3 4 4 5 6 2 3 1. Print a frequency table where the frequency of each number is represented by an asterisk.