Created
December 24, 2019 05:37
-
-
Save elminson/37d3caa43033f901dd100386f7ae7bde to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Created by PhpStorm. | |
* User: elminsondeoleo | |
* Date: 2019-12-23 | |
* Time: 22:13 | |
*/ | |
/** | |
Challenge description | |
Consider an infinite alphabet grid: | |
ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
ABCDEFGHIJKLMNOPQRSTUVWXYZ | |
... | |
Take a word (CODEGOLF in this example) and make it a subsequence of the grid, replacing unused letters by a space and removing letters at the end of the infinite grid altogether: | |
--C------------O------------ | |
---D-E--G--------O------------ | |
-----------L--------------- | |
-----F | |
Examples | |
STACKEXCHANGE | |
------------------S-T------- | |
A--C--------K---------------- | |
----E-------------------X--- | |
--C-----H------------------- | |
A-------------N------------- | |
------G-------------------- | |
----E | |
ANTIDISESTABLISHMENTARIANISM | |
A-------------N------T------- | |
--------I------------------ | |
---D-----I----------S-------- | |
----E--------------S-T------- | |
A-B----------L--------------- | |
--------I----------S-------- | |
-------H-----M-------------- | |
----E---------N------T------- | |
A-----------------R--------- | |
--------I------------------ | |
A-------------N------------- | |
--------I----------S-------- | |
------------M | |
Notes | |
Trailing whitespaces are allowed. | |
You don't need to pad the last any line with spaces. For example, if the input is ABC, you may output just ABC without 23 trailing spaces. | |
You may assume input will match [A-Z]+ regex. | |
Alternatively, you may use lower-case alphabet, in which case output will match [a-z]+. | |
You must use a newline (\n, \r\n or equivalent) to separate lines, that is a list of strings is not a proper output format. | |
This is a code-golf challenge, so make your code as short as possible! | |
* | |
* | |
*/ | |
/** | |
* @param $string | |
* @return string | |
*/ | |
function createD($string) | |
{ | |
$i = 0; | |
$alpha = range("A", "Z"); | |
$j = 0; | |
$result = ''; | |
while ($i < strlen($string)) { | |
while ($j < 26) { | |
if ($string[$i] == $alpha[$j]) { | |
$result .= $string[$i]; | |
break; | |
} else { | |
$result .= " "; | |
} | |
if ($j == 25) { | |
$result .= "\n"; | |
$j = -1; | |
} | |
$j++; | |
} | |
$i++; | |
} | |
return $result; | |
} | |
$result = createD("CODEGOLF"); | |
$result = createD("STACKEXCHANGE"); | |
$result = createD("ANTIDISESTABLISHMENTARIANISM"); | |
print_r($result); | |
echo "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment