Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Golf code in perl

by tt08 (Initiate)
on Mar 11, 2017 at 09:55 UTC ( [id://1184255]=perlquestion: print w/replies, xml ) Need Help??

tt08 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: reduce code (Golfing References)
by eyepopslikeamosquito (Archbishop) on Mar 12, 2017 at 09:27 UTC
Re: Golf code in perl
by tybalt89 (Monsignor) on Mar 11, 2017 at 11:31 UTC

    Applying a few standard golf tricks...

    <>=~$";$\+=2*$s[$_%$']++for split$",<>;print

    44, pad as required :)

      So cool!Thank u
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Golf code in perl
by haukex (Archbishop) on Mar 11, 2017 at 10:01 UTC
      Yes i have posted the question first on stackoverflow and they suggested me perlmonks would help you.Can you please help me?
        Yes i have posted the question first on stackoverflow and they suggested me perlmonks would help you.

        Could you provide a link? At the link I posted above there is no such reference to PerlMonks.

        Can you please help me?

        Well, first of all, it seems the code you posted doesn't work, as opposed to what was posted on StackExchange, so I'll look at the latter.

        $ cat input.txt 5 5 6 11 16 7 12 $ perl -e '@j=split$",<>;print$% map$%+=2*$q[$_%@j[1]]++,split$",<>' i +nput.txt $ perl -e '@j=split$",<>;map$r+=2*$a[$_%@j[1]]++,split$",<>;print$r' i +nput.txt 8

        I take it you're not allowed any switches like e.g. -F? Anyway, here's one idea:

        $ perl -e '<>=~/\d+$/;map$r+=2*$a[$_%$&]++,split$",<>;print$r' input.t +xt 8

        Update: That's 50 chars... what do I win? ;-) Update 2: tybalt89 shows how it's done, very nice :-)

Re:reduce code
by marto (Cardinal) on Mar 12, 2017 at 09:09 UTC

    ”I am new to perl and somehow I wrote the code in perl."

    I think it's disingenuous to claim you wrote this code. The idea behind such challenges which you've copied/paste here is that the participant exercise their skills.and try out new tricks. Since you claim to be new to perl I suggest your time would be better spent learning the basics, rather than keep asking people to improve someone else's answer (inbox spam isn't a wise course of action by the way), http://learn.perl.org, learn perl in 2 hours 30 minutes. Work through the tutorials section of this site. Ask specific questions (How do I post a question effectively?), rather than repeat "do it for me" type requests.

Re: reduce code
by shmem (Chancellor) on Mar 11, 2017 at 19:31 UTC
    I am new to perl and somehow I wrote the code in perl.

    You would be the first perl beginner of all time to concoct such a piece of code. If you did.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Golf code in perl
by thospel (Hermit) on Jan 22, 2018 at 23:28 UTC
    Oooh, a golf thread. also 44 strokes:

    m? ?||map$\+=2*$s[$_%$']++,split for<>;print

    In older perls you could leave out the initial m and it would have been 43

      And for 40:

      print<>!~($/=$")+2*map 1..$s{$_%$'}++,<>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found