TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-04-2008, 07:57 PM   #1 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default 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;
}
__________________

Village Idiot is offline  
Reply With Quote
Old 01-05-2008, 05:45 AM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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

Last edited by SOCK : 02-25-2008 at 01:11 AM. Reason: URI change
SOCK is offline  
Reply With Quote
Old 01-05-2008, 05:57 AM   #3 (permalink)
The Acquainted
 
obolus's Avatar
 
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
obolus is on a distinguished road
Default

lol

you guys already got me beat
obolus is offline  
Reply With Quote
Old 01-05-2008, 08:28 AM   #4 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

uhh....

PHP Code:
echo "h";
echo 
"e";
echo 
"l";
echo 
"l";
echo 
"o";
echo 
" ";
echo 
"w";
echo 
"o";
echo 
"r";
echo 
"l";
echo 
"d"
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-05-2008, 12:17 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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?
Salathe is offline  
Reply With Quote
Old 01-05-2008, 12:19 PM   #6 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

i wanna try but i know i wont win so why bother lol
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-05-2008, 12:26 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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);
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-05-2008, 03:34 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
__________________


Last edited by Village Idiot : 01-05-2008 at 06:52 PM.
Village Idiot is offline  
Reply With Quote
Old 01-05-2008, 06:49 PM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by webtuto View Post
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
__________________

Village Idiot is offline  
Reply With Quote
Old 01-07-2008, 02:15 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.

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!
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:10 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design