At the end of your interview, the Evil Interviewer tells you, "We make all of our applicants take a short coding test, to see if they really know what they are talking about. Don't worry; it's easy. And if you create a working program, I'll offer you the job immediately." He gestures for you to sit down at a nearby computer. "All you have to do is create a working Hello World program. But"--and he grins broadly--"there's a catch. Unfortunately the only compiler we have on this machine has a small bug. It randomly deletes one character from the source code file before compiling. Ok, see you in five minutes!" And he walks out of the room, whistling happily.
Can you guarantee that you will get the job?
The Task
Write a program that will print Hello, world!
to standard output even after a single character is removed from any position in the file. Or come as close to this as possible.
The Rules
No Extraneous Output - Hello, world!
must be the only substantive thing printed to standard output. It is ok to include other characters if they are naturally produced by your language of choice--such as a trailing newline or even something like [1] "Hello, world!"
(for example if you were using R), but it must print the exact same thing every time. It cannot print Hello, world!Hello, world!
or Hello world!" && x==1
some of the time, for example. Warnings, however, are allowed.
Testing In order to test determine your score, you have to test each possible permutation of the program: test it with each character removed, and see if it produces the correct output. I have included a simple Perl program for this purpose below, which should work for many languages. If it doesn't work for you, please create a test program and include it in your answer.
Scoring Your score is the number of times your program fails. In other words, the number of individual positions in your file where deleting a character prevents your program from working. Lowest score wins. In the event of a tie, the shortest code wins.
Trivial Solutions such as "Hello, world!"
in several languages (score of 15) are acceptable, but they aren't going to win.
Happy programming, and may you get the job. But if you fail, you probably didn't want to work for that evil boss anyway.
Perl test script:
use warnings;
use strict;
my $program = 'test.pl';
my $temp_file = 'corrupt.pl';
my $command = "perl -X $temp_file"; #Disabled warnings for cleaner output.
my $expected_result = "Hello, world!";
open my $in,'<',$program or die $!;
local $/; #Undef the line separator
my $code = <$in>; #Read the entire file in.
my $fails = 0;
for my $omit_pos (0..length($code)-1)
{
my $corrupt = $code;
$corrupt =~ s/^.{$omit_pos}\K.//s; #Delete a single character
open my $out,'>',$temp_file or die $!;
print {$out} $corrupt; #Write the corrupt program to a file
close $out;
my $result = `$command`; #Execute system command.
if ($result ne $expected_result)
{
$fails++;
print "Failure $fails:\nResult: ($result)\n$corrupt";
}
}
print "\n$fails failed out of " . length $code;
Hello, World!
not being printed is a failure. \$\endgroup\$