Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Counting positive numbers

by Corion (Pope)
on Jun 29, 2010 at 12:48 UTC ( #847116=note: print w/ replies, xml ) Need Help??


in reply to Counting positive numbers

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..<>}<>"


Comment on Re: Counting positive numbers
Select or Download Code
Replies are listed 'Best First'.
Re^2: Counting positive numbers
by eyepopslikeamosquito (Chancellor) on Jun 29, 2010 at 13:18 UTC

    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.

Re^2: Counting positive numbers
by ikegami (Pope) on Jun 29, 2010 at 15:46 UTC

    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
Re^2: Counting positive numbers
by Anonymous Monk on Jun 29, 2010 at 12:53 UTC

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

    Im focussed on getting a hold of this language!

    Thanx again :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://847116]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (15)
As of 2015-07-31 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls