More CS60-Related Bragging

Mar 17, 2004 16:41

I'll put it behind a cut, in case you don't wish to gaze upon such a raw display of 1337ness for fear of being permanently altered by it.


Here's the bonus question for the rex part of the assignment I'm making up for 60:

For optional extra credit of 3 points, write two functions named g and h such that for as many integers as possible

g(h(x)) is equal to 1 + h(g(x))

Full credit will go to a solution in which the above relationship holds for all integers. Some credit will go for solutions that handle some integers, but not all. It does not matter what your functions do for nonintegers.

After some messing around, the truly brilliant solution made itself known to me:

g(x) => x == 1 || x == 0 ? 1 : 0;

h(x) => x == 1 || x == 0 ? 0 : 1;

The true beauty of this is that g(h(x)) == 1 + h(g(x)) for all *inputs*, not just all integers! Yes, that's right, this equality holds whether x is an integer, a float, a char, a string, or even a function!

*takes a bow*
Previous post Next post
Up