...making Linux just a little more fun!
Jimmy O'Regan [joregan at gmail.com]
A friend just sent me a Sierpi?ski triangle generator in 14 lines of C++ (http://codepad.org/tD7g8shT), my response is 11 lines of C:
#include <stdio.h>
int main() { int a, b; for (a = 0; a < 64; ++a) { for (b = 0; b < 64; ++b) printf ("%c ", ((a + b) == (a ^ b)) ? '#' : ' '); printf("\n"); } return 0; }
...but I'm wondering if Ben has a Perl one-liner?
(BTW, CodePad is awesome!)
-- <Leftmost> jimregan, that's because deep inside you, you are evil. <Leftmost> Also not-so-deep inside you.
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 18, 2010 at 02:48:14PM +0100, Jimmy O'Regan wrote:
> A friend just sent me a Sierpi?ski triangle generator in 14 lines of > C++ (http://codepad.org/tD7g8shT), my response is 11 lines of C: > > #include <stdio.h> > > int main() { > int a, b; > for (a = 0; a < 64; ++a) { > for (b = 0; b < 64; ++b) > printf ("%c ", ((a + b) == (a ^ b)) ? '#' : ' '); > printf("\n"); > } > return 0; > } > > ...but I'm wondering if Ben has a Perl one-liner?
Heh. Well, it wouldn't be very different from what you wrote - kinda hard to compress math operations, since they're already expressed in a rather efficient "code" - but the Perl "golf" version would look something like this (first cut, 71 characters):
for$a(0..63){printf("%s ",($a+$_)==($a^$_)?'#':' ')for 0..63;print"\n"}
I could save a character by using the '-l' commandline argument to 'perl' and replacing "\n" with "", but that's pushing just a little too hard.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Thomas Adam [thomas at xteddy.org]
2010/6/18 Jimmy O'Regan <joregan at gmail.com>:
> A friend ?just sent me a Sierpi?ski triangle generator in 14 lines of > C++ (http://codepad.org/tD7g8shT), my response is 11 lines of C: > > #include <stdio.h> > > int main() { > ?int a, b; > ?for (a = 0; a < 64; ++a) { > ? ?for (b = 0; b < 64; ++b)
You can likely golf this using bit-shifting.
> ...but I'm wondering if Ben has a Perl one-liner?
http://www.perlmonks.org/index.pl?node_id=188405
-- Thomas Adam
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 18, 2010 at 10:09:01AM -0400, Benjamin Okopnik wrote:
> > Heh. Well, it wouldn't be very different from what you wrote - kinda > hard to compress math operations, since they're already expressed in a > rather efficient "code" - but the Perl "golf" version would look > something like this (first cut, 71 characters): > > ``` > for$a(0..63){printf("%s ",($a+$_)==($a^$_)?'#':' ')for 0..63;print"\n"} > '''
67 characters:
for$a(0..63){printf"%s ",$a+$_==($a^$_)?'#':' 'for 0..63;print"\n"}
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Ben Okopnik [ben at okopnik.com]
On Fri, Jun 18, 2010 at 03:04:00PM +0100, Thomas Adam wrote:
> 2010/6/18 Jimmy O'Regan <joregan at gmail.com>: > > A friend ?just sent me a Sierpi?ski triangle generator in 14 lines of > > C++ (http://codepad.org/tD7g8shT), my response is 11 lines of C: > > > > #include <stdio.h> > > > > int main() { > > ?int a, b; > > ?for (a = 0; a < 64; ++a) { > > ? ?for (b = 0; b < 64; ++b) > > You can likely golf this using bit-shifting. > > > ...but I'm wondering if Ben has a Perl one-liner? > > http://www.perlmonks.org/index.pl?node_id=188405
I love PM. You can find all kinds of Perl funk there. Although, with my last improvement, the one-liner offered there is exactly the same length as mine.
-- OKOPNIK CONSULTING Custom Computing Solutions For Your Business Expert-led Training | Dynamic, vital websites | Custom programming 443-250-7895 http://okopnik.com
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 18, 2010 at 10:16:39AM -0400, Ben Okopnik wrote:
> On Fri, Jun 18, 2010 at 03:04:00PM +0100, Thomas Adam wrote: > > > > http://www.perlmonks.org/index.pl?node_id=188405
Turns out that one doesn't work, though - at least not without a command-line argument (a number from 1-6). So, I guess my version is the shortest one.
Not bad, for so early in the morning.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Jimmy O'Regan [joregan at gmail.com]
On 18 June 2010 15:23, Ben Okopnik <ben at linuxgazette.net> wrote:
> On Fri, Jun 18, 2010 at 10:16:39AM -0400, Ben Okopnik wrote: >> On Fri, Jun 18, 2010 at 03:04:00PM +0100, Thomas Adam wrote: >> > >> > http://www.perlmonks.org/index.pl?node_id=188405 > > Turns out that one doesn't work, though - at least not without a > command-line argument (a number from 1-6). So, I guess my version is the > shortest one. > > Not bad, for so early in the morning.
Well, if I use tcc, I can get the C version down to:
echo 'int main(){int a,b;for(a=0;a<64;a++){for(b=0;b<64;b++)p("%s ",a+b==(a^b)?"#":" ");p("\n");}return 0;}'|tcc -Dp=printf -run -
...which is not bad for C.
echo 'int main(){int a,b;f(a){f(b)p("%s ",a+b==(a^b)?"#":" ");p("\n");}return 0;}'|tcc -Dp=printf -D'f(x)=for(x=0;x<64;x++)' -run -
Works out one character longer. -- <Leftmost> jimregan, that's because deep inside you, you are evil. <Leftmost> Also not-so-deep inside you.
Jimmy O'Regan [joregan at gmail.com]
On 18 June 2010 15:33, Jimmy O'Regan <joregan at gmail.com> wrote:
> On 18 June 2010 15:23, Ben Okopnik <ben at linuxgazette.net> wrote: >> On Fri, Jun 18, 2010 at 10:16:39AM -0400, Ben Okopnik wrote: >>> On Fri, Jun 18, 2010 at 03:04:00PM +0100, Thomas Adam wrote: >>> > >>> > http://www.perlmonks.org/index.pl?node_id=188405 >> >> Turns out that one doesn't work, though - at least not without a >> command-line argument (a number from 1-6). So, I guess my version is the >> shortest one. >> >> Not bad, for so early in the morning. > > Well, if I use tcc, I can get the C version down to: > > echo 'int main(){int a,b;for(a=0;a<64;a++){for(b=0;b<64;b++)p("%s > ",a+b==(a^b)?"#":" ");p("\n");}return 0;}'|tcc -Dp=printf -run - > > ...which is not bad for C.
Pasquale, the friend who accidentally started this, sends: perl -we 'for($a=0;$a<32;++$a){for($b=0;$b<32;++$b){print((($a+$b)==($a^$b))?"# ":" ");}print"\n";}'
as well as this link: http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
(Pasquale was my student during last year's GSoC; I had very little mentoring to do
-- <Leftmost> jimregan, that's because deep inside you, you are evil. <Leftmost> Also not-so-deep inside you.
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 18, 2010 at 03:33:26PM +0100, Jimmy O'Regan wrote:
> > Well, if I use tcc, I can get the C version down to: > > echo 'int main(){int a,b;for(a=0;a<64;a++){for(b=0;b<64;b++)p("%s > ",a+b==(a^b)?"#":" ");p("\n");}return 0;}'|tcc -Dp=printf -run - > > ...which is not bad for C. > > echo 'int main(){int a,b;f(a){f(b)p("%s ",a+b==(a^b)?"#":" > ");p("\n");}return 0;}'|tcc -Dp=printf -D'f(x)=for(x=0;x<64;x++)' -run > -
Well, if external code doesn't count, I could cut off a whole bunch more... but I don't feel like messing with that.
> Works out one character longer.
Cool - I'd heard of 'tcc', never used it myself. C is just too much work for me, these days.
Using the features of Perl 5.10 (the current version), I can cut off 4 more characters:
perl -lE'for$a(0..63){printf"%s ",$a+$_==($a^$_)?"#":" "for 0..63;say}'
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Thomas Adam [thomas at xteddy.org]
On 18 June 2010 15:43, Ben Okopnik <ben at linuxgazette.net> wrote:
> Using the features of Perl 5.10 (the current version), I can cut off 4 > more characters:
Isn't it 5.12 these days?
-- Thomas Adam
Jimmy O'Regan [joregan at gmail.com]
On 18 June 2010 15:43, Ben Okopnik <ben at linuxgazette.net> wrote:
> On Fri, Jun 18, 2010 at 03:33:26PM +0100, Jimmy O'Regan wrote: >> >> Well, if I use tcc, I can get the C version down to: >> >> echo 'int main(){int a,b;for(a=0;a<64;a++){for(b=0;b<64;b++)p("%s >> ",a+b==(a^b)?"#":" ");p("\n");}return 0;}'|tcc -Dp=printf -run - >> >> ...which is not bad for C. >> >> echo 'int main(){int a,b;f(a){f(b)p("%s ",a+b==(a^b)?"#":" >> ");p("\n");}return 0;}'|tcc -Dp=printf -D'f(x)=for(x=0;x<64;x++)' -run >> - > > Well, if external code doesn't count, I could cut off a whole bunch > more... but I don't feel like messing with that. > >> Works out one character longer. > > Cool - I'd heard of 'tcc', never used it myself. C is just too much work > for me, these days. >
Well, if you use qemu, you're using part of it. Dynamic recompilation uses tcc's backend (same author).
> Using the features of Perl 5.10 (the current version), I can cut off 4 > more characters: > > ``` > perl -lE'for$a(0..63){printf"%s ",$a+$_==($a^$_)?"#":" "for 0..63;say}' > ''' > > > -- > * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET * > > TAG mailing list > TAG at lists.linuxgazette.net > http://lists.linuxgazette.net/listinfo.cgi/tag-linuxgazette.net >
-- <Leftmost> jimregan, that's because deep inside you, you are evil. <Leftmost> Also not-so-deep inside you.
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 18, 2010 at 03:45:01PM +0100, Thomas Adam wrote:
> On 18 June 2010 15:43, Ben Okopnik <ben at linuxgazette.net> wrote: > > Using the features of Perl 5.10 (the current version), I can cut off 4 > > more characters: > > Isn't it 5.12 these days?
Not on my just-updated Ubuntu system - but 'say' is supported from 5.10 forward, AFAIK, so that should work fine.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Mulyadi Santosa [mulyadi.santosa at gmail.com]
On Fri, Jun 18, 2010 at 21:23, Ben Okopnik <ben at linuxgazette.net> wrote:
> Turns out that one doesn't work, though - at least not without a > command-line argument (a number from 1-6). So, I guess my version is the > shortest one. > > Not bad, for so early in the morning.
omg Ben, you did that straight after you woke up? jeezzzzzzzzzzzzzzzzzzzz (hail Ben....)
-- regards,Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Ben Okopnik [ben at okopnik.com]
On Sun, Jun 20, 2010 at 07:18:05PM +0700, Mulyadi Santosa wrote:
> On Fri, Jun 18, 2010 at 21:23, Ben Okopnik <ben at linuxgazette.net> wrote: > > Turns out that one doesn't work, though - at least not without a > > command-line argument (a number from 1-6). So, I guess my version is the > > shortest one. > > > > Not bad, for so early in the morning. > > omg Ben, you did that straight after you woke up? > jeezzzzzzzzzzzzzzzzzzzz (hail Ben....)
Oh, c'mon, Mulyadi - you're embarrassing me. I've been doing (and playing with) Perl for so long that it's nearly instinctive by now... but I have no doubt that you have your own areas of competence where I'd be completely out of my depth.
But I'm glad you liked my pre-coffee attempt.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Hi Ben...
On Mon, Jun 21, 2010 at 10:49, Ben Okopnik <ben at okopnik.com> wrote:
> Oh, c'mon, Mulyadi - you're embarrassing me. I've been doing (and > playing with) Perl for so long that it's nearly instinctive by now... > but I have no doubt that you have your own areas of competence where I'd > be completely out of my depth.
I agree that after some time (read: quite long time), we'll become instinctive on what we usually do. It's like knowing that whenever I need to check a data structure related to a task, I should lurk to include/sched.h )
-- regards,Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com