in reply to RFC: Self Assessment Perl
See On Interviewing and Interview Questions for how I have gone about interviewing over the years.
Further to that, I've dug out some old Perl interview questions I used to assess candidates who said they "knew" Perl (to keep them honest). That is, these questions are not difficult for a Perl expert.
Given a list of numbers, namely:
write some code to add 42 to every item in this list, producing a new list. For this example data, newlist should contain the values: ( 46, 49, 50 ). Sample answer:my @oldlist = ( 4, 7, 8 );
my @oldlist = ( 4, 7, 8 ); my @newlist = map($_ + 42, @oldlist);
Given a string containing a space-separated list of names:
write some code to produce a frequency table of names, sorted descending by frequency, then ascending by name. For this data, the output should be:my $names = "freddy fred bill jock kevin andrew kevin kevin jock";
kevin : 3 jock : 2 andrew : 1 bill : 1 fred : 1 freddy : 1
Sample answer:
my $names = "freddy fred bill jock kevin andrew kevin kevin jock"; my %freq; for my $name (split ' ', $names) { ++$freq{$name}; } for my $k (sort { $freq{$b} <=> $freq{$a} || $a cmp $b } keys %fre +q) { printf "%-10s: %d\n", $k, $freq{$k}; }
Given an input text file and an output file as follows:
write some code to read infile and change all occurrences of 'Peking' to 'Beijing', leaving infile unchanged and writing the changed text to a new file outfile. Sample answer:my $infile = 'in.tmp'; my $outfile = 'out.tmp';
If they use s/Peking/Beijing/g contrast with s/\bPeking\b/Beijing/g and ask which they prefer.my $infile = 'in.tmp'; my $outfile = 'out.tmp'; open(my $fhin, '<', $infile) or die "error: open '$infile': $!"; open(my $fhout, '>', $outfile) or die "error: open '$outfile': $!" +; while (my $line = <$fhin>) { $line =~ s/\bPeking\b/Beijing/g; print $fhout $line; } close($fhin); close($fhout);
Updated: Added \b assertions to s/Peking/Beijing/ and extra questions around regex assertions. Thanks haukex.
References Added Later
|
---|