Amusing programming mystery
Dec. 1st, 2003 05:35 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Thought this might entertain the geeks here.
My co-worker Simon is working on something or other in MATLAB. As part of a long and complicated program, he needs to invert a vector X (i.e. swap entries from top to bottom).
It's not a complicated operation. I don't recall exactly how he did it, but I'd guess something like "for i = 1 to 0.5*size(X), swap entries 'i' and (size(X)+1-i)".
But when he ran it, X just wouldn't invert. He looked through the code, checked the bit where he inverts X, cleaned up a few things that shouldn't make any difference, and ran it again.
Still not inverting.
Looked through the code again. Checking, checking... yup, that should do it. But still not inverting.
When he finally figured out what the problem was, he asked me to take him outside and shoot him... see if you can guess, before peeking at the answer.
As a clue, the part of the program that was supposed to invert the vector wasn't, in itself, flawed.
The problem was that near the start of the program he'd very carefully written code to invert the vector.
Then, later on, he'd forgotten that he'd inverted it, and written code to invert it again.
When you invert a vector twice...
My co-worker Simon is working on something or other in MATLAB. As part of a long and complicated program, he needs to invert a vector X (i.e. swap entries from top to bottom).
It's not a complicated operation. I don't recall exactly how he did it, but I'd guess something like "for i = 1 to 0.5*size(X), swap entries 'i' and (size(X)+1-i)".
But when he ran it, X just wouldn't invert. He looked through the code, checked the bit where he inverts X, cleaned up a few things that shouldn't make any difference, and ran it again.
Still not inverting.
Looked through the code again. Checking, checking... yup, that should do it. But still not inverting.
When he finally figured out what the problem was, he asked me to take him outside and shoot him... see if you can guess, before peeking at the answer.
As a clue, the part of the program that was supposed to invert the vector wasn't, in itself, flawed.
The problem was that near the start of the program he'd very carefully written code to invert the vector.
Then, later on, he'd forgotten that he'd inverted it, and written code to invert it again.
When you invert a vector twice...
no subject
Date: 2003-11-30 10:54 pm (UTC)And here I was thinking more simplistic thoughts like "freaky symmetric vector with odd-numbered elements"...
no subject
Date: 2003-12-01 03:14 am (UTC)no subject
Date: 2003-12-01 01:13 pm (UTC)no subject
Date: 2003-12-01 04:08 pm (UTC)That's just beautiful. :-)