- 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
This may be the first time I'm posting, but " char Buffer[16000];//16K is always enough. " is probably what just made me spit out my drink. Extra WTF++++ points for the solution. And a gold star sticker.
Admin
switch (selected) { case 0: return selected; break;
case 1: return selected; break;
case 2: return selected; break;
case 3: return selected; break; }
/* CAPTCHA: muhahaha */
Admin
(defun GetMessage (selected) (cond ((and (> selected -1) (< selected 4)) selected) (0)) )
I think I might be missing a parenthesis.
Admin
private int GetMessage(int selected){ var selectedArray = new Array(); for (var i=0; i<4; i++){ selectedArray.push(i); } return selectedArray[selected]; }
Admin
<nit>Both template solutions use explicit ("total") specializations, not partial ones. "template<>" -> explicit.</nit>
And what's with the people posting "solutions" that don't preserve semantics?
edu's only returns the right value for 0-3, and dolo54's doesn't even return if you're outside that range!
Admin
lol i'm preserving stupidity... wasn't that the point?
Admin
Here's a new, improved, this time actually working version in Argh!:
This time the input is the second character on the first line.
I love this language.
Admin
In Haskell: getmessage n = let a = \b c d -> if d c b then 0 else c in a 3 (a 0 n (<)) (>)
Admin
private string GetMessage( int selected ) { return "penguin"; }
Not particularly useful, but more efficient.
Admin
28 lines down to 10 lines and I'll only charge half the cost!
private int GetMessage( int selected ) { int index=0; switch( selected ) { // Get the Message case 0: index=0; break; case 1: index=1; break; case 2: index=2; break; case 3: index=3; break; } return index; }
Monk JUSTIN TIMBERLAKE (indirect quote):
If you give it your all and put in 150%, you're sure to get 100% back.
Admin
max(0, min(x, 3))
Admin
Admin
Admin
I think it is a nice example of inline documentation. It's also easy exspandable Who ask's for more
Admin
I haven't tested it, but this should work: ,>+++[->+<<->]<[>>[-]<<-]>>.
Admin
Python version:
def GetMessage( selected ): try: return [0,1,2,3][selected] except: return 0
Admin
no wrong
Admin
Now that I think of it, I could probably simplify that down to: ,>+++[->+<<->]<[>>>]>>.
Admin
I heard GAS assembly for X86 is the real thing, so here it goes. As an optimization is doesn't use the stack, nor branching. It is so simple! Only minor bugs I can't find, but I hope it doesn't affect your application. :-)
/* Input and output in %eax register. */ .globl GetMessage GetMessage: add $0x4, %eax mov %eax, %ebx sub $0x8, %eax mov %eax, %edx sar $0x1f, %edx xor %edx, %eax and $0x1, %edx add %edx, %eax sub %eax, %ebx mov %ebx, %eax sar $0x1, %eax neg %eax mov %eax, %edx mov %eax, %ebx sar $0x1f, %edx xor %edx, %eax and $0x1, %edx add %edx, %eax sub %ebx, %eax shr $0x1, %eax and $0x3, %eax ret
Admin
i think i see where you are going, but this function isn't compatibile with the definition of the first (think further the first probably isn't compatible with the definition either, but that is beside the point.)
private int GetMessage( int selected ) { static const int ONE = 1; int index = 0;
}
for those who missed it, Optimizer's version gives 0 => 4, 1 => 3, 2 => 2, 3 => 1
but still good use of select case fall through.
Admin
I think I'd better stop it before I hurt something/someone.
Admin
There's ABSOLUTELY NO WTF whatsoever with that code. As "Nicholas" is described as a "contractor", he's probably had more than his fair share of picking up other people's crap code and trying to figure out what it does. And once he's figured out what it does mechanically, he has to figure out how that fits into the logic of the program. By removing any ambiguity whatsoever over the mechanics (it took commenters precisely two comments to notice that it assigned 0 for all values other than 1,2,3) the only thing left to figure out is the logic.
Pieces of crap code like this:
private int GetMessage( int selected ) { return ((selected < 0 || selected > 3) ? 0 : selected); }
need to be picked apart and figured out from a mechanical point of view.
Let me spell it out for you:
And yes, I have learned that after 22 years of programming.
Admin
if this is c++ you could do:
private int GetMessage( int selected ){ return ((unsigned int)selected > 3) : 0 ? selected; }
other ways envolve: private int GetMessage( int selected ){ return return (selected & (~0x3)) : 0 ? selected; } private int GetMessage( int selected ){ if (selected & (~0x3)) return 0; return selected; }
private int GetMessage( int selected ){ if ((unsigned int)selected > 3) return 0; return selected; }
private int GetMessage( int selected ){ if (selected > 3) return 0; if (selected < 0) return 0; return selected; }
Addendum (2007-01-31 18:39): sorry them : and ? should be the other way around
Admin
If it's only branches that need to be avoided and relational oprators are ok, here's a python implementation of abs with no conditionals:
def new_abs(x): return ((x >= 0) * 2 - 1) * x
First time posting, apologies if this looks off.
Admin
Actually it'd better be
/* enums */ // (...)
switch (ONE) { case (selected) { return 1; } } switch (TWO) { case (selected) { return 2; } }
Admin
Um what if I pass -3?
Admin
um what if I pass -3?
Admin
Translate as needed to your language of choice. Someone should also test the original code to make sure this unit test is correct.
GetMessage(0) == 0 GetMessage(1) == 1 GetMessage(2) == 2 GetMessage(3) == 3 GetMessage(4) == 0 GetMessage(10000) == 0 GetMessage(-1) == 0 GetMessage(-5) == 0 GetMessage(-10000) == 0
Admin
Admin
What, no default case!? Oh the horror!
Admin
In perl : sub boo { (0,1,2,3)[shift] || 0 }
I.e. test cases from 1 to 10 :
perl -e 'sub boo {(0,1,2,3)[shift] || 0}; print boo $_ for 0..10 '
:)
Admin
private static int GetMessage(int selected) { switch (selected) { case 1: case 2: case 3: return selected; default: return 0; } }
Admin
Jaded? How about incompetent? It is amazing to me how the "programmers" who read this site make fun of these while at the same time posting incorrect code.
Admin
and little bit shorter :
sub boo {(0..3)[shift] || 0}
;)
Admin
Admin
Why bother with a function? you could do this as:
if (($selected > 0) && ($selected < 4)) { $index = $selected; } else { $index = 0; }
But of course that's just one line, so not much payoff!
Admin
It only works if the integer type doesn't overflow...
Admin
I vote for this one as the hands-down winner. The sheer chutzpah to use almost 1/3 of the code as a string constant used in the code is...breathtaking.
Just don't compile it on an EBCDIC machine...
Admin
Or, you could use a CPU where integer 'abs' is implemented as an instruction. Sure there's conditionals, but they're conditionals etched in silicon...
I don't remember if x86 is such a CPU, but from the comments here I'm guessing it's not.
Admin
(x*((x&3)==x))
Admin
We're really trying to avoid branches here, since highly pipelined CPU's will stop dead, scratch their heads, then slowly purge their prefetched, partially executed, but now useless instructions before they can start following the branch. Since sizeof(int) and CHAR_BIT typically don't change at runtime, the compiler can evaluate them once and compile the result with no branches.
Admin
Cute...if the range of values was 0..2 we could use e instead of pi, or cos(0.5) if the range was 0..1.
Admin
I see that nub can't think recursively:
Admin
Ruby version
Admin
NullSoft Install System (NSIS)
Function GetMessage Pop $R0 IntCmp $R0 1 0 OutOfRange 0 IntCmp $R0 3 0 0 OutOfRange Push $R0 Return OutOfRange: StrCpy $R0 "0" Push $R0 FunctionEnd
Admin
int GetMessage( int selected ) { try { int result = 1 / ( 1 - selected ) / ( 2 - selected ) / ( 3 - selected ); } catch( DivisionByZeroException e ) { return selected; } return 0; }
Admin
Admin
well, yes, basically it does... but that's what comparison operators where invented for ;o)
captcha: craaazy ...... sooo truuueee ;o)
Admin
No, No, NO! That's inefficient. Don't you know that you should ALWAYS replace a recursive algorithm that uses O(n) stack space with one using O(lg n)?
Admin
index = selected;
(It's ok. I don't need the money.)