TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   The Lounge (http://www.talkphp.com/lounge/)
-   -   The hello world contest (http://www.talkphp.com/lounge/1864-hello-world-contest.html)

Village Idiot 01-04-2008 07:57 PM

The hello world contest
 
I saw this on another forum a long time ago, it became quite an entertaining thread.

Purpose:
To create an application that prints out "Hello World' in the most inefficient way possible. Any language is acceptable.

Rules:
-1 source file only (as many non-executable files as you want)
-All commands must be relevant to the task, they cannot just take up space and do nothing on consequence.
-Messiness should not rely off of bad style alone, make it hard to read even if properly formatted. But format your code sloppy nonetheless.

I have two entries
PHP
http://justanotherportfolio.com/hello/hello.php

Code:

<?
class printletter{
public $letter;
function __construct($var){
$this->letter = preg_split('//', $var, -1, PREG_SPLIT_NO_EMPTY);;}}

function display($var){
foreach($var as $output){
if($output == " "){
$output = "space";}

$output = strtolower($output);
echo"<img src=\"$output.jpg\" alt=\" \">";}}

$originalstring = new printletter("Hello World");
display($originalstring->letter);
?>

C++
http://www.justanotherportfolio.com/hello/hello.exe
Code:

#include <iostream>
using namespace std;

int main()
{
char letter = 'a';
int hello[] = {7,-3,7,0,3};
int world[] = {8,-8,3,-6,-8};

letter = letter + hello[0]; cout << letter;
letter = letter + hello[1]; cout << letter;
letter = letter + hello[2]; cout << letter;
letter = letter + hello[3]; cout << letter;
letter = letter + hello[4]; cout << letter;

cout << " "; 

letter = letter + world[0]; cout << letter;
letter = letter + world[1]; cout << letter;
letter = letter + world[2]; cout << letter;
letter = letter + world[3]; cout << letter;
letter = letter + world[4]; cout << letter;
cin >> letter;
}


SOCK 01-05-2008 05:45 AM

I love these obfuscated code contests! Haven't seen one in awhile.


First entry in PHP
PHP Code:

<?php
$a
=range(chr(65),chr(90));$x=0;$h=NULL;
foreach(array(
7,4,11,11,14,22,14,17,11,3) AS $v){
    
$h.=$a[$v];$h.=($x==4)?chr(32):NULL;$x++;
}
echo 
$h;
?>

working example

obolus 01-05-2008 05:57 AM

lol

you guys already got me beat

Aaron 01-05-2008 08:28 AM

uhh....

PHP Code:

echo "h";
echo 
"e";
echo 
"l";
echo 
"l";
echo 
"o";
echo 
" ";
echo 
"w";
echo 
"o";
echo 
"r";
echo 
"l";
echo 
"d"


Salathe 01-05-2008 12:17 PM

Your PHP function, Village Idiot, outputs HTML containing a series of images -- is this allowed for the contest or do we have to output plain text only? :-)

webtuto 01-05-2008 12:19 PM

i wanna try but i know i wont win so why bother lol

Wildhoney 01-05-2008 12:26 PM

php Code:
$szSeq = '721011081081113287111114108100';
$aChars = str_split((string) $szSeq, 1);

for($iIndex = 0; $iIndex < count($aChars); $iIndex++)
{
    list($iA, $iB, $iC) = array
    (
        $aChars[$iIndex],
        !isset($aChars[$iIndex + 1]) ? null : $aChars[$iIndex + 1],
        !isset($aChars[$iIndex + 2]) ? null : $aChars[$iIndex + 2]
    );
                           
    $iSeq = (int) str_pad($iA . $iB . $iC, 3, STR_PAD_LEFT);
   
    if($iSeq >= 120)
    {
        $iSeq = substr($iSeq, 0, 2);
        $iIndex += 1;
    }
    else $iIndex += 2;
   
    echo chr($iSeq);
}

Village Idiot 01-05-2008 03:34 PM

Quote:

Originally Posted by Salathe (Post 7715)
Your PHP function, Village Idiot, outputs HTML containing a series of images -- is this allowed for the contest or do we have to output plain text only? :-)

Images allowed, I never disallowed it in the rules.

Village Idiot 01-05-2008 06:49 PM

Quote:

Originally Posted by webtuto (Post 7716)
i wanna try but i know i wont win so why bother lol

Its not really a contest. I will choose the best entries after a few days, but nothing is on the line. Just put one out for the fun of it

Salathe 01-07-2008 02:15 PM

Here's my code submission, it's rather long (217 lines) but could've been much, much longer!

It's a messy mixture of a collection of design patterns (badly implemented, this isn't a clean code contest) making a mess of an OOP approach. You get brownie points if you can recognise the patterns being used, hehe. The end result is a rendering of "Hello World" on the screen, as displayed in the screenshot below proving that, even with uggglllyyy code, beautiful results can be achieved... if you like red text. :-P

Click for 100% scale version


The Code
To run this, you'll need 'Georgia.ttf' else a whole slew of errors will be thrown in your face.
PHP Code:
<?php

interface Outputtable
{
    public function generate();
    public function output();
}

abstract class HelloWorld
{
    protected $phrase = 'Hello World';
}

class HelloXml extends HelloWorld implements Outputtable {
    private $root = 'phrase';
   
    public function generate()
    {
        $output  = '<?xml version="1.0" encoding="utf-8"?>';
        $output .= "\n";
        $output .= sprintf('<%1$s>%2$s</%1$s>', $this->root, $this->phrase);
        return $output;
    }
    public function output()
    {
        header('Content-Type: application/xml; charset=utf-8');
        echo $this->generate();
       
    }
}

class TextImage implements Outputtable
{
    public $img;
    public $text;
    protected $palette;
    const FONT_FILE  = 'Georgia.ttf';
    const FONT_SIZE  = 20;
    const FONT_ANGLE = 4;
   
    public function __construct($text)
    {
        $this->text = $text;
        $this->generate();   
    }
   
    public function resource()
    {
        return $this->img;
    }
   
    public function generate()
    {
        $bbox = (object) array_combine(array('llx', 'lly', 'lrx', 'lry', 'urx', 'ury', 'ulx', 'uly'),
                                       imagettfbbox(self::FONT_SIZE, self::FONT_ANGLE, TextImage::FONT_FILE, $this->text));
        $width  = abs(max($bbox->lrx, $bbox->urx) - min($bbox->llx, $bbox->ulx));
        $height = abs(max($bbox->lry, $bbox->lly) - min($bbox->ury, $bbox->uly));
        $left   = min($bbox->llx, $bbox->lrx);
        $bottom = min($bbox->lly, $bbox->lry);
        $this->img = imagecreatetruecolor($width, $height);
   
        $this->generate_palette();
       
        imagefill($this->img, 0, 0, $this->palette['white']);
        imagettftext($this->img,
                    self::FONT_SIZE,
                    self::FONT_ANGLE,
                    max(0, abs($bbox->ulx)),
                    self::FONT_ANGLE < 0
                        ? abs($bbox->lly - $bbox->uly)
                        : abs($bbox->lly - $bbox->ury),
                    $this->palette['red'],
                    self::FONT_FILE,
                    $this->text);
    }
   
    public function output()
    {
        header('Content-Type: image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
   
    private function generate_palette()
    {
        $this->palette = array(
            'white' => imagecolorallocate($this->img, 255, 255, 255),
            'black' => imagecolorallocate($this->img, 0,   0,   0  ),
            'red'   => imagecolorallocate($this->img, 255, 0,   0  )
        );
    }
}

class Image2Text implements Outputtable
{
    private $img;
    private $text = 'hello';
   
    public function __construct(TextImage $img, $text = 'text', $elem = 'span')
    {
        $this->img  = $img;
        $this->elem = $elem;
        $this->text = $text;
    }
   
    public function generate()
    {
        $img     = $this->img->img;
        $width   = imagesx($img);
        $height  = imagesy($img);
        $counter = 0;
        $output  = '';
       
        for ($y = 0; $y < $height; $y++)
        {
            for ($x = 0; $x < $width; $x++)
            {
                $counter %= strlen($this->text);
                $colour   = imagecolorat($img, $x, $y);
                $rgb      = imagecolorsforindex($img, $colour);
                $hex      = self::rgb2hex(array($rgb['red'], $rgb['green'], $rgb['blue']));
                $output  .= sprintf('<%s style="color: #%s;">%s</%s>',
                                    $this->elem,
                                    $hex,
                                    $this->text[$counter],
                                    $this->elem);
                $counter++;
            }
            $output .=  '<br />';
        }
        return $output;
    }
   
    public function output()
    {
        header('Content-Type: text/html; charset=utf-8');
        echo $this->generate();
    }

    public static function rgb2hex($rgb)
    {
        $rgb = array_slice(array_pad($rgb, 3, 0), 0, 3);
       
        $hex = array();
        foreach ($rgb as $dec)
        {
            $hex[] = str_pad(dechex($dec), 2, '0', STR_PAD_RIGHT);
        }
        return implode($hex);
    }
}

class HelloImage implements Outputtable
{
    protected $source;
   
    public function __construct(HelloXml $source)
    {
        $this->source = $source;
    }
   
    public function generate()
    {
        $xml_raw = $this->source->generate();
        $xml = new SimpleXMLElement($xml_raw);
        $phrase = (string) $xml;
       
        $img = new TextImage($phrase);   
        $text = new Image2Text($img, preg_replace('/[^a-zA-Z]+/', '', $phrase), 'span');
        echo '<style type="text/css">body {font:normal 8px/1.0 courier, monospace;}</style>';
        echo $text->generate();
        exit;
        return $phrase;
    }
   
    public function output()
    {
        //header('Content-Type: image/png');
        echo $this->generate();
    }
}

class HelloContest extends HelloWorld implements Outputtable
{
    protected $renderer;
    public function __construct()
    {
        $this->renderer = new HelloImage(new HelloXml);
    }
    public function generate() { }
    public function output()
    {
        $this->renderer->output();
    }
}

// Singleton pattern
class Hello
{
    private static $instance = false;
    private function __construct() { }
    private function __clone() { }
   
    // Factory pattern
    public static function instance($key)
    {
        if (self::$instance === false and $key == 'contest')
        {
            return new HelloContest;
        }
       
        return self::$instance;
    }
}

$hello = Hello::instance('contest');
$hello->output();

Now, to get the dirty, dirty code off I'm going to scrub myself raw with lashings of soapy water. Unclean! Unclean! :-P


All times are GMT. The time now is 01:23 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0