- 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
How about...
private int GetMessage( int selected ) { return ( selected%4!=selected ? 0 : selected); }
Admin
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services;
namespace GetMessageService { public class GetMessageService : System.Web.Services.WebService { public GetMessageService() { InitializeComponent(); }
}
Admin
Looks like a methodstub for some more sensible functionality... Maybe it shouldn't been checked into source-control, but still...
I miss FILE_NOT_FOUND!
captcha "berätta för lina!"
Admin
That method not only ignores extensibility but doesn't properly cache its results. Here's a much better implementation:
Yes, it actually, seriously works. How much do I get paid?
Admin
Bzzzzzzzzzt. What about negative values of selected? Those xlate to 0.
Admin
I'm doing the math for these, and they don't seem to add up right.
Correct me if I am wrong but if moose=5: result = Math.min(Math.max(5,0),3); result = Math.min(5,3); result = 3; //!=0 like the original code would return
or ((5+abs(5))/2+3-abs((5+abs(5))/2-3))/2 (10/2+3-abs(10/2-3))/2 (5+3-abs(5-3))/2 (8-2)/2 6/2=3 !=0
Admin
Admin
That's easy to fix, just wrap the method with an AOP method to add one to the result if it is 0. Now you have 1,2,3 and 4.
Admin
(I'm not a Java/C#er here, but it ought to make sense to them anyway) [#include <algorithm> and <list> is implied, of course]
Admin
At least mine works...
alternatively:
Admin
Ok, so this code is C++... so what? for(int i = INT_MAX; i > 0; i--) if(i == selected && i < 4) return i; return 0;
Optimized for speed, obviously... Or maybe I should unroll the loop...?
Admin
Admin
I didn't see a comment with a for statement.
#define ZERO 0; #define ONE=1; #define MINUSONE=-1;
private int GetMessage(int selected) { int parameter = selected;
}
Admin
OK, put me out of my misery, but what does the ["(c&3)"] do?
CAPTCHA: ewww --- enough said.
Admin
So basically (assuming 32 bit integers)
Admin
This annoys me greatly, who the frack paid this guy at all?
Admin
Exercise: Rewrite this function recursively. Extended: Use Y,S, or K combinators to write the function.
Admin
Oh yes it can be real. I have seen this code style before except it went up till 20 or so instead. And I thought that guy was the only one who could come up with it but I guess I was wrong. For once.
Admin
Awesome -- I love the implication that some external actor might come in and change the values of zero through three. (clearly, this is running on some piece of hardware with an input buffer). Bonus points for returning table[ZERO] after this happens as was suggested later on.
Admin
Don't miss out on the opportunities provided by the Java < 1.5 enum design pattern:
public final class MessageIndex { private Integer id; private static Integer upperBound = Integer(-1);
}
public class MessageIndexException extends Exception { }
private Integer GetMessage( Integer selected ) throws MessageIndexException { Integer index = Integer(0);
}
Not only does the above create more lines of code, but it requires that whoever calls it handle exceptions, wrap ints, etc.
Admin
Two pages of comments, and nobody suggested a way to do it using XML?!
Admin
Ahem. I said it was evil. OK:
With a string literal we can do
"abc"[1]
which is 'b'. But a[b] is the same as b[a], so we could also write
1["abc"]
which is also 'b'.
Now (!(c&~3)) will be 0 or 1 depending on whether c is between 1 and 3, so we get either '(' or 'c', which is then anded with c&3. I'll leave the rest up to you...
Admin
No wonder we're skint if we code like that...
Admin
I'll go with:
Can this be improved? (I doubt it!)Admin
min(max(5, 0), 3) = min(5, 3) = 3 != 0
This should work :
:-D
Admin
Oh My Dear Lord!
I had forgotten using C[++] for doing horrible stuff like that (for laughs, of course).
Admin
None of you are going to have the Canadian twin problem.
Admin
...
I know, you can use AJAX (that includes XML) to do a request to a server somewhere on the other side of the world to get the right result value. Added bonus for the person that starts the server localy when the remote server is not available.
Admin
I saw such constructs in algorithm class. My teacher was quite fond of 'alternative' ways of doing things.
Another one: how do you swap the values of 2 int variables without using a third variable?
Admin
Should have been using python:
GetMessage = lambda x: x
Admin
Bah!
#DEFINE GetMessage(x) x
Admin
Clean and elegant.
return (selected & 3) == selected ? selected : 0;
But I get that that's not the point of the exercise.
Admin
fun GetMessage 1 = 1 | GetMessage 2 = 2 | GetMessage 3 = 3 | GetMessage 4 = 4 | GetMessage other = 0
Admin
Admin
Of course it is a bad code: It uses "magical" numbers instead of constants, and it is also not flexible enough.
Here is it corrected:
Admin
Admin
This is clearly the best implementation. Add some SQL and temp tables and we've got a winner.
Admin
I think the proper Python version should be:
def GetMessage(selected): return dict([(x,x) for x in range(4)]).get(selected,0)
Admin
Maks version turns out to save about two ops. Really thought the optimizer would turn it into virtually the same code, but no such luck.
; return selected & ~3 ? 0 : selected; get_maks: movl 4(%esp), %eax movl $0, %edx testl $-4, %eax cmovne %edx, %eax ret
; return (selected & 3) == selected ? selected : 0; get_eloj: movl 4(%esp), %eax movl %eax, %edx andl $3, %edx cmpl %edx, %eax movl $0, %edx cmovne %edx, %eax ret
Admin
Best line of code ever.
Admin
Removing the conditional element for example. return !(s&~3)*s Although Java will probably throw a tantrum because of the int->bool->int casts. But I assume java can be coerced.
Admin
:s/GetMessage((.*))/((\1) % 4)/g
Admin
for (int i=0;i<10;i++) while(i!=11) return FILE_NOT_FOUND; //brilliant!!
Admin
I used a^=b^=a^=b; (which I think only compiles with GCC) in an interview once, and sufficiently confused the interviewer to assure myself the job :)
Max
Admin
; return !(selected & ~3)*selected; get_legs: movl 4(%esp), %eax xorl %edx, %edx testl $-4, %eax sete %dl imull %edx, %eax ret
On i686/gcc4.1.2 your code trade a cmov for a flagset and an imul.
IKN.
Admin
In haskell:
getMessage = id
Admin
How sad it is that this is the first time that I've ever seen this representation (after at least 17 years of mathematics and around 6 of computer science)!
This makes me even more angry at my teacher that told me the answer to "how do I compute sqrt(x) by hand?" was "punch the sqrt key on your calculator."
Admin
Unfortunately the latest gcc compiler warns that the result is undetermined, and it doesn't swap them. Guess who had to go through their code when we upgraded recently... ;-(
Admin
Hey Mr. Jaded, at least write a function that has the same behavior as the original.
Admin
...yeah, that shouldn't be "((\1) % 4)". That should be "(\1)>3?0:((\1)<1?0:(\1))". Either way.. screw functions.
And if you must use a function (or refuse to use vim)..
#pragma GetMessage( x ) (x)>3?0:((x)<1?0:(x))
It's been a while since I used C. :( Don't hate me if I used pragma where I should've used define.