Page 1 of 1

1 to 10

Posted: Wed Apr 05, 2006 7:36 am
by onion2k
The challenge .. write a script that outputs the numbers from 1 to 10.

Easy, of course.

But try to do it in the most interesting and non-obvious way possible .. the further out of the box you can think the better .. eg ..

Code: Select all

<?php
  array_walk(range(1,10),create_function('&$n','echo $n;'));
?>

Posted: Wed Apr 05, 2006 7:39 am
by John Cartwright

Code: Select all

print_r(range(1,10));
8)

Posted: Wed Apr 05, 2006 8:16 am
by Weirdan

Code: Select all

echo preg_replace_callback('/,/', create_function('&$a', 'static $count=0; return ++$count . " ";'), str_repeat(',', 10));

Posted: Wed Apr 05, 2006 8:20 am
by R4000
hehehe, i just created a really long thing to do this, boredem sucks :P

Code: Select all

<?php
class Count {
  var $_numbers = array();
  function Count($to){
    for($n=1;$n<=$to;$n++){
     $this->_numbers[] = $n;
    }
  }
  function debug($var_to_debug){
    echo "<font style=\"font-family: verdana; font-size: 10px; color: red;\">" . var_dump($this->$var_to_debug) . "</font><br />\n";
  }
  function print($message){
    echo "<font style=\"font-family: verdana; font-size: 10px;\">" . $message . "</font><br />\n";
  }
  function printNumbers(){
    foreach($this->_numbers as $number){
     $this->print($number);
    }
  }
}
$count = new Count(10);
$count->print("Numbers between 1 and 10!");
$count->printNumbers();
$count->print("Debug:");
$count->debug("_numbers");
destroy($count);
?>

Posted: Wed Apr 05, 2006 8:25 am
by onion2k
Weirdan wrote:

Code: Select all

echo preg_replace_callback('/,/', create_function('&$a', 'static $count=0; return ++$count . " ";'), str_repeat(',', 10));
Very nice. :lol:

Posted: Wed Apr 05, 2006 9:42 am
by Grim...

Code: Select all

<?php
function counttoten ()
{
    print date(n, 253508766)."<br />";
    print date(n, 1517500913)."<br />";
    print date(n, 891307002)."<br />";
    print date(n, -2043377409)."<br />";
    print date(n, 1272691156)."<br />";
    print date(n, 266685009)."<br />";
    print date(n, 1593578289)."<br />";
    print date(n, 933542826)."<br />";
    print date(n, 589112158)."<br />";
    print date(n, 276253800)."<br />";    
}

counttoten();
?>

Posted: Wed Apr 05, 2006 11:48 am
by Nathaniel

Code: Select all

<?PHP
function numbersToWords()
{
    static $alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

    foreach ( func_get_args() as $words )
    {
        foreach ( $words as $key => $letter )
        {
            echo ( $key === 0 ? ucfirst($alphabet[$letter]) : $alphabet[$letter] );
            
            if ( end(func_get_args()) != $words && $key == count($words) - 1 )
            {
	            echo ' ';
            }
        }
    }
}

numbersToWords(
     array(14, 13, 4), 
     array(19, 22, 14), 
     array(19, 7, 17, 4, 4), 
     array(5, 14, 20, 17), 
     array(5, 8, 21, 4), 
     array(18, 8, 23), 
     array(18, 4, 21, 4, 13), 
     array(4, 8, 6, 7, 19), 
     array(13, 8, 13, 4), 
     array(19, 4, 13)
);
?>

Posted: Wed Apr 05, 2006 12:02 pm
by hawleyjr
I don't believe I've seen a recursive entry yet...

Code: Select all

define('START_NO',1);
define('MAX_COUNT',10);

function callMe( $thisVar ){
	echo $thisVar . '<br />';
	$thisVar++;
	if( $thisVar <= MAX_COUNT ){
		callMe( $thisVar );
	}
}

callMe( START_NO );

Posted: Wed Apr 05, 2006 12:43 pm
by irisblaze

Code: Select all

//why im doing this again?
$crazy = "abcdefghij";
for($i=0;$i<strlen($crazy);$i++)
{
  $ch = substr($crazy,$i,1);
  $pos = strpos($crazy,$ch);
  echo ($pos+1)."<br/>";
}
there's no way to tell this code gonna print numbers from 1 to 10

Posted: Wed Apr 05, 2006 1:23 pm
by onion2k
A php script that opens itself..

Code: Select all

<?php

	$handle = fopen(basename($PHP_SELF), "r");
	while (!feof($handle)) {
		$contents[] = fgets($handle);
	}
	for ($x=1;$x<=count($contents);$x++) { echo $x; }
	fclose($handle);

?>

Posted: Wed Apr 05, 2006 1:36 pm
by hawleyjr

Code: Select all

echo '1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10';

Does this mean I loose?

Posted: Wed Apr 05, 2006 1:42 pm
by R4000
yup it does :P lol

Posted: Wed Apr 05, 2006 1:53 pm
by patrikG
bit like the lottery - and is a bit artistic with the output, but does eventually print 1 to 10

Code: Select all

<?php
class numbers{
	var $result = array();
	function numbers(){
		while(count($this->result)<10){
			list($usec, $sec) = explode(' ', microtime());
			mt_srand((float) $sec + ((float) $usec * 100000));
			$this->number_check(mt_rand(1,1000));
		}
		echo "<p style='font-size:300%;color:#CC0000;'>".implode(",",$this->result)."</p>";
	}
	
	function number_check($number){
		if(in_array($number,array(1,2,3,4,5,6,7,8,9,10))){
			$this->result[$number]=$number;
			echo "<b style='font-size:300%;color:#CC0000;'>$number</b>";
			return;
		}
		echo "<b style='font-size:".mt_rand(0,200)."%;'>$number</b> ";
	}
}

$numbers = new numbers();
?>

Posted: Thu Apr 06, 2006 12:36 am
by alex.barylski
I like JCart's...

I never even knew about the range()...

Thats one thing I like and hate about PHP at the same time...the strange little caveats it has...

Honestly...I would never use or think to write a function like range()...

But here you go...there is a use for such a function... :P

I like learning little things like that... :)

Posted: Thu Apr 06, 2006 1:37 am
by Todd_Z

Code: Select all

$r=range('!','+');while($c=next($r))echo(ord($c)-33).chr(10);