1 to 10

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

1 to 10

Post 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;'));
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

print_r(range(1,10));
8)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

echo preg_replace_callback('/,/', create_function('&$a', 'static $count=0; return ++$count . " ";'), str_repeat(',', 10));
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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);
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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:
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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();
?>
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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)
);
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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 );
User avatar
irisblaze
Forum Newbie
Posts: 22
Joined: Sun Mar 19, 2006 3:24 am
Location: Palestine
Contact:

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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);

?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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?
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

yup it does :P lol
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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();
?>
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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... :)
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Code: Select all

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