SitePoint Sponsor

User Tag List

Page 4 of 5 FirstFirst 12345 LastLast
Results 76 to 100 of 114
  1. #76
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,109
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Code PHP:
    $myText = '9-0 srebmun eht';
    for($i=0;$i<1;$i++) {
     echo strrev($myText);
    }

  2. #77
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really should be working.

    Code:
    $q = '21i3v9';
    for(;;)
    die(base_convert($q,$q{0}*$q{0}*$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}},$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}}+$q{1}));

  3. #78
    SitePoint Zealot ShytKicka's Avatar
    Join Date
    Aug 2004
    Location
    New York
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, I didn't think I'd ever live to see this type of skill. But man was I wrong. Someone did a BASE64 conversion and then decoded it. Phew.

  4. #79
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Animated FOR loop:
    PHP Code:
    for ($i 0$i 10$i++) {
    ob_implicit_flush(true);
        echo 
    "$i ";
        
    sleep(2);

    That's so funny!
    Imagination is more important than knowledge. - Einstein

  5. #80
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple solution.... No logic involved
    Quote Originally Posted by BrandonK View Post
    or even
    PHP Code:
    echo '1 2 3 4 5 6 7 8 9'

  6. #81
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ShytKicka View Post
    Wow, I didn't think I'd ever live to see this type of skill. But man was I wrong. Someone did a BASE64 conversion and then decoded it. Phew.
    It's not BASE64. And relax. It's just for fun.

  7. #82
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You forgot 0 though :P

    PHP Code:
    $q '2' . (print 0) . 'i3v9';
    for(;;)
    die(
    base_convert($q,$q{0}*$q{0}*$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}},$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}}+$q{1})); 

  8. #83
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol. this is a really funny thread.
    try this!

    you can uncomment $patience and the elseif if or just reduce the max in rand (but that's not interesting). The code respects the requirements. It is in a for loop, and it displays numbers from 0 to 9 not necessarily in order (but that was not an explicit requirement)

    PHP Code:
    <?php
    $tries 
    0;
    $found = array();
    //$patience = 10000;
    for(;;) {
      
    $tries++;
      
    $n rand(0,100000000);
      if(
    $n >= && $n <= && !isset($found[$n]) ) {
        echo 
    $n."\n<br />";
            
    $found[$n] = true;
      }
      if( 
    array_sum(array_keys($found)) == 45 && isset($found[0]) ) {
        echo 
    "\n<br />...finally after $tries tries!";
            break;
        } 
    /* elseif($patience == $tries) {
            echo "\n<br />...damn, I've run out of patience!";
            break;
        } */
    }
    ?>

  9. #84
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, you guys have been great!

    Once again Sitepoint has come though. i learned a lot from this thread. Thanks!

  10. #85
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps View Post
    hahahaha I actually didnt get it at first.

    never knew about the range() function either.
    Me either!
    It's pretty neat.

  11. #86
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bruin03 View Post
    Ah, you guys have been great!

    Once again Sitepoint has come though. i learned a lot from this thread. Thanks!
    Ah ha! The instigator returns.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #87
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Angry

    Quote Originally Posted by simsim View Post
    Animated FOR loop:
    PHP Code:
    for ($i 0$i 10$i++) {
    ob_implicit_flush(true);
        echo 
    "$i ";
        
    sleep(2);

    That's so funny!
    I was promised 'animation'...

    *takes simsim off his Christmas card list.*
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #88
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    I was promised 'animation'...

    *takes simsim off his Christmas card list.*
    Its pretty in the CLI!
    MySQL v5.1.58
    PHP v5.3.6

  14. #89
    SitePoint Addict KelliShaver's Avatar
    Join Date
    Mar 2003
    Location
    Morehead, KY
    Posts
    308
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Formula for oh nine...

    My own little contribution:

    PHP Code:
    for($i=0$i<10$i++) {
      
    $n '9876543210';
      
    $out[] = substr($n$icount($n $i));
      
    array_reverse($out);
      if(
    count($out) == 10) {
        
    $c 0;
        foreach(
    $out as $key=>$val) {
          echo 
    $key $i $val $c;
          
    $c++;
        }
      }

    Furthermore:

    PHP Code:
    for($i=0$i<10$i++) {
      
    $n '9876543210';
      
    $out[] = substr($n$icount($n $i));
      
    array_reverse($out);
      if(
    count($out) == 10) {
        
    $c 0;
        foreach(
    $out as $key=>$val) {
          
    $output .= $key $i $val $c;
          
    $c++;
        }
        if(
    $output) {
          
    $result =  $n $output 9629629632;
          
    $result "0" $result;
        } else {
          
    $result '';
        }
      }
      if(!empty(
    $result)) {
        echo 
    $result;
      }

    but that one involved me actually doing some math. :/

  15. #90
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    You forgot 0 though :P

    PHP Code:
    $q '2' . (print 0) . 'i3v9';
    for(;;)
    die(
    base_convert($q,$q{0}*$q{0}*$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}},$q{$q{0}+$q{1}}*$q{$q{0}+$q{1}}+$q{1})); 
    Ah, found out. Extra points for going to the trouble of actually running it!

  16. #91
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Twitter Version (untested, should theoretically work)

    PHP Code:
    <?php
    for ($i=0;1<strlen((string)$i);42) {
        
    $z=rand(pow(2,24),pow(10,6)+2*pow(2,23));
        
    $q=@simplexml_load_file('http://twitter.com/statuses/user_timeline/'.$z.'.rss');
        
    $m=$q->channel->item->pubDate;
        if (!empty(
    $m)) echo (false==strstr($m,(string)$i))?null:substr(strstr($m,(string)$i++),0,1);
    }

  17. #92
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a "simple" solution with genetic algorithms!

    It uses a very na´ve implementation of the roulette wheel selection algorithm, two-point crossover breeding, and simple mutation.

    DISCLAIMER: Do NOT run this on a public web server as it is extremely CPU intensive!

    Code PHP:
    <?php
    	interface IObserver {
    		public function receiveEvent($source, $message, $params);
    	}
     
    	interface IObservable {
    		public function attachObserver(IObserver $observer);
    	}
    ?>

    Code PHP:
    <?php
    	abstract class Chromosome {
    		protected $state;
     
    		public function __construct($initialState = null) {
    			if (is_null($initialState)) $this->setState($this->getDefaultState());
    			else $this->setState($initialState);
    		}
     
    		protected function validateState($state) {
    			return !is_null($state);
    		}
     
    		protected function getDefaultState() {
    			return $this->getRandomState();
    		}
     
    		abstract protected function getRandomState();
     
    		protected function setState($state) {
    			if (!$this->validateState($state)) throw new Exception('Invalid state.');
    			$this->state = $state;
    		}
     
    		public function getState() {
    			return $this->state;
    		}
     
    		//Must return at least 1
    		abstract public function getFitness();
     
    		abstract public function mutate();
     
    		abstract public function breed(Chromosome $partner);
     
    		abstract public function getMaxFitness();
     
    		public function __toString() {
    			return $this->getState();
    		}
    	}
    ?>

    Code PHP:
    <?php
    	abstract class Selector {
    		protected $population;
    		protected $matingPopulation;
     
    		public function __construct($matingPopulation) {
    			$this->resetPopulation();
    			$this->setMatingPopulation($matingPopulation);
    		}
     
    		public function setMatingPopulation($matingPopulation) {
    			$this->matingPopulation = $matingPopulation;
    		}
     
    		public function getMatingPopulation() { return $this->matingPopulation; }
     
    		public function resetPopulation() {
    			$this->population = array();
    		}
     
    		abstract public function addChromosome(Chromosome $chromosome);
     
    		abstract public function select();
    	}
    ?>

    Code PHP:
    <?php	
    	class RouletteWheelSelection extends Selector {
    		protected $totalFitness;
     
    		public function resetPopulation() {
    			$this->totalFitness = 0;
    			parent::resetPopulation();
    		}
     
    		public function addChromosome(Chromosome $chromosome) {
    			$fitness = $chromosome->getFitness();
    			$this->totalFitness += $fitness;
    			$this->population[] = array('fitness' => $fitness, 'chromosome' => $chromosome);
    		}
     
    		public function select() {
    			$selection = array();
    			for ($i = 0; $i < $this->matingPopulation; ++$i) {
    				$selection[] = $this->selectIndividual();
    			}
    			return $selection;
    		}
     
    		protected function selectIndividual() {
    			$target = mt_rand(0, $this->totalFitness - 1);
    			$fitnessMin = 0;
    			$n = count($this->population);
    			for ($i = 0; $i < $n; ++$i) {
    				$fitness = $this->population[$i]['fitness'];
    				if ($target >= $fitnessMin && $target < $fitnessMin + $fitness) {
    					return $this->population[$i]['chromosome'];
    				} else {
    					$fitnessMin += $fitness;
    				}
    			}
    			throw new Exception('Could not select individual!');
    		}
    	}
    ?>

    Code PHP:
    <?php
    	class Planet implements IObservable {
    		protected $chromosomeDomain;
    		protected $chromosomes;
    		protected $observers;
    		protected $solutions;
    		protected $hasSolution;
     
    		public function __construct($chromosomeDomain) {
    			$this->setChromosomeDomain($chromosomeDomain);
    			$this->chromosomes = array();
    			$this->observers = array();
    			$this->solutions = array();
    			$this->hasSolution = false;
    		}
     
    		public function attachObserver(IObserver $observer) {
    			$this->observers[] = $observer;
    		}
     
    		protected function sendEvent($message, $params) {
    			reset($this->observers);
    			while ($value = each($this->observers)) {
    				$value['value']->receiveEvent($this, $message, $params);
    			}
    		}
     
    		protected function setChromosomeDomain($chromosomeDomain) {
     
     
    			$this->chromosomeDomain = $chromosomeDomain;
    		}
     
    		public function plantChromosome(Chromosome $chromosome) {
    			if (get_class($chromosome) != $this->chromosomeDomain) throw new Exception('Chromosomes must be of type '.$this->chromosomeDomain.' to be added to this planet.');
    			$this->chromosomes[(string)$chromosome] = $chromosome;
    			$this->sendEvent('plantChromosome', $chromosome);
    		}
     
    		public function deleteChromosome(Chromosome $chromosome) {
    			if (!isset($this->chromosomes[(string)$chromosome])) return;
    			unset($this->chromosomes[(string)$chromosome]);
    			$this->sendEvent('deleteChromosome', $chromosome);
    		}
     
    		public function breedGeneration(Selector $selector) {
    			if (count($this->chromosomes) < 2) throw new Exception('The planet must have at least two chromosomes.');
    			$selector->resetPopulation();
    			foreach ($this->chromosomes as $chromosome) {
    				$selector->addChromosome($chromosome);
    			}
    			$selection = $selector->select();
    			$children = array();
    			$n = count($this->chromosomes);
    			for ($i = 0; $i < $n; ++$i) {
    				$parent1 = mt_rand(0, count($selection) - 1);
    				$parent2 = $parent1;
    				while ($parent1 == $parent2) {
    					$parent2 = mt_rand(0, count($selection) - 1);
    				}
    				$parent1 = $selection[$parent1];
    				$parent2 = $selection[$parent2];
    				$child = $parent1->breed($parent2);
    				$child->mutate();
    				if (isset($children[(string)$child])) {
    					--$i;
    					continue;
    				}
    				$fitness = $child->getFitness();
    				if ($fitness >= $child->getMaxFitness()) {
    					if (!isset($this->solutions[(string)$child])) $this->solutions[(string)$child] = $child;
    					$this->hasSolution = true;
    					$this->sendEvent('foundSolution', $child);
    				}
    				$children[(string)$child] = $child;
    			}
    			$this->chromosomes = $children;
    			$this->sendEvent('breedGeneration', $children);
    		}
     
    		public function getSolutions() {
    			return $this->solutions;
    		}
     
    		public function isSolved() {
    			return $this->hasSolution;
    		}
    	}
    ?>

    Code PHP:
    <?php
    	class NumberSequence extends Chromosome {
    		protected $length;
    		protected $perfect;
     
    		public function __construct($length, $initialState = null) {
    			if ($length < 3 || $length > 10) throw new Exception('The number sequence must be 3-10 characters long.');
    			if (!is_null($initialState) && strlen($initialState) != $length) throw new Exception('Initial states must match length.');
    			$this->setLength($length);
    			$this->perfect = $this->getPerfectSolution();
    			parent::__construct($initialState);
    		}
     
    		protected function setLength($length) { $this->length = $length; }
    		public function getLength() { return $this->length; }
     
    		protected function getPerfectSolution() {
    			$string = '';
    			for ($i = 0; $i < $this->length; ++$i) $string .= $i;
    			return $string;
    		}
     
    		protected function getRandomState() {
    			$string = '';
    			for ($i = 0; $i < $this->length; ++$i) $string .= mt_rand(0, 9);
    			return $string;
    		}
     
    		protected function validateState($state) {
    			return ctype_digit($state) && strlen($state) == $this->length;
    		}
     
    		public function getFitness() {
    			$n = strlen($this->state);
    			$fitness = $n + 1;
    			for ($i = 0; $i < $n; ++$i) {
    				if ($this->state{$i} != $this->perfect{$i}) --$fitness;
    			}
    			return $fitness;
    		}
     
    		public function getMaxFitness() {
    			return strlen($this->perfect) + 1;
    		}
     
    		public function mutate() {
    			if (mt_rand(0, 1000) > 5) return;
    			$split = str_split($this->getState());
    			$replacePosition = mt_rand(0, count($split) - 1);
    			$split[$replacePosition] = (string)mt_rand(0,9);
    			$this->setState(implode('',$split));
    		}
     
    		public function breed(Chromosome $partner) {
    			if (mt_rand(0, 100) > 70) return $this;
    			if (mt_rand(0, 100) < 50) {
    				$baseSplit = str_split($this->getState());
    				$replaceSplit = str_split($partner->getState());
    			} else {
    				$replaceSplit = str_split($this->getState());
    				$baseSplit = str_split($partner->getState());
    			}
    			$onePoint = mt_rand(-1, count($baseSplit) - 1);
    			$twoPoint = $onePoint;
    			while ($twoPoint == $onePoint) $twoPoint = mt_rand(-1, count($baseSplit) - 1);
    			if ($onePoint > $twoPoint) {
    				$temp = $onePoint;
    				$onePoint = $twoPoint;
    				$twoPoint = $temp;
    			}
    			$childSplit = array();
    			$n = $this->getLength();
    			for ($i = 0; $i < $n; ++$i) {
    				if ($i > $onePoint && $i <= $twoPoint) {
    					$childSplit[] = $replaceSplit[$i];
    				} else {
    					$childSplit[] = $baseSplit[$i];
    				}
    			}
    			return new NumberSequence($this->getLength(), implode('',$childSplit));
    		}
    	}
    ?>

    Code PHP:
    <?php
    	class SolutionPrinter implements IObserver {
    		public function receiveEvent($source, $message, $params) {
    			if ($message == 'foundSolution') {
    				echo $params;
    			}
    		}
    	}
    ?>

    Code PHP:
    <?php
    	$wheel = new RouletteWheelSelection(24);
    	while (true) {
    		$planet = new Planet('NumberSequence');
    		$planet->attachObserver(new SolutionPrinter());
    		for ($i = 0; $i < 70; ++$i) $planet->plantChromosome(new NumberSequence(10));
    		for ($gen = 0; $gen < 1000; ++$gen) {
    			$planet->breedGeneration($wheel);
    			if ($planet->isSolved()) break 2;
    		}
    	}
    ?>

  18. #93
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for ($i=0; $i<=9; $i++) 
    {
    echo $i;
    }
    <3php && SitePoint ?>

  19. #94
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tarh View Post
    Here's a "simple" solution with genetic algorithms!

    It uses a very na´ve implementation of the roulette wheel selection algorithm, two-point crossover breeding, and simple mutation.
    Ok. You win. Thread over. Nothing else to see here folks, move along.

  20. #95
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No recursive functions?

    PHP Code:
    function foo($i)
    {
       echo 
    $i "\r\n";
       if(
    $i == 9) return;
       
    foo($i+1);
    }
    for(
    $i=0;$i<=1;$i++)
    {
      
    foo($i);

    chrome is a wrapper that combines a browser with spyware

  21. #96
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tarh wins.

    Please run his code on a public webserver to pay homage to him.

  22. #97
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Code:
    foreach(range(0,9) as $num) {
      echo $num;
    }
    http://us3.php.net/manual/en/function.range.php
    I like that, sweet.

  23. #98
    Web developer Carl's Avatar
    Join Date
    Sep 2003
    Location
    sweden
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -T- View Post
    No recursive functions?

    PHP Code:
    function foo($i)
    {
       echo 
    $i "\r\n";
       if(
    $i == 9) return;
       
    foo($i+1);
    }
    for(
    $i=0;$i<=1;$i++)
    {
      
    foo($i);

    PHP Code:

    function for_loop($i,$x){
        if(
    $i $x){
            echo 
    $i;
            ++
    $i;
            
    for_loop($i,$x);
         
        }
        else{
            echo 
    $x;
        }
    }

    for_loop(0,9); 

  24. #99
    SitePoint Enthusiast reflash's Avatar
    Join Date
    Jul 2008
    Location
    Pocono Mtns.
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! and I thought I had too much time on my hands...

  25. #100
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Alot of people have alot of spare time and some great ideas!

    How would everyone feel about a regular 'Heath Robinson' thread with simple questions and complex, over worked answers?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •