Created
November 2, 2014 11:02
-
-
Save weeking/9eb9b5599ad02da8604b to your computer and use it in GitHub Desktop.
Daily #186 easy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = '186.txt'; | |
$file = fopen($file, 'r'); | |
$fileContent = array(); | |
$sortedArray = array(); | |
while(!feof($file)) | |
{ | |
array_push($fileContent, trim(preg_replace('/\s\s+/', ' ', fgets($file)))); | |
} | |
fclose($file); | |
foreach($fileContent as $content) | |
{ | |
if(array_key_exists($content, $sortedArray)) | |
{ | |
$sortedArray[$content] += 1; | |
} else | |
{ | |
$sortedArray[$content] = 1; | |
} | |
} | |
echo 'Candy in the bag: ' . sizeof($fileContent) . "\n"; | |
echo 'Candy by type and % of total: ' . "\n"; | |
foreach($sortedArray as $key=>$value) | |
{ | |
echo $key . ': ' . $value . '(' . (($value/sizeof($fileContent))*100) . '%)' . "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment