Task description
Sometimes, you really need to fit something you’re writing in a small space. It may be tempting to drop the vowels and wrt lk ths – and failing that, who really needs spaces? Thssprfctlrdbl!†
Write a function or program that removes lowercase vowels aeiou
, and then spaces, and then any characters from an input string. Furthermore, each time you remove a character, it must be the rightmost character eligible for removal. It must repeat this process until the string is no longer than some given input length.
† “This is perfectly readable!” But if you’re reading this footnote, it probably isn’t, really... :)
Examples
Here, you can see this process applied for successively smaller input sizes:
23: Hello, Code Golf World!
22: Hello, Code Golf Wrld!
21: Hello, Code Glf Wrld!
20: Hello, Cod Glf Wrld!
19: Hello, Cd Glf Wrld!
18: Hell, Cd Glf Wrld!
17: Hll, Cd Glf Wrld!
16: Hll, Cd GlfWrld!
15: Hll, CdGlfWrld!
14: Hll,CdGlfWrld!
13: Hll,CdGlfWrld
12: Hll,CdGlfWrl
11: Hll,CdGlfWr
(etc.)
After squeezing the string down to 17 characters, we run out of vowels to remove, so the next character we remove is the rightmost space; when we hit 14 characters, we’ve removed all vowels and spaces, so we simply start munching the string from right to left.
Here is some pseudocode Python code that solves this challenge:
def crunch_string(string, to_length):
while len(string) > to_length:
# Store the best candidate index for deletion here.
best = None
# First, find the rightmost vowel's index.
for i in range(len(string)):
if string[i] in 'aeiou':
best = i
# If there were no vowels, find the rightmost space's index.
if best is None:
for i in range(len(string)):
if string[i] == ' ':
best = i
# If there were no spaces either, use the final index.
if best is None:
best = len(string) - 1
# Remove the selected character from the string.
string = string[:best] + string[best + 1:]
# Return the string once `len(string) <= to_length`.
return string
Rules
This is code-golf, so the shortest code in bytes wins.
The input string will consist of the printable ASCII characters from space (
, decimal 32) up to and including tilde (
~
, decimal 126). There will be no uppercase vowelsAEIOU
in the string. In particular, there will be no Unicode, tabs, or newlines involved.Call the input string s, and the input target length t. Then 0 < t ≤ length(s) ≤ 10000 is guaranteed. (In particular, the input string will never be empty. If t = length(s), you should just return the string unmodified.)
Test cases
Input: 50, Duis commodo scelerisque ex, ac consectetur metus rhoncus.
Output: Duis commodo scelerisque ex, ac cnscttr mts rhncs.
Input: 20, Maecenas tincidunt dictum nunc id facilisis.
Output: Mcnstncdntdctmnncdfc
Input: 150, golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf golf
Output: glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glf glfglfglfglfglfglfglfglfglfglf
y
a vowel ? \$\endgroup\$aeiou
are vowels, andAEIOU
will not occur, for simplicity. (The whole uppercase/lowercase thing is not what I want to focus on.) I added clarification. \$\endgroup\$w
(for example, in the word cow,w
is a vowel!) Of course, that's settled for this one, but for where it's not stated that the set of vowels isaeiou
, you should sometimes includey
andw
. :-O \$\endgroup\$for index, char in enumerate(string)
instead of therange(len(str))
construct \$\endgroup\$