|
|
|
| Non-WTF Job: IT Applications Manager at Questex Media Group (Auburndale, Ma) |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |
|
I would rather say that this means a lack of coffee, not a real wtf.
Even if I didn't ask someone such a question yet, I guess everybody has those weird thoughts from time to time :D |
|
This WTF originally racked up 5 pages of comments and alternate solutions! What more can be said than... int i; if(i < 11){
*crossing fingers as I click 'Post...'* |
|
the real wtf here is that the original code doesn't check for integer overflow
|
|
Wouldn't it have been obvious... variable(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)(++) or to mix it up a little (++)(++)(++)(++)(++)variable(++)(++)(++)(++)(++)(++) |
|
I would have told him the following:
There is no way to increment by 11. The only way to do this is to use ++ 11 times. So if x = 5 and you need to increment by 11, you have to do the following: x++; x++; x++; x++; x++; x++; x++; x++; x++; x++; x++; |
You use +=. This is not a WTF. It might even be the opposite. Nathan may never have seen += for whatever reason, or it could be a brief zone-out. Sincerely, Gene Wirchenko |
|
what's wrong with x + 11;
|
|
int j = 0; while( j != 11 ) { j = rand() % RAND_MAX; } i = i + j + ( 0 + 1 + 0 - 1 + 0 + 1 + 0 - 1 + 0 ) * cos( 0 ) * (cos(i)*cos(i) + sin(i)*sin(i)) + (number_of_correct_proofs_for_P_equals_NP); And there you have it. |
|
Actionscript Version
//Frame 1 var i=0; gotoAndPlay(2); //frame 2 i++; nextFrame(); //frame 3 if(i <11){ prevFrame(); } |
|
for (i = 0; i < 11; i++) {
i++; } |
|
I think I saw something about recursion on the other thread...
int i = 3; inc(ref i, 11); ... private void inc(ref int num, int amt) { if (amt == 0) return; amt--; num++; inc(ref num, amt); } |
Re: If ++ Increments ... (++)
2005-12-29 15:02
•
by
Charlie Marlow
|
|
Heh... guess that should be "if (amt <= 0)" just to make sure that, while stupid, it won't dig its way to China on negative numbers.
|
|
Too bad none of the suggested solutions are thread-safe.
|
|
All above solutions are error prone, anyone knows that the following method that will be included in the Java SDK come next major update looks like this:
int addNumber(int variable, int numberToAdd) { int variable2 = variable; for(int i = 0; i < numberToAdd; i++) { variable2 += (numberToAdd- (numberToAdd--); } if (variable + numberToAdd == variable2) { return variable += numberToAdd; } else { return (variable < variable2) ? true : FileNotFound; } } |
|
const int FORTYTWO = 11;
i += FORTYTWO; |
|
A Modern C++ version:
// Abstract class for Incrementor interface template class IncrementorBase { public: IncrementorBase() {} virtual ~IncrementorBase() {} virtual T operator+(const T& rhs) const = 0; }; // General Purpose Integer Incrementor // Assumes T has an over-ride for operator+(int) template class Incrementor : public IncrementorBase { public: Incrementor() {} virtual ~Incrementor() {} virtual T operator+(const T& rhs) const { return T(rhs + Amount); } // it's safe to do this becuase addition is commutative }; typedef Incrementor int IncrementByEleven(int i) { IntElevenIncrementor i11; return incr+i; } |
|
Here's a nice convoluted Perl version, which could be made more convoluted if it was all in-line.
${\(++$i)}++; ${\(++$i)}++; ${\(++$i)}++; ${\(++$i)}++; ${\(++$i)}++; $i++; The taking a reference ( \( ... ) ) and then dereferencing it ( ${ ... } ) gets around the fact that, in perl, the pre- and post- decrement operators don't return lvalues, thus causing ++$i++ to generate a parse error, and (++$i)++ to bomb with a seemingly custom error message related to exactly this construct. Can't modify postincrement (++) in preincrement (++) |
x++ increments x. x + 11 does not affect the value of x. |
I love it! |
|
i = i + ((i << 3) + (i << 2) - i) / i;
|
That's right... can't have any of those pesky "magic numbers" floating around in the code. |
...it does if you make it this: x = x+11; So really, even not knowing about a += isn't a valid excuse. |
|
if(x==5) {
x *= 2; x++; } else { raise IllegalValueException("aaaarrrgh!\n") } |
|
Much simpler in Python.
i += int(str(ord("\x01"))*2) |
|
Just because this comment deserves it....
|
I always thought it was something like this (excuse me my formatting, I really don't know how to use this forum ;)): |
|
How about:
const long Eleven1th = 11/1 inline long incrementByEleven(long val) return val + Eleven1th |
Re: If ++ Increments ... (++)
2005-12-29 17:57
•
by
Kiss me, I'm Polish
|
|
Why not use tools that you have for free. function AddElevenToX( int x ) {System.Clipboard.put( x ); System.run("calc.exe"); System.keyboard.press("V", "Ctrl"); System.keyboard.press("+", none); System.keyboard.press("1", none); for ( i = 1; i <= 11; i++ ) {System.keyboard.press( "=", none); } System.keyboard.press("C", "Ctrl");System.Clipboard.get( x ); return x; } |
Re: If ++ Increments ... (++)
2005-12-29 18:24
•
by
GuruBuckaroo
|
And this is exactly why I refuse to learn C++. |
|
Unfortunately for Nathan, adding eleven is one of the most laborious tasks in programming:
if (x == 1) { x = 12; } else if (x == 2) { x = 13; } else if (x == 3) { x = 14; } ... |
|
Tom Lehrer says that the important thing [in new math] is to understand what you're doing, rather than to get the right answer: #!/usr/bin/perlmy $val = shift; my @digits = split(//,$val); $digits[$#digits]++; $digits[$#digits -1]++ if $val > 9; $pow10 = 0; $x = 0; for ($j = $#digits; $j >= 0; $j--) { $x += (10 ** $pow10) * $digits[$j]; $pow10++; } print "$x\n"; |
|
Since we are being silly.
i += 011 + 1 + 1; |
|
Why haven't anyone said the obvious:
// inc i by 11 (++(++(++(++(++(++(++(++(++(++(++i))))))))))); |
|
No, it's something like this: i = ((i / 11) + 1) * 11 + (i % 11); When you do numbers larger than 1, you have to split up the plusses you see. |
|
This is much faster in SQL, so he should use a database:
declare @numTable table ( input int identity, answer int ) declare @i int set @i = 5000 --substitute the value of i you need to increment as a param in stored proc declare @counter int set @counter = 0 insert into @numtable values ( @counter + 12 ) -- identity seed is 1, so we have to use 12 set @counter = @counter + 1 while ( select max(answer) from @numtable ) < ( @i + 11 ) begin insert into @numtable values ( @counter + 12 ) set @counter = @counter + 1 end select answer from @numtable where input = @i |
First I thought, this afro-american be trippin'. Then... >>> 'WTF'*2 'WTFWTF' Brillant! |
|
You clowns are all doing it wrong. Here's what you do:
public int incrementBy11(int thisIsTheNumberThatYouWantToIncrementBy11) |
Nothing cracks me up more than seeing critics try to write "funny" code - only to have it backfire. |
|
template
T inc11( T x ) { return ++++++++++++++++++++++x; } |
|
I get this sorta stuff from my coworker all the time! coworker: i have a question... *sound of head banging on desk reverberate across world* |
|
Was this a one-time question, or does he come back with another number? Nathan: Okay Steve... I incremented 11 times. |
lol |
|
I get to use templates! Nobody told me I could use templates! template <size_t incval, typename T> There, now you can increment by lots of different values! Just do |
|
Then Steve responds: Steve: Nathan...hang on a minute... -- sorry Mike. |
|
A few days later...
Nathan: Hey Steve, thanks, that project really came out great. Steve: *sniggers* Yeah, great. Nathan: Do you mind if I just ask you one more question? Steve: Er, ok, go for it. Nathan: Well, I figured out how to get it back to 5 from 11, but I want to increment it by another half. How do I do that? Steve: <_< |
|
Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative: double number_you_want_to_increment = 0; double numero = 11; double temporary_variable_to_hold_stuff = number_you_want_to_increment; while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11) { numero = numero / 2 ; number_you_want_to_increment += numero; } I think this gets puuuuurty close to incrementing a number by 11. [6]
|
But Insidious and Elusive numbers are no problem at all! How aboot: x += 0xB x += 013 x += 1110.toDecimal() |
|
I'd like to see some code that almost adds 11.
Preferrably by introducing floating point errors. |
Heh, never quite gets there, does it? |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |