5
\$\begingroup\$

In this challenge, you must write a Hello World program that is both short and complex. We measure complexity by Cyclomatic McCabe complexity, and length by the number of bytes (not characters).

Score = complexity / bytes

For example, if the cyclomatic complexity is 5 and you use 100 bytes, your score is 5/100=0.05. If the cyclomatic complexity is 25 and you use 200 bytes, your score is 25/200=0.125.

The program shall take no input. The program must output exactly the ASCII-encoded string:

Hello, world!

in this exact case. This string may or may not be followed by a single newline (LF, ASCII 10). No other text shall be output.

\$\endgroup\$
7
  • \$\begingroup\$ Is this really code bowling? Seems like golf, with an added element. \$\endgroup\$
    – Iszi
    Jan 27, 2014 at 21:42
  • \$\begingroup\$ @Iszi bowling is more appropriate than golf in this case because big score wins. \$\endgroup\$
    – hildred
    Jan 27, 2014 at 22:23
  • \$\begingroup\$ @hildred Highest score wins, but the code has to be fairly short to keep a score high. Though the scoring may not be golf-like, the nature of the challenge most closely matches code golf. \$\endgroup\$
    – Iszi
    Jan 28, 2014 at 2:41
  • 1
    \$\begingroup\$ @Iszi, I'm not sure whether it's really bowling, but it's certainly not golf because the scoring depends on a factor other than the length of the code. \$\endgroup\$ Jan 28, 2014 at 12:31
  • \$\begingroup\$ @PeterTaylor I understand there's a factor other than length of code, but length of code is a strong enough factor in this that I think it really does fit. I'm pretty sure we've had other modified code golf challenges like this before, and I can't think of another tag that fits well enough - unless you just want to genericize it to code bowling. \$\endgroup\$
    – Iszi
    Jan 28, 2014 at 14:07

4 Answers 4

10
\$\begingroup\$

C# - 1/3

string x = null;
Console.WriteLine(x??x??x??...??x??"Hello, world!");

Score calculation:

lim(x→x/(x*3+53))
x→∞

If you feel the need to wrap this all in a class with a Main method... it doesn't change the score.

\$\endgroup\$
12
  • \$\begingroup\$ Yes, I thought of doing something similar in GolfScript with {}/. \$\endgroup\$ Jan 27, 2014 at 14:06
  • \$\begingroup\$ Since x is always initialized to null, the score here should probably be one (every time the program is executed, one path - and exactly the same path - is taken). \$\endgroup\$
    – primo
    Jan 27, 2014 at 16:08
  • 2
    \$\begingroup\$ @primo The OP didn't specify that each path must be reachable, which would an entirely new challenge, since it would require randomization. \$\endgroup\$ Jan 27, 2014 at 16:16
  • \$\begingroup\$ @KendallFrey Or, that your program follow each branch at some point. \$\endgroup\$
    – primo
    Jan 27, 2014 at 16:20
  • 2
    \$\begingroup\$ @gerrit That's a good question. In the strictest theoretical sense, a program that does not take input takes only one possible path, so it's not possible to take multiple paths without input. Therefore the complexity will always be 1 unless you account for paths that are not taken. At least, this is my extrapolation. \$\endgroup\$ Jan 27, 2014 at 19:55
8
\$\begingroup\$

Perl - score = 29/42 ≈ 0.6905

fork?wait:print for'Hello, world!
'=~/./gs

This will spawn a total of 16384 processes (don't worry though, no more than 15 are ever active at a time), each with a cyclomatic complexity of 3:

E - N + 2P = 8 - 7 + (2·1) = 3

Each time a branch is encountered, both paths are taken; once by the parent, and once by the newly spawned child.

My contention is that because the parent process never stops executing (that is, control is not passed, but rather split in two directions), the child process can not be considered to be running in the same control flow as the parent, but rather in its own. The total complexity diagram would therefore be more accurately depicted as the following:

The nodes from all paths which can never be reached have been removed.

E = 100; N = 73; P = 1 ⇒ CC = 29


Perl - score = 6000000/38 ≈ 157894.74

print eval'0?1:'x6e6."'Hello, world!'"

If you don't like the previous solution, consider this one: 6000000 nested branches, with a code length of 38. It could be argued - in fact I would even argue - that the complexity of this code is exactly 1, as the true condition of each branch can never be met, and therefore should be excluded from the complexity analysis.

In general, I think solutions of this type trivialize the challenge.

\$\endgroup\$
9
  • \$\begingroup\$ As a non-Perl hacker, I'm pretty confused by this. Can you explain how it works, and why it has a complexity of more than 4, which I would have calculated it as? \$\endgroup\$ Jan 27, 2014 at 19:58
  • \$\begingroup\$ The false branch spawns an entirely new process, essentially creating a whole new code tree. If one were to draw each of these trees on a single canvas, there would be 16384 of them. \$\endgroup\$
    – primo
    Jan 27, 2014 at 20:03
  • 2
    \$\begingroup\$ It's the same program, isn't it? It's just one more decision branch, adding 1 to the complexity. \$\endgroup\$ Jan 27, 2014 at 20:06
  • 1
    \$\begingroup\$ @StéphaneGourichon It's been a while, but I'm pretty sure I used draw.io. \$\endgroup\$
    – primo
    Apr 1, 2016 at 7:07
  • 1
    \$\begingroup\$ @Akangka perl will immediately declare that it is out of memory. 6e6 is the largest possible value representable in three chars that executes on a 32-bit system. \$\endgroup\$
    – primo
    Jun 6, 2020 at 9:51
5
\$\begingroup\$

HQ9+, 1/1 = 1

H

1 byte, 1 path.

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't output exactly Hello, world!. \$\endgroup\$ Jan 27, 2014 at 21:24
  • 1
    \$\begingroup\$ Yeah it does... try the official interpreter. \$\endgroup\$
    – Timtech
    Jan 27, 2014 at 21:26
  • \$\begingroup\$ I didn't know there was an official one. The one I tried didn't. \$\endgroup\$ Jan 27, 2014 at 21:27
  • \$\begingroup\$ Ingenious. A bit like cheating though… \$\endgroup\$
    – Tobia
    Jan 27, 2014 at 22:04
4
\$\begingroup\$

VBScript - ?/337 = ?

Set s=WScript.CreateObject("WScript.Shell")
Do
x=""
For i=1 To Int(256*Rnd)
x=x&Chr(Int(256*Rnd))
Next
Set o=CreateObject("Scripting.FileSystemObject")
Set f=o.OpenTextFile("c:\a.exe",2,1)
f.Write x
f.Close
Set e=s.Exec("c:\a.exe")
Do Until e.Status:WScript.Sleep 9:Loop
t=e.StdOut.ReadLine()
Loop While t<>"Hello, world!"
Wscript.Echo t

Not sure exactly how to score this, as it's what I call "Bogo, world!". It generates random bytes and then shells them as executables until one of them successfully outputs "Hello, world!". The complexity for any given run is impossible to determine - eventually it will exit, but the number of code branches that it generates are outside of my abilities to determine statistically.

My original thought was to do something similar with randomly emitted assemblies in C#, but VBScript has a much lower byte count given the error trapping .NET would require...

BTW - have a Task Manager open if you run this (the Exec method doesn't have a way to suppress the command window, and I need the return value so I can't use Run).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.