Tux

...making Linux just a little more fun!

=?utf-8?q?Sierpi=C5=84ski_triangle?=

Jimmy O'Regan [joregan at gmail.com]


Fri, 18 Jun 2010 14:48:14 +0100

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.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Fri, 18 Jun 2010 10:09:01 -0400

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 *


Top    Back


Thomas Adam [thomas at xteddy.org]


Fri, 18 Jun 2010 15:04:00 +0100

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


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Fri, 18 Jun 2010 10:14:15 -0400

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 *


Top    Back


Ben Okopnik [ben at okopnik.com]


Fri, 18 Jun 2010 10:16:39 -0400

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


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Fri, 18 Jun 2010 10:23:13 -0400

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 *


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Fri, 18 Jun 2010 15:33:26 +0100

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.


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Fri, 18 Jun 2010 15:42:28 +0100

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.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Fri, 18 Jun 2010 10:43:39 -0400

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 *


Top    Back


Thomas Adam [thomas at xteddy.org]


Fri, 18 Jun 2010 15:45:01 +0100

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


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Fri, 18 Jun 2010 15:49:41 +0100

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.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Fri, 18 Jun 2010 10:50:10 -0400

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 *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sun, 20 Jun 2010 19:18:05 +0700

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


Top    Back


Ben Okopnik [ben at okopnik.com]


Sun, 20 Jun 2010 23:49:13 -0400

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 *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Tue, 22 Jun 2010 02:25:16 +0700

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


Top    Back