Isn't that one of the programming challenges on this board?
Isn't that one of the programming challenges on this board?
Right! d-(^_^)-bOriginally Posted by manutd
> Isn't that one of the programming challenges on this board?
Really? ö I didn't know!
Last edited by TriKri; 11-08-2006 at 05:42 PM.
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.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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)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.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...?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
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
> 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.
So write that as a loop.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)
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Busy thinking about code...But I really take it as a compliment that you confused li'l ol' me with Salem.
Equivalent to King Mir's:
Code:std::accumulate(begin + 1, end, *begin, some_binary_function);
Nobody cracked my first three yet?
Last edited by jafet; 11-09-2006 at 03:10 AM.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
yep.
That's the second exactly. (expect for the functor part)
The first is very very simmilar.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
The first is
Code:std::accumulate(xs, xs+strlen(xs), a, func);
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
Last edited by King Mir; 11-09-2006 at 09:27 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
>
(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);}
Last edited by TriKri; 11-09-2006 at 03:57 PM.
As already mentioned, ROT-13.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); }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Yes, that's what I understood. In other words, the code should hve an obscure purpose, not implementation.