- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Admin
Okay, since character comparison is an highly time-consuming operation, we should think about a multithreaded version to take full advantage of massively parallel architectures.
So, here is my multithreaded ruby version. Each thread processes a single character : that couldn't be more optimal when string length is equal to the size of the cluster.
Admin
I lol't.
Admin
Here's my version of this function, written completely in Whitespace. It was going to be written as a one-liner, but that would have caused the page to scroll horizontally quite a bit, so it's been split up into multiple lines.
Admin
Sure, but it's not so easy as that for those of us who don't have the translate function available!
Here's elisp, which I don't see represented here too much:
Admin
The quantum computing version:
function RemoveSpaces(qstring stringWithOrWithoutSpaces) { // Collapse variable waveform to non-spaced string form. stringWithOrWithoutSpaces.contains(" ") = false;
}
Runs in -3ns, beat that Ruby!
Admin
Recursive, mixes std::string and char* for no good reason, wastes object creation like mad. Has some fun times converting between string and stringstream representations. All it needs is random templating and it could become the best code ever written...
std::string removeSpacesAux( char* str ) { std::stringstream stream;
}
const char* removeSpaces( char* str, char *out ) { std::string removed = removeSpacesAux( str ); strcpy( out, removed.c_str() ); return out; }
Admin
In Python:
Too boring I guess.Admin
Hard returns are meaningful in WhiteSpace - it is not just spaces. In fact, you don't even have any tabs in there. I call BS.
Admin
function removeSpaces(&$_string){ strtr($_string, " ",""); }
Admin
Ahah!
Well, in a real enterprisey environment, everything is configurable, even values for TRUE, FALSE, ZERO or even FILENOTFOUND.
So, just insert that line in the beginning of the file :
" " = ""
Admin
Lisp:
(remove #\Space s)
Admin
It's not quite one-liner but close.
The real wtf is the number of dumb people who don't know that newlines are part of Whitespace syntax and still try to be funny with it. Muhahaha.
Admin
Do we get bonus point for obfuscation?
void foo(char *in, char *out) { while(*out++=in==' '?++in:*in)in++; }
Admin
That's even without evilness...
Admin
Why check each letter when you can split and append! :)
private string RemoveSpaces(string s) { string[] removedspaces; string results = string.Empty; int count = 0; int i = 0;
Admin
How about this one? :-)
captcha: dreadlocks - the new spaghetti?
Admin
I forgot the <sarcasm> tags surrounding my code ;)
Admin
Very nice! I'm sure we could figure out a way to really obfuscate this.
Why not build this as a js object, make it dependent upon the prototype libary, and increase the user download 10-fold so we can remove spaces from a string? LOL
I like how creative some of the replies have been to turn a simple string replacement into something funny.
Admin
Haven't you people heard of Web 2.0? Everything is in ajax now!
removespaces.php:
Admin
ColdFusion:
function removeSpaces(string) { return Replace(string," ",""); }
Some things ASP.NET and C were just not meant to do...
Admin
VB6
Admin
I got a big kick out of that, thanks. Who doesn't like Char Walker Demons?
Admin
choose? when? OTHERWISE?!! WTF?! Haven't the 'genius' creators of xsl ever heard of 'if', 'else', or 'switch' statements which are the de facto standard control statements for almost every other modern language?
I dread the day that I am forced to use such a convoluted, designed-by-committee, half-assed, angle-bracket, wanna-be programming language. It actually looks worse than ColdFusion!! And that's saying something!
Admin
Ahah, excellent.
But then again, you really need to parallelize that time-consuming character comparison process and take advantage of your load balancer. I suggest you send one ajax request per character ( and send them simultaneously, indeed, don't wait between each character ) and asynchronously rebuild the whitespaceless string. This has to be absolutely optimal. Too bad I'm too busy right now, must be entertaining.
I wonder if an haskell guy can figure out a similar parallel network-enabled implementation.
Admin
Obligatory Ruby version:
Admin
captcha: bathe -- somehow I feel dirty
Admin
Really? Even as a newbie programmer I almost never made that mistake. I think I recall once, about ten years ago, starting to make it, catching myself before I finished typing the line, and backspacing to correct it.
I've always thought this was mostly a myth. I consider if (3 == a) to be fairly silly.
Admin
This is braindead but XSL is not really a programming language. It's used for document transformations. Still, I think it's okay to use it for a RSS to XHTML conversion, for instance ... but it's so limited and still complicated, I don't know if it really makes a sense to learn it. It's so early-00's-let's-express-everything-in-XML ...
Admin
Real Programmers use Prolog:
In SWI-Prolog we get:
Not in Ruby:Admin
Close. Try this:
removeSpaces [] = [] removeSpaces (x:xs) = if isSpace x then removeSpace xs else x : removeSpaces xs
Admin
I'd rather parallelize server side only so I can expose the front end as a Web service to create mashups when aggregating the tubes. This is my plan to become the next Internet millionaire: www.spaceremovr.com. Now all I need is some venture capital . . .
Admin
Admin
Congratulations, you've actually managed to turn what was merely a WTF into something that's actually broken.
The original function only replaced spaces. With nothing else to go on (no documentation to the contrary, for example), we can only assume that it was MEANT to only replace spaces, and not tabs, carriage returns, line feeds, vertical tabs (!), or anything else. Your version changes the semantics of the function.
Apparently the thinking here is, "Man, that is some messed up code, I could rewrite that to be a lot simpler, clearer, and more readable. Oh, and, also, that guy is only removing spaces, that must be because he didn't know how to handle tabs and other whitespace (I mean, just look, he could barely handle writing the version that just removes the spaces), so I'll help him out by changing it so that it removes all whitespace, not just spaces." WTF?
Admin
REXX is still one of my favorites. It has lots of "word" functions ...
sentence = space(sentence,0)
The 2nd parameter is the number of spaces between words, defaults to 1. The 3rd parameter (not shown) is the pad character, defaults to blank.
Admin
I'll give you ten million dollars, but you have to buy lots of Aeron chairs and a cappucino machine.
Admin
Feel free to point out deficiencies in a language, but don't just make up stuff. C++'s string class DOES have a replace function (several variations of it, in fact): basic_string
Admin
string RemoveSpace(string s) { char[] c = s.ToCharArray(); for(int n=0; n < c.Length; n++) { if (' ' == c[n]) { c[n] = '-'; } } return(new string(c)); }
// my programs in school often did what the assignment asked but not what the teacher wanted.
Admin
Or just
removeSpaces () { echo "${1// /}"; }
since you would probably use this like so:
val="
removeSpaces "foo bar "
"Admin
Mine takes advantage of the Dictionary generic from .NET 2.0. The dictionary is indexed by each unique character in the string, with the value for each item being a list of all occurrences of that character. That way you don't have to do a bunch of compares when there are multiple occurrences of the one you want to remove. Isn't that efficient? :)
Admin
Seriously, reading compiler warnings is the only safe way to approach this problem, because the "make my code ugly by reversing conditionals" approach fails as soon as you compare variables: writing "if (b = a)" for "if (a == b)" is not going to help! So it's far better that people get used to being careful with = and ==, rather than relying on a "clever" syntactic crutch that will let them down silently and disastrously when they least expect it.
Admin
Captcha: doom
Admin
String is a reference type, so ByVal means this function is getting a pointer, and may change the original string. This is one of the biggest mistakes in VB.Net(*) ... In conclusion, in the example, the string will have not have changed, and the return value is the new string.
(*) Even with a "toy language", one needs to learn how to use it before attempting.
Admin
Heinous, and also dead wrong. Assignment to *to++ must occur while to still points to the beginning of the string; otherwise, the function will preserve spaces when they appear at the beginning of the string.
Admin
Erlang!
wtf(X) -> [I || I <- X, [I] /= " "].
or, if you prefer ...
wtf(X) -> lists:filter(fun(I) -> [I] /= " " end, X).
Admin
Actually, any decent compiler will generate a warning, and not compile just fine.
While it is mostly a style issue, it really grates on my to read code like that. We don't care what 3 is equal to, we want to know what a is equal to. Put a first, it reads better and makes more sense.
Admin
The Real WTF is that the example in C places the literal values on the left side of the comparison operators and the variables on the right.
Admin
This is true, which is why my Prolog version should actually be:
The previous version would strip all whitespace.
Admin
void removeSpace(char* string) { if( string ) *string = 0; }