Tråd: Fizzbuzz!
View Single Post
ps: dette er tross alt et programmeringsforum, så å skrive ned hele greia på et ark og ta bilde av det vil bare medføre at du vil bli ledd av og hånet. Dessuten er det allerede gjort tidligere: http://picz.no/2578
Vis hele sitatet...
Etter å ha lest dette så begynte jeg å tenke litt; hva om jeg skriver en kodesnutt som henter ned bilder av bokstaver og tall fra Google, og som deretter setter sammen disse til ordene "Buzz", "Fizz" og "FizzBuzz"?

Resultat:
http://bildr.no/thumb/VjZuVHNo.jpeg

Kode

<?php
$time_start = microtime(true);

for ($i = 1; $i <= 100; $i++)
{
	if ($i % 3 == 0 && $i % 5 == 0)
		echo $Image->FizzBuzz() . "\n";

	else if ($i % 3 == 0)
		echo $Image->Fizz() . "\n";

	else if ($i % 5 == 0)
		echo $Image->Buzz() . "\n";

	else
		echo $Image->Number($i) . "\n";
}
$time_end = microtime(true);
$time = $time_end - $time_start;

echo 'Script finished in ' . $time . " seconds\n";

Kode

<?php
// Turn off all error reporting
error_reporting(0);

// Increase execution time since this script is soooo slow :(
set_time_limit (120);

class Image
{
	private $_FIZZ, $_BUZZ, $options, $userip;

	public function __construct()
	{
		$this->_FIZZ = array('F', 'i', 'z', 'z');
		$this->_BUZZ = array('B', 'u', 'z', 'z');
		$this->userip = $_SERVER['REMOTE_ADDR'];

		$this->options = array(
  						'http'=>array(
    					'method'=>"GET",
    					'header'=>"Accept-language: en\r\n" .
              				"Cookie: foo=bar\r\n" .
              				"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102013-01-21 20:23:10\r\n" 
  									)
								);
	}

	public function Fizz()
	{
		$fizzWord = NULL;
		$tempData = NULL;

		for ($i = 0; $i < 4; $i++)
		{
			$random = rand(0, 3);
			$curChar = $this->_FIZZ[$i];
			$tempData = $this->GenerateChar($curChar);

			$fizzWord .= '<img height="32" width="32" src="' . $tempData->responseData->results[$random]->tbUrl . '" />';
		}

		return $fizzWord;
	}

	public function Buzz()
	{
		$buzzWord = NULL;
		$tempData = NULL;

		for ($i = 0; $i < 4; $i++)
		{
			$random = rand(0, 3);
			$curChar = $this->_BUZZ[$i];
			$tempData = $this->GenerateChar($curChar);

			$buzzWord .= '<img height="32" width="32" src="' . $tempData->responseData->results[$random]->tbUrl . '" />';
		}

		return $buzzWord;
	}

	public function FizzBuzz()
	{
		return $this->Fizz() . $this->Buzz();
	}

	public function Number($_num)
	{
		$tempData = NULL;
		$random = rand(0, 2);

		$tempData = $this->GenerateChar($_num, "number%20");

		return '<img height="32" width="32" src="' . $tempData->responseData->results[$random]->tbUrl . '" />';
	}

	private function GenerateChar($_char, $number = NULL)
	{
		$context = stream_context_create($this->options);

		$json = file_get_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' . $number . $_char . '&imgtype=clipart&imgsz=medium&rsz=8&userip=' . $this->userip, false, $context);
		$json = json_decode($json);

		return $json;
	}
}

?>
Script finished in 159.34375 seconds
Vis hele sitatet...
Sist endret av s1gh; 21. januar 2014 kl. 20:26.