66
\$\begingroup\$

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;
\$\endgroup\$
  • 1
    \$\begingroup\$ Can the deleted character result in the program not compiling? Is that still counted as not working? \$\endgroup\$ – lochok Apr 18 '13 at 7:06
  • \$\begingroup\$ @lochok, yes, that would count as a failure. Any deleted character which leads to Hello, World! not being printed is a failure. \$\endgroup\$ – user7486 Apr 18 '13 at 7:09
  • 2
    \$\begingroup\$ Similar question: codegolf.stackexchange.com/questions/4486/… \$\endgroup\$ – mowwwalker Apr 18 '13 at 8:26
  • \$\begingroup\$ @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. \$\endgroup\$ – user7486 Apr 18 '13 at 8:33
  • \$\begingroup\$ 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? \$\endgroup\$ – CasaDeRobison Feb 18 '14 at 7:25
129
\$\begingroup\$

Befunge, Score 0

I think I cracked it - no single character deletion will change the output.
Deleting any character from line 1 changes nothing - it still goes down at the same place.
Lines 2 and 3 are redundant. Normally line 2 is executed, but if you delete a character from it, the < is missed, and line 3 takes charge.
Deleting newlines doesn't break it either (it did break my previous version).
No test program, sorry.

EDIT: simplified a lot.

                              vv
@,,,,,,,,,,,,,"Hello, world!"<<
@,,,,,,,,,,,,,"Hello, world!"<<

A short explanation of the flow:

  1. Befunge starts executing from top-left, moving right. Spaces do nothing.
  2. v turns the execution flow downward, so it goes down one line.
  3. < turns the execution flow left, so it reads line 2 in reversed order.
  4. "Hello, world!" pushes the string to the stack. It's pushed in reversed order, because we're executing right to left.
  5. , pops a character and prints it. The last character pushed is printed first, which reverses the string once more.
  6. @ terminates the program.
\$\endgroup\$
  • 2
    \$\begingroup\$ +1, great solution. I don't think a test program is needed, because it is obvious that there is only a small number of potentially significant deletions. \$\endgroup\$ – user7486 Apr 18 '13 at 12:14
  • 4
    \$\begingroup\$ So easy to verify and no discussion involved. Genius! \$\endgroup\$ – Karma Fusebox Apr 19 '13 at 22:51
  • \$\begingroup\$ Congratulations on an excellent solution. \$\endgroup\$ – user7486 Apr 29 '13 at 7:39
  • 2
    \$\begingroup\$ I know this isn't code-golf, but here's a version in 66 bytes. \$\endgroup\$ – MD XF Feb 28 '18 at 0:26
42
\$\begingroup\$

Perl, Score 0

(147 characters)

Here is my solution, which I managed to get from 4 down to 0:

eval +qq(;\$_="Hello, world!";;*a=print()if length==13or!m/./#)||
+eval +qq(;\$_="Hello, world!";;print()if*a!~/1/||!m/./#)##)||
print"Hello, world!"

It must appear all on one line to work; line breaks are for "readability" only.

It benefits from Perl's pathologically permissive syntax. Some highlights:

  • Barewords that are not recognized are treated as strings. So when eval becomes evl, that is not an error if a string is permissible at that point.
  • Unary + operator, which does nothing other than disambiguate syntax in certain situations. This is useful with the above, because function +argument (where + is unary) becomes string + argument (addition) when a function name is mangled and becomes a string.
  • Many ways to declare strings: a double quoted string qq( ) can become a single-quoted string q(); a string delimited by parentheses qq(; ... ) can become a string delimited by a semicolon qq; ... ;. # inside strings can eliminate balancing issues by converting things to comments.

The length of this can probably be reduced somewhat, though I doubt ugoren's solution can be beaten.

\$\endgroup\$
  • 13
    \$\begingroup\$ +1 for line breaks are for readability only ... \$\endgroup\$ – Bakuriu Apr 24 '13 at 14:47
40
\$\begingroup\$

HQ9+

This will never fail to produce the intended result when a character is deleted so gets a score of zero.

HH

When do I start?

\$\endgroup\$
  • 1
    \$\begingroup\$ It produces "hello, world" though, not "Hello, world!" as specified. \$\endgroup\$ – Paul R Apr 17 '13 at 21:17
  • 16
    \$\begingroup\$ Nonsense, the language was just incorrectly specified on the esolang wiki ;) \$\endgroup\$ – skeevey Apr 17 '13 at 21:20
  • 11
    \$\begingroup\$ Good for you if they have an HQ9+ compiler on the interview computer... :) \$\endgroup\$ – user7486 Apr 18 '13 at 4:41
  • \$\begingroup\$ You could write your own buggy compiler. \$\endgroup\$ – PyRulez Feb 19 '14 at 20:50
  • 2
    \$\begingroup\$ @mbomb007 This interpreter prints Hello, world! for the H command. \$\endgroup\$ – Dennis Dec 14 '16 at 1:28
12
\$\begingroup\$

Befunge-98, score 0, 45 bytes

20020xx""!!ddllrrooww  ,,oolllleeHH""cckk,,@@

Try it online!

Although an optimal solution has already been found (and there's no tie breaker), I thought I'd show that this can be simplified considerably with Befunge 98.

Explanation

The 20020xx reliably sets the delta (the steps of the instruction pointer between ticks) to (2,0) so that starting at the first x, only every other command is executed. See this answer for a detailed explanation of why this works. Afterwards, the code is merely:

"!dlrow ,olleH"ck,@

We first push all the relevant character codes onto the stack with "!dlrow ,olleH". Then ck, means print the top of the stack (,), 13 (c plus 1) times (k). @ terminates the program.

\$\endgroup\$
11
\$\begingroup\$

J, 7 points

Selecting every letter with odd position:

   _2{.\'HHeellllo,,  wwoorrlldd!!'
Hello, world!
\$\endgroup\$
  • 5
    \$\begingroup\$ The same thing in golfscript would be 4 points: 'HHeelllloo,, wwoorrlldd!!'2% \$\endgroup\$ – cardboard_box Apr 18 '13 at 0:07
  • \$\begingroup\$ @cardboard_box Feel free to submit it. :) \$\endgroup\$ – randomra Apr 18 '13 at 10:04
  • \$\begingroup\$ Upvoting because I understand it, and it doesn't feel like cheating. \$\endgroup\$ – Michael Stern Apr 17 '14 at 16:43
3
\$\begingroup\$

Befunge-93, Score 0 (63 bytes)

I know this isn't a code-golf challenge, but I thought it would be interesting to see if the existing Befunge-93 solution could be improved upon in terms of size. I had originally developed this technique for use in the similar Error 404 challenge, but the need for a wrapping payload in that case made the 3 line solution more optimal.

This isn't quite as good as Martin's Befunge-98 answer, but it's still a fairly significant reduction on the winning Befunge-93 solution.

<>>  "!dlrow ,olleH">:#,_@  vv
  ^^@_,#!>#:<"Hello, world!"<<>#

Try it online!

Explanation

There are two versions of the payload. For an unaltered program, the first < causes the program to execute right to left, wrapping around to the end of the line, until it reaches a v directing it down to the second line, and a < directing it left into the left-to-right version of the payload.

An error on the second line causes the final < to be shifted left and replaced with a > directing the flow right instead. The # (bridge) command has nothing to jump over, so the code just continues until it wraps around and reaches a ^ at the start of the line, directing it up to the first line, and then a > directing it right into the right-to-left payload.

Most errors on the first line just cause the final v commands to shift across by one, but that doesn't alter the main flow of the code. Deleting the first < is slightly different, though - in that case the execution path just flows directly into the left-to-right payload on the first line.

The other special case is the removal of the line break. When the code wraps around to the end of the line, in this case it's now the end of the what used to be the second line. When it encounters the # command from the right, this jumps over the > and thus continues directly into the right-to-left payload.

In case there's any doubt, I've also tested with the perl script and it confirmed that "0 failed out of 63".

\$\endgroup\$
0
\$\begingroup\$

Gol><>, score 0, 38 bytes

<<H"Hello, world!"/
H"Hello, world!" <

The language is released after the challenge.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy