Skip to content

Instantly share code, notes, and snippets.

@blerou
Last active December 12, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blerou/4709706 to your computer and use it in GitHub Desktop.
Save blerou/4709706 to your computer and use it in GitHub Desktop.
Let's play Haskell! - #8 - beautiful strings
*Beautiful Strings*
When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.
Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.
The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)
You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?
*Input*
The input file consists of a single integer m followed by m lines.
*Output*
Your output should consist of, for each test case, a line containing the string "Case #x: y" where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.
*Constraints*
5 ≤ *m* ≤ 50
2 ≤ length of *s* ≤ 500
import Data.List
import Data.Char
sample = "5\nABbCcc\nGood luck in the Facebook Hacker Cup this year!\nIgnore punctuation, please :)\nSometimes test cases are hard to make up.\nSo I just go consult Professor Dalves"
sentence = "Good luck in the Facebook Hacker Cup this year!"
main :: IO ()
main = interact solver
solver :: String -> String
solver = unlines . solve' . drop 1 . lines
solve' :: [String] -> [String]
solve' sentence = zipWith formatter [1..] (map beautyIndex sentence)
where formatter index score = "Case #" ++ show index ++ ": " ++ show score
--beautyIndex :: String -> Int
beautyIndex sentence = sum (zipWith (*) [26,25..1] (charGroup (relevantChar sentence)))
charGroup letters = reverse (sort (map length (group (sort letters))))
relevantChar sentence = filter (`elem` ['a'..'z']) (map toLower sentence)
<?php
$sentence = 'Good luck in the Facebook Hacker Cup this year!';
echo beauty_index($sentence);
echo "\n";
echo beauty_index2($sentence);
echo "\n";
function beauty_index($sentence) {
$sentence = strtolower($sentence);
$sentence = preg_replace('/[^a-z]/', '', $sentence);
$count = array_count_values(str_split($sentence, 1));
arsort($count);
$scores = range(26, 26-count($count)+1, -1);
$count = array_combine($scores, $count);
$sum = 0;
foreach ($count as $score => $c) {
$sum += $score*$c;
}
return $sum;
}
function beauty_index2($sentence) {
$sentence = preg_replace('/[^a-z]/', '', strtolower($sentence));
$count = array_count_values(str_split($sentence, 1));
arsort($count);
$scores = range(26, 26-count($count)+1, -1);
$count = array_combine($scores, $count);
array_walk($count, function(&$c, $score) { $c = $c * $score; });
$sum = array_reduce($count, function($sum, $score) { return $sum + $score; }, 0);
return $sum;
}
5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves
Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment