Saturday, March 30, 2013

CodeGolf Answer Explained: Removing Vowels

I participate in challenges at CodeGolf.SE (part of the StackExchange network), so from time to time, I will set aside some effort to elaborate on certain solutions I provide.

Today's solution (which can be found by following this link) is in response to a question titled: "Remove vowels without using too many different characters"


This challenge requires one to remove the vowels (a, e, i, o, and u) from a string. The catch is that the winning response will be the code which uses the fewest number of different characters in the code. In other words, a response of aaaaaaaa would have a score of 1, and the lowest score wins.

VBA is generally not a good language to golf with, but the nature of this challenge makes it easier than normal, since we can reuse code/functions without increasing cost. My final response is below, scoring only 22 points, using each of the following characters: (space),(newline),",,,(,),=,a,b,c,d,e,E,I,l,n,O,p,R,S,u,1.
Sub S(u)
u=Replace(Replace(Replace(Replace(Replace(u,"u","",,,1),"O","",,,1),"I","",,,1),"e","",,,1),"a","",,,1)
End Sub
Here, I used a variable named u since this is included in the Sub call, saving me 1 point up front. The Replace function (built in) does all the work for me, and nesting multiple times costs no extra. The function call uses the the arguments of u, the String passed into the Sub, the vowel to remove (cased as needed to match previous uses in the code), an empty character (""), and ,,,1. The initial arguments in this set are empty, ignoring possible, optional arguments, and the final 1 (representing the constant vbTextCompare) allows for ignoring case so CAPS and non-caps will both be removed.
comments powered by Disqus