"be consistent" PerlMonks

### Counting positive numbers

by Young Monk (Novice)
 on Jun 29, 2010 at 12:21 UTC Need Help??
Young Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi! Im a newbie to PERL.

I came across a size contest in spoj. The Problem was:

The number of inputs will be given as the first input. After that, a series of numbers both positive and negative are given. The program has to count only the values of the positive numbers and display the result. Eg,

```Input:
5
2
-2
3
4
Output:
9```

For this problem, i wrote a Perl Program. This was my Program:

\$a=<>; (\$x=<>)>0 ? \$_ += \$x : 0 while \$a--; print

I've heard Perl excels in short programs. But i cant make it any shorter!

Can u guys help me make it shorter???

Thanks!

Update:

Btw, the shortest solution length was a mind boggling "6" characters long!!!! OMG!!!

Note: Actually the characters whose ASCII values are >=32 are only counted as characters

Replies are listed 'Best First'.
Re: Counting positive numbers
by Corion (Pope) on Jun 29, 2010 at 12:48 UTC

I count your attempt at 40 chars:

```          #1234567890123456789012345678901234567890
perl -wle "\$a=<>;(\$x=<>)>0?\$_+=\$x:0 while\$a--;print

My attempt is 23 chars:

```          #123456789012345678901234
perl -wle "print 0+map{1..<>}2..<>"

Likely there are ways to improve on that, for example the space between print and 0 likely can be eliminated. If you use Perl 5.10 or higher, you can replace print with say, shaving off another two characters.

Update: Relaxing the condition of respecting the number of items in the first line doesn't shorten the code for me:

```          #123456789012345678901234
perl -wle "<>;print 0+map{1..<>}<>"

Using ~~ inchworm secret operator saves a stroke:

```          #1234567890123456789012
perl -wle "print~~map{1..<>}2..<>"
Update: The inchworm secret operator is more clearly described here. I remember it from good old TPR0 way back in 2002. You can read all about TPR0 and the other early Perl golf tournaments by downloading Terje/mtv's excellent Book of Perl Golf.

Relaxing the first line restriction allows 20 strokes (which can probably be further shortened):

```         #12345678901234567890
perl -pe "\$.>1&\$_>0and\$\+=\$_}{"
This time we use the }{ eskimo greeting secret operator in harness with Eugene's infamous \$\ trick.

It's customary to include the switches in the count as a lot of code can be replaced by switches. For example, switching to say actually saves 3 chars.

```     12345678901234
perl -le"print ..."
perl -E"say ..."
12345678901

oh! Thank u very much Corion for ur Quick Reply!

Im focussed on getting a hold of this language!

Thanx again :)

Re: Counting positive numbers
by moritz (Cardinal) on Jun 29, 2010 at 13:06 UTC
Perl 6 solution, with latest Rakudo (ie built today):
```#         12345678901234567890123456
perl6 -e 'get;say [+]lines.grep: *>0'

26 character if you don't count the quotes.

Perl 6 - links to (nearly) everything that is Perl 6.
Re: Counting positive numbers
by Anonymous Monk on Jun 30, 2010 at 10:09 UTC
The shortest solution was six characters long, in what language?

I don't believe it can be done "legitimately" in six strokes. Only by "cheating". I have no experience of spoj competitions, but it looks like solutions are accepted automatically by a robot referee (i.e. without a human checking each submitted solution for correctness). With this sort of automated judging, part of the game (lamentably IMHO) is finding a way to trick the robot into accepting an invalid solution. In the past, cheating has been common at codegolf sites with automated judging. For example:

In your spoj game, one (bizarre) rule that may be exploitable is:

Score equals to size of source code of your program except symbols with ASCII code <= 32.
Why on earth would they make such an arbitrary rule? This is not true golf IMHO. It's also possible there are bugs in their robot referee or test program for this particular game (e.g. a poor or very small set of fixed test data) which may be exploited. But, to me, this sort of activity is not really golf anymore; it's more akin to finding exploits to hack into web sites.

I don't believe it!

Re: Counting positive numbers
by JavaFan (Canon) on Jul 05, 2010 at 12:13 UTC
I don't understand your example. In my book, 5 + 2 + 3 + 4 equals 14, why is the desired output 14?

As for an example of a short program, I may go for:

```perl -nE'/-/ or\$x+=\$_}{say\$x'
But that's probably not the shortest.
The number of inputs will be given as the first input.

This is why the first number needs to be skipped. And seemingly, that number counts itself as the input as well, if we assume the example to be correct. I'm not sure what the program is supposed to do with input that goes beyond the announced number of lines.

Ah, duh. I should learn to read questions correctly. Having a number as the first line of input telling how many lines the input consists of is so not needed in Perl, that I just didn't pick up on that.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://847112]
Approved by Corion
help
Chatterbox?
 [ovedpo15]: If I use "unless(opendir(my \$dir, \$test_dir))" why does it give me error "Global symbol "\$dir" requires explicit package name at" if I used "my"? If I use my \$dir before the opendir it works.. but why?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-04-22 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (83 votes). Check out past polls.

Notices?