68
17
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. I have at least found a Perl solution with a score of 4, which I will post eventually.
Update: The official winner will use a Turing-complete programming language and will not use any predefined mechanism that prints Hello, world!
. Any external resource (other than standard libraries for your language) that is used is considered part of your program and subject to the same 1-character deletion. These requirements were stuck to the desk on a post-it note. Apologies if you didn't see them at first.
Update 2: Yes, your program has to actually accomplish the task described above in order to receive a score! Meaning it should successfully print Hello, world!
at least once. This should have been obvious. Command-line switches and other settings that add functionality also count as part of your program and are subject to the single character deletion. The program must accomplish its task without any user input. A failure to compile counts in your failure count.
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;
1Can the deleted character result in the program not compiling? Is that still counted as not working? – lochok – 2013-04-18T07:06:48.320
@lochok, yes, that would count as a failure. Any deleted character which leads to
Hello, World!
not being printed is a failure. – None – 2013-04-18T07:09:54.3832
Similar question: http://codegolf.stackexchange.com/questions/4486/write-a-program-that-always-outputs-2012-even-if-its-modified
– mowwwalker – 2013-04-18T08:26:22.197@Walkerneo, thanks! I searched for similar questions and didn't find that one. I think this is significantly different, though. In particular, that question guarantees that only modifications resulting in syntactically valid code have to be handled. – None – 2013-04-18T08:33:47.440
The Perl test script should be subject to the same 1-character deletion – xDaizu – 2017-07-03T08:45:11.433
Could you give an example of "command-line switches and other settings that add functionality"? Do you mean switches and settings given to the program itself, or using switches and settings within the build environment? – CasaDeRobison – 2014-02-18T07:25:38.407