Mailing List Archive

Re: Python Golf
On 2023-11-07, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> I read this in a shell newsgroup:
>
> perl -anE '$s += $F[1]; END {say $s}' in
>
> , so I wrote
>
> py -c "import sys; print(sum(int(F.split()[1])for F in sys.stdin))" <in
>
> to show that this is possible with Python too.
>
> But now people complain that it's longer than the Perl version.
>
> Do you see ways to make it shorter (beyond removing one space
> after the semicolon ";")?

It's a bit of an unfair competition given that, unlike Perl,
Python is not designed to be an 'awk' replacement.

Having said that, you could make it a bit shorter:

py -c "print(sum(int(F.split()[1])for F in open(0)))" <in
--
https://mail.python.org/mailman/listinfo/python-list
RE: Python Golf [ In reply to ]
Discussions like this feel a bit silly after a while. How long something is
to type on a command line is not a major issue and brevity can lead to being
hard to remember too especially using obscure references.

Consider that the Perl version as shown below does not need to import
anything. If you had python import sys by default and perhaps even create a
briefer alias for sys.stdin, then this gets shorter:

py -c "import sys; print(sum(int(F.split()[1])for F in sys.stdin))" <in

becomes

py -c "print(sum(int(F.split()[1])for F in sys.stdin))" <in

or even

py -c "print(sum(int(F.split()[1])for F in stdin))" <in

As noted, some languages are designed with different perspectives in mind. I
have seen books and other internet resources that suggest ways to take many
of the UNIX utilities I used to love to place in pipelines and replace each
with a one-liner written in Python or Perl or AWK or whatever. An example
might be a program that does a head(or a tail) or a grep or a sed and so on.
One liners can often capture a good portion of what such programs do, albeit
handling the multiple options many provide may not be worth doing versus
making a small family of one-liners that together provide most of the
functionality.

But just because it can be done; it does not mean it is a good way or that
programs that do it with fewer characters are in most ways better.

Let's not flog Python.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Jon Ribbens via Python-list
Sent: Tuesday, November 7, 2023 11:06 AM
To: python-list@python.org
Subject: Re: Python Golf

On 2023-11-07, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> I read this in a shell newsgroup:
>
> perl -anE '$s += $F[1]; END {say $s}' in
>
> , so I wrote
>
> py -c "import sys; print(sum(int(F.split()[1])for F in sys.stdin))" <in
>
> to show that this is possible with Python too.
>
> But now people complain that it's longer than the Perl version.
>
> Do you see ways to make it shorter (beyond removing one space
> after the semicolon ";")?

It's a bit of an unfair competition given that, unlike Perl,
Python is not designed to be an 'awk' replacement.

Having said that, you could make it a bit shorter:

py -c "print(sum(int(F.split()[1])for F in open(0)))" <in
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Python Golf [ In reply to ]
On 2023-11-07, <avi.e.gross@gmail.com> <avi.e.gross@gmail.com> wrote:
> Discussions like this feel a bit silly after a while. How long
> something is to type on a command line is not a major issue and
> brevity can lead to being hard to remember too especially using
> obscure references.

Of course it's silly, that's why it's called "golf"!

It would be basically insane to use open(0) instead of sys.stdin
like this except where the length of the source code overrides
all other considerations - which is essentially never, unless
playing code golf...
--
https://mail.python.org/mailman/listinfo/python-list