Created
January 27, 2013 00:06
-
-
Save jayzalowitz/4645402 to your computer and use it in GitHub Desktop.
The solution to "Beautiful Strings" on facebook's hackers cup in php https://www.facebook.com/hackercup/problems.php?pid=475986555798659&round=185564241586420
This file contains 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 | |
$case = 0; | |
$filename = "beautiful_stringstxt"; | |
$facebook = file($filename, FILE_IGNORE_NEW_LINES); | |
foreach($facebook as $line) | |
{ | |
$linebeauty = 0; | |
if (!($case == 0)){ | |
$stack = array(); | |
$line = strtolower(preg_replace('/[^a-z]+/i', '', $line)); | |
$linechars = count_chars($line, 1); | |
foreach($linechars as $letter => $amount){ | |
array_push($stack, $amount); | |
} | |
arsort($stack); | |
$currentbeauty = 26; | |
foreach($stack as $letter){ | |
$letterval = $letter * $currentbeauty; | |
$linebeauty = $linebeauty+$letterval; | |
$currentbeauty--; | |
} | |
echo 'Case #'.$case.': '.$linebeauty. PHP_EOL; | |
} | |
$case++; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment