Skip to content

Instantly share code, notes, and snippets.

@subins2000
Created September 22, 2017 14:55
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 subins2000/1974ee8136c2ae08e269be6ed9d9af45 to your computer and use it in GitHub Desktop.
Save subins2000/1974ee8136c2ae08e269be6ed9d9af45 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Subin Siby <subinsb.com>
*/
function getInput()
{
return trim(fgets(STDIN));
}
function getResult($i)
{
$s = explode("\n", $i);
$words = $s[0];
$length = $s[1];
preg_match_all('/([a-z|A-Z|\.|\s])=\s(.*?)[,|\z]/', $words, $matches, PREG_SET_ORDER);
$letters = [];
foreach($matches as $match) {
$positions = explode(' ', $match[2]);
foreach ($positions as $position) {
$letters[$position] = $match[1];
}
}
ksort($letters);
return implode('', $letters);
}
echo 'Enter file path:';
$i = getInput();
echo 'Your string:' . PHP_EOL . getResult(file_get_contents($i)) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment