Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Golf/Perlish solution to this problem?

by hipowls (Curate)
on Jan 31, 2008 at 02:36 UTC ( #665263=note: print w/replies, xml ) Need Help??


in reply to Golf/Perlish solution to this problem?

If you are golfing then

@n = map { $n + 1 - $_, $_ } 1 .. $n / 2;
if it the divisible by 4 constraint wasn't present then a little more care is required
use integer; @n = map { $n + 1 - $_, $_ } 1 .. $n / 2; push @n, $n / 2 + 1 if $n % 2;

Update: As kindly pointed out by fellow monks I misread the specs. For the sake of completeness I give a correct (I hope) solution in the same vein as the previous offerings.

@n = map { $n + 2 - 2 * $_, 2 * $_ - 1, 2 * $_, $n + 1 - 2 * $_ } 1 .. + $n / 4;
Unfortunately it's not as golfy but it is still cryptic looking;-)

Replies are listed 'Best First'.
Re^2: Golf/Perlish solution to this problem?
by ikegami (Pope) on Jan 31, 2008 at 02:51 UTC
    That gives 81726354 instead of 81276345

      Silly me, I misread the problem.

Re^2: Golf/Perlish solution to this problem?
by GrandFather (Saint) on Jan 31, 2008 at 02:59 UTC

    Except it gives the wrong answer:

    Wanted: 8 1 2 7 6 3 4 5 Given: 8 1 7 2 6 3 5 4

    Less important - the OP seems to want the array elements in that order rather than just a numeric sequence, but that's trivial to fix by using a slice.


    Perl is environmentally friendly - it saves trees
      Less important - the OP seems to want the array elements in that order rather than just a numeric sequence

      Except that the array elements that the OP wants in that order are: "I have the numbers 1 to n". I doubt s/he cares whether it is his/her original 1..n or just a generated numeric sequence. Just FYI.

      - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-01-15 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?