Aug 20, 2010 at 4:13pm UTC
write a program in unix which display a following pattern
1
01
101
0101
10101
Aug 20, 2010 at 5:31pm UTC
That would be 20€ (to be paid in advance).
Aug 20, 2010 at 6:16pm UTC
I wont do it for you, but I'd suggest a recursive function that outputs 1 and 0.
Aug 22, 2010 at 8:46pm UTC
Done with the writing :)
For the actual code you'll have to give me 10€ :) (which is discount on Athar's price :)
Aug 22, 2010 at 8:57pm UTC
I'll further undercut the price and do it for £1 (€1.22).
I changed my mind. I'll do it for no money, all you have to do is tell me your parents' credit card details.
Last edited on Aug 22, 2010 at 8:59pm UTC
Aug 22, 2010 at 9:39pm UTC
I'll do it for free! Just pay shipping and handling.
Aug 22, 2010 at 10:19pm UTC
you should be careful.
these sort of people usually end up as project managers.
Aug 24, 2010 at 2:18pm UTC
Ok guys. Stop BASH ing the poor guy!
Aug 24, 2010 at 2:31pm UTC
I'll have a little fun with this one...
1 2 3 4 5 6
std::string s( "1" );
for ( int i = 5; i > 0; --i )
{
std::cout << s << std::endl;
s = ( i % 2 ? "0" : "1" ) + s;
}
1
01
101
0101
10101
Last edited on Aug 24, 2010 at 2:32pm UTC
Aug 24, 2010 at 2:47pm UTC
Why did you do it for him?
Aug 24, 2010 at 3:36pm UTC
Hope this helps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
unsigned r( unsigned x, unsigned y ) {
return ( y & 1 ) && !( y & ~1 ) ? x : ( y ^ ( ( y >> 1 ) << 1 ) ) * x +
( r( x, ( y & ~1 ) >> 1 ) << 1 );
}
int main() {
unsigned x = !!( r( 10, 1 ) ^ ~r( 1, 10 ) );
unsigned c = !( ( x & ~x ) + 1 );
do {
switch ( ( ( c >> 1 ) << 1 ) ^ c ) {
default : std::cout << 1;
case 0: case 1 << 1: case 1 << 1 << 1: if ( c - ( ( c >> 1 ) << 1 ) )
case 1: case 1 + ( 1 << 1 ): case ( 1 + 1 ) << 1 + 1: std::cout << 0;
}
std::cout << x << std::endl;
x = r( x, c & 1 ? ( ( ( 011 << 1 ) + ( 010 << 1 << 1 ) )
<< 1 ) : 1 ) | ( c ^ ( ( c >> 1 ) << 1 ) );
} while ( ++c < ( ( ( 1 + 1 ) ^ 1 ) << 1 ) - 1 );
}
PS: If you need any help, ask soon because I'll forget how I got this to work in about 15 seconds.
Last edited on Aug 24, 2010 at 3:37pm UTC
Aug 24, 2010 at 3:44pm UTC
You like bitwise operators, don't you?
Aug 24, 2010 at 4:02pm UTC
Is this now a "who can do this in the most ridiculously complex way" competition?
Aug 24, 2010 at 4:52pm UTC
Well, the trick was to make the program only use 1s and 0s, because executable programs are binary files,
and binary files obviously can only have 1s and 0s. So, are you saying there was a simpler way?
Aug 24, 2010 at 5:23pm UTC
jsmith, nice code. :P
Im busy deciphering it, this is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
unsigned int r( unsigned int x, unsigned int y ) {
return ( y & 1 ) && !( y & -2 ) ? x : y%2 * x +
( r( x, ( y & -2 ) >> 1 ) << 1 );
}
int main() {
unsigned int x = 1;
unsigned int c = 0;
do {
switch ( c ) {
default : std::cout << 1;
case 0:
case 2:
case 4: break ;
case 1:
case 3:
case 8: std::cout << 0;
}
std::cout << x << std::endl;
x = r( x, c & 1 ? 100 : 1 ) | c%2;
} while ( ++c < 5 );
}
Last edited on Aug 24, 2010 at 5:32pm UTC
Aug 24, 2010 at 5:36pm UTC
@jsmith,
The question doesn't make sense. Of course there's a simpler way (of getting the same output); moorecm posted one. His way is more easily read and understood than yours, and it is also shorter, so it is provably simpler (or, at least, less complex). But your question seems to ask "is there a simpler way of getting the same output in the same way " which doesn't make sense, because it implicitly means "doing the same thing", which makes your question void (HEUHEUHEU).
Last edited on Aug 24, 2010 at 5:38pm UTC
Aug 24, 2010 at 5:39pm UTC
Chris, just look up. I did exactly the same as jsmith, I really only removed redundant parts and changed the spacing.