Isn't that one of the programming challenges on this board?
Printable View
Isn't that one of the programming challenges on this board?
Right! d-(^_^)-bQuote:
Originally Posted by manutd
> Isn't that one of the programming challenges on this board?
Really? ö I didn't know!
Indeed it is, Dark_Pheonix. Sorry about the Shakespearean speak, BTW, I have a play coming up, enow. :)
Here's a another. Same deal, write without recursion, thereby making apparent what it does.
This one may be easier, because I got rid of the second condition. I also changed the end condition and made it into a template, both in compliance with C++ common practice. As such, Itterator is an iterator of type T object in a STL compatible container. Begin is the starting point, end is the beyond the end point.Code:template<typename T, typename Iterator>
T whatDoIDo2(T (*func)(T, T), Iterator begin, Iterator end)
{
return begin+1 != end ? func(*begin, whatDoIDo2( func, begin+1)) : *begin;
}
Both of my questions have very simmilar answers.
Well, I got that if you called the function with parameters (f, 'x', "abcd"), the function would return character f( f( f( f( 'x', 'a' ), 'b' ), 'c' ), 'd' ), right? When can you benefit that function? (o_O)Quote:
Originally Posted by King Mir
No -- I misposted it, but I'd already posted a correction (it wasn't missing a multiply) and an explanation.Quote:
Originally Posted by jafet
[edit=2]But I really take it as a compliment that you confused li'l ol' me with Salem.
[edit]Faster than...?Quote:
Originally Posted by TriKri
The short-circuit operation of the && versus the branching makes for an interesting comparison in the speed category.Code:x >= 0 && x <= 1 && y >= 0 && y <= 1
> The short-circuit operation of the && versus the branching makes for an interesting comparison in the speed category.
Yes, that's about what I realized too. :D
So write that as a loop.Quote:
Originally Posted by TriKri
Here's an example of a call to that function (this should make it easy):
In retrospect I should have done what I did with the other function, using an end itteraotor and either a template or just int.Code:char sum(a,b){
return a+b)
}
...
whatDoIDo(sum,0,charArray)
Busy thinking about code... :DQuote:
But I really take it as a compliment that you confused li'l ol' me with Salem.
Equivalent to King Mir's:
Nobody cracked my first three yet?
yep.
That's the second exactly. (expect for the functor part)
The first is very very simmilar.
The first is
Code:std::accumulate(xs, xs+strlen(xs), a, func);
yep.
I got both of these are from Haskell -- the foldl and foldr1 functions. In haskell, there are no loops, so you have to use recursion to repeat anything. There are also no variables (except constants, which are really 0 arguement functions), so you can't store a result for later use. This is mainly because there is no such thing as later; there are no sequence points in Haskell.
foldl, foldr, foldl1, and foldr1 are all functions used to apply a binary function to a list and return the final result.
>
(a^32&a) makes all lower case letters a uppe case lettersCode:main(a){while(a=getchar())putchar(a-1/((a^32&a)/13*2-11)*13);}
'A' first char = 5 when divided by 13
'A' – 'M' /13 gives 5
'N' – 'Z' /13 gives 6
prev*2 gives 10 and 12 respectively
prev-11 gives -1 and +1 respectively
(other values for a will give less that -1 or more than +1)
1/prev gives -1 and 1 respectively, non-letters a will give 0
a - prev*13 will cause both upper and lower case letters 'A' to 'M' to jump to letters 'N' to 'Z' and vice versa; no non-letter characters will be affected.
The program prints the line you writes but the two halves of the alphabet has flipped.
Good work, you must have studied the ascii table for a while, huh? (b^_^)
These programs do the same thing.Code:main(a){while(a=getchar())putchar(a-1/(~(~a|32)/13*2-11)*13);}
main(a){while(a=getchar())putchar(a+1/(11-(a&32^a)/13*2)*13);}
As already mentioned, ROT-13.Quote:
Originally Posted by jafet
I took the original intent of this thread to be posting bits that were useful but possibly not well-recognized snippets, as opposed to obfuscated code.
Along that line,
Code:int foo(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x > *y) - (*x < *y);
}
Yes, that's what I understood. In other words, the code should hve an obscure purpose, not implementation.