1. Count vowels in 'Linguam refrenans temperet ne litis horror insonet, visum fovendo'
print("\n"); print("-" x 35, "\n"); print("Challenge 1: count vowels\n"); print("-" x 35, "\n"); $str = "Linguam refrenans temperet ne litis\nhorror insonet, visum fovendo"; print("$str\n\n"); %vowel_count = (); for ($i = 0; $i < length($str); $i++) { $char = lc( substr( $str, $i, 1 ) ); $vowel_count{$char}++ if ( $char =~ /[aeiou]/ ); } foreach $vowel (sort( keys(%vowel_count) )) { print("$vowel => $vowel_count{$vowel}\n"); } print("\n" . "-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 2: average of each student's math scores\n"); print("-" x 35, "\n"); @AoA = ( ['Sebastian', 45, 60, 72], ['Daniel', 55, 65, 70], ['Florence', 71, 79, 78], ); for($i = 0; $i < scalar(@AoA); $i++) { print("Average math score student " . $AoA[$i][0] . ": " . eval ( ($AoA[$i][1] + $AoA[$i][2] + $AoA[$i][3]) / (scalar(@{@AoA[$i]} -1)) ) . "\n"); } print("\n" . "-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 3: which sentence has the maximum number of words? 'The sun is very bright.', 'There are only five students here today.', 'I’d love that.' , 'I would like to be there\n"); print("-" x 35, "\n"); $array_no = -1; sub find_max_words { ($list) = @_; $max_words = 0; foreach $sentence (@$list) { $sentence =~ s/\h+/ /g; @words = split(/\s{1}/, $sentence); $count = scalar(@words); if ($count > $max_words) { $max_words = $count; $array_no++; } } return $max_words; } @s = ('The sun is very bright.', 'There are only five students here today.', 'I’d love that.' , 'I would like to be there'); print("Maximum number of words is " . find_max_words(\@s) . " in the sentence '" . $s[$array_no] . "'\n"); print("\n" . "-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 4: typo-generator IV: wrong letter when typing 'Reinier'\n"); print("-" x 35, "\n"); $str = "Reinier"; %HoA = ( 'R' => ['E','T','G','F','D'], 'r' => ['e','t','g','f','d'], 'e' => ['w','r','f','d','s'], 'i' => ['u','o','l','k','j'], 'n' => ['b','m','h','j','k'] ); for ($i = 0; $i < length($str); $i++) { $char = substr($str, $i, 1); @replace = @{$HoA{$char}}; for ($y = 0; $y < scalar(@replace); $y++) { substr($str, $i, 1, @replace[$y]); print $str . "\n"; $str = "Reinier"; } } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 5: typo-generator V: extra letter when typing 'Reinier'\n"); print("-" x 35, "\n"); $str = "Reinier"; %HoA = ( 'R' => ['E','T','G','F','D'], 'r' => ['e','t','g','f','d'], 'e' => ['w','r','f','d','s'], 'i' => ['u','o','l','k','j'], 'n' => ['b','m','h','j','k'] ); for ($i = 0; $i < length($str); $i++) { $char = substr($str, $i, 1); @replace = @{$HoA{$char}}; for ($y = 0; $y < scalar(@replace); $y++) { substr($str, $i, 0, @replace[$y]); print $str . "\n"; $str = "Reinier"; } } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 6: Find the common numbers of two arrays, using a function and references\n"); print("-" x 35, "\n"); sub intersection { ($f, $s) = @_; @result = (); foreach $item_1 (@$f) { foreach $item_2 (@$s) { if(defined($item_2) && $item_1 eq $item_2) { push(@result, $item_2); undef($item_2); last; } } } return @result; } @one = (1..10); @two = (8..15); intersection(\@one, \@two); print("@result\n"); # 8 9 10 print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 7: Create @array_of_arrays and store references to @array1, @array2, and @array3 within it. Each array consists of three numbers. Print the third number of the second array.\n"); print("-" x 35, "\n"); @array1 = (1, 2, 3); @array2 = (4, 5, 6); @array3 = (7, 8, 9); @array_of_arrays = (\@array1, \@array2, \@array3); print( $array_of_arrays[1][2] ); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 8: Create @AoA and store references to two anonymous arrays within it. Each array consists of three names. Print the third name of the first array and print all names.\n"); print("-" x 35, "\n"); @AoA = ( [ qw( Sebastian Daniel Florence ) ], [ qw( Kirsten Bibian Niki ) ], ); print("The name of my third child is: "); print( $AoA[0]->[2] ); print("\n"); foreach $aoa_ref (@AoA) { print("The names of all my children are: "); print("$_ ") foreach (@$aoa_ref ); print("\n"); } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 9: Store and print information about employees in a company. Each employee has a unique employee ID, and for each employee, we want to store their name, department, and salary. You should use a hash of hashes to represent this data structure.\n"); print("-" x 35, "\n"); %employees = (); $employees{1001} = { name => "John Doe", department => "Engineering", salary => 60000 }; $employees{1002} = { name => "Jane Smith", department => "Marketing", salary => 55000 }; $employees{1003} = { name => "Alice Johnson", department => "Human Resources", salary => 50000 }; foreach $emp_id (keys %employees) { print "Employee ID: $emp_id\n"; print "Name: " . $employees{$emp_id}{name} . "\n"; print "Department: " . $employees{$emp_id}{department} . "\n"; print "Salary: \$" . $employees{$emp_id}{salary} . "\n"; print "\n"; } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 10: Store and print information about three books (title, author, genre, year of publication). Use push to add data for three books to the array of hashes.\n"); print("-" x 35, "\n"); @library = (); push @library, { title => "The Great Gatsby", author => "F. Scott Fitzgerald", genre => "Fiction", publication_year => 1925 }; push @library, { title => "To Kill a Mockingbird", author => "Harper Lee", genre => "Fiction", publication_year => 1960 }; push @library, { title => "1984", author => "George Orwell", genre => "Dystopian", publication_year => 1949 }; foreach $book (@library) { print "Title: " . $book->{title} . "\n"; print "Author: " . $book->{author} . "\n"; print "Genre: " . $book->{genre} . "\n"; print "Publication Year: " . $book->{publication_year} . "\n"; print "\n"; } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 11: Store and print information about students in a school. Each student has a name and a list of subjects they are studying. Use a hash of arrays.\n"); print("-" x 35, "\n"); %students = (); $students{"Alice"} = ["Math", "Physics", "Chemistry"]; $students{"Bob"} = ["English", "History"]; $students{"Charlie"} = ["Biology", "Geography", "Art"]; $students{"David"} = ["Computer Science"]; foreach $student (keys %students) { print "Student: $student\n"; print "Subjects: " . join(", ", @{$students{$student}}) . "\n"; print "\n"; } print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 12: Read all lines under __DATA__ sequentially. All lines contains 3 semicolon separated integers. Use 'split' and 'push' to add them as references to anonymous arrays into @matrix. Print @matrix.\n"); print("-" x 35, "\n"); @matrix = (); @lines = <DATA>; chomp(@lines); foreach $line (@lines) { @split = split(';', $line); push @matrix, [ @split ]; } foreach $AoA_ref (@matrix) { print("$_ ") foreach ( @$AoA_ref ); print("\n"); } __DATA__ 1;2;3 4;5;6 7;8;9 print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 13: Print the length of a hash that has some key value pairs.\n"); print("-" x 35, "\n"); %data = ( 'a' => 1, 'b' => 2, 'c' => 3, ); $length = keys %data; print("$length\n"); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 14: Add an element to the hash of the previous challenge and print the new length.\n"); print("-" x 35, "\n"); %data = ( 'a' => 1, 'b' => 2, 'c' => 3, ); $data{'d'} = 4; $length = keys %data; print("$length\n"); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 15: Remove the second element from the hash of the previous challenge and print the new length.\n"); print("-" x 35, "\n"); %data = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, ); delete $data{'b'}; $length = keys %data; print("$length\n"); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 16: Pass a hash to a function and print its keys and values in sorted order.\n"); print("-" x 35, "\n"); sub f { (%hash) = @_; foreach $item (sort keys(%hash)) { print("$item => $hash{$item}\n"); } } %data = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, ); f(%data); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 17: Pass a hash as reference to a function and print its keys and values in sorted order.\n"); print("-" x 35, "\n"); sub f { ($hash_ref) = @_; foreach $item (sort keys(%$hash_ref)) { print("$item => " . %{$hash_ref}{$item} . "\n"); } } %data = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, ); f(\%data); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 18: Call a function by reference. Use the previous challenge.\n"); print("-" x 35, "\n"); sub f { ($hash_ref) = @_; foreach $item (sort keys(%$hash_ref)) { print("$item => " . %{$hash_ref}{$item} . "\n"); } } %data = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, ); $ref_f = \&f; &$ref_f(\%data); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 19: Pass to a function: an integer, a reference to an array, a hash, and a variable. Print their values.\n"); print("-" x 35, "\n"); sub f { for ( $i = 0; $i <= $#_; $i++ ) $#_ is the index of the last element in the array @_ { if (ref($_[$i]) eq "ARRAY") { print("Argument nr. $i: @{$_[$i]} \n"); } else { print("Argument nr. $i: " . $_[$i] ."\n"); } } } $x = 14; @array = qw( a b c ); %hash = ('a' => 1, 'b' => 2); f ( 5, \@array, %hash, $x );# Notice that passing the array (not as reference) would give the values a, b and c as separate results print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 20: Pass to a function: an integer, an array, a reference to a hash, and a variable. Print their values.\n"); print("-" x 35, "\n"); sub f { for ( $i = 0; $i <= $#_; $i++ ) $#_ is the index of the last element in the array @_ { if (ref($_[$i]) eq "HASH") { print("Argument nr. $i: @{[ (%{$_[$i]}) ]} \n");# [] creates reference to anonymous array; @{} dereferences this array; result: the hash is printed as a list! } else { print("Argument nr. $i: " . $_[$i] ."\n"); } } } $x = 14; @array = qw( a b c ); %hash = ('a' => 1, 'b' => 2); f ( 5, @array, \%hash, $x );# See previous challenge for passing the hash not as reference print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 21: Read three names: a first name and family name, separated by one space (e.g. Paul Auster). Print family name comma first name (e.g. Auster, Paul) in sorted order.\n"); print("-" x 35, "\n"); %names = (); while (<DATA>) { chomp; ($fn, $ln) = split(' ', $_); $names{$ln} = $fn; } foreach $n ( sort( keys(%names) ) ) { print("$n, $names{$n}\n"); } __DATA__ Umberto Eco Albert Camus Paul Auster print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 22: Variant of challenge 21. Read three names: a first name, second name or initials (optional) and family name, separated by one space (e.g. Jean Paul Sartre). Print family name comma first name, second name/initial (e.g. Sarte, Jean Paul) in sorted order.\n"); print("-" x 35, "\n"); %names = (); while (<DATA>) { chomp; @fn_ln = split(' ', $_); if (scalar(@fn_ln) == 2) { # e.g. Michel Foucault $fn = $fn_ln[0]; $ln = $fn_ln[1]; $names{$ln} = $fn; } else {# e.g. Jean Paul Sartre and Henry J. James $fn = $fn_ln[0] . " " . $fn_ln[1]; $ln = $fn_ln[2]; $names{$ln} = $fn; } } foreach $n (sort(keys(%names))) { print("$n, $names{$n}\n"); } __DATA__ Michel Foucault Jean Paul Sartre Henry J. James print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 23: Write a program that has to be invoked with one argument. The program converses a month to its corresponding number (1-12). Print the result.\n"); print("-" x 35, "\n"); $USAGE = "month.pl name month"; # assume that this program is saved as month.pl %months = ( 'January' => 1, 'February' => 2, 'March' => 3, 'April' => 4, 'May' => 5, 'June' => 6, 'July' => 7, 'August' => 8, 'September' => 9, 'October' => 10, 'November' => 11, 'December' => 12 ); $selected_month = ""; if (@ARGV != 1) { die "Expected one argument: $USAGE"; } if (@ARGV == 1) { $selected_month = shift @ARGV; $selected_month = ucfirst( lc( $selected_month ) );# $selected_month should be capitalized @mc_arg = (grep { $selected_month eq $_ } qw(January February March April May June July August September October November December)); die "Error: expected the name of a month as argument!" if (! @mc_arg); } print("$selected_month has number $months{$selected_month}\n") if defined( $months{$selected_month} ); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 24: Variant of challenge 23. Modify it with minimal changes. Write a program that has to be invoked with one argument. The program converses a number (1-12) to the correponding month name. Print the result.\n"); print("-" x 35, "\n"); $USAGE = "month.pl number_month (1-12)"; # assume that this program is saved as month.pl %months = ( 'January' => 1, 'February' => 2, 'March' => 3, 'April' => 4, 'May' => 5, 'June' => 6, 'July' => 7, 'August' => 8, 'September' => 9, 'October' => 10, 'November' => 11, 'December' => 12 ); $selected_month_number = ""; if (@ARGV != 1) { die "Expected one argument: $USAGE"; } if (@ARGV == 1) { $selected_month_number = shift @ARGV; @mc_arg = (grep { $selected_month_number == $_ } (1..12)); die "Error: expected the number of a month as argument!" if (! @mc_arg); } %rev_months = reverse(%months);# this is the crux of this modification of challenge 23: simply reversing the old hash print("The number $selected_month_number corresponds to $rev_months{$selected_month_number}\n") if defined( $rev_months{$selected_month_number} ); print("-" x 35, "\n");