CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Fun With Perl: Perl Golf:


Administrator / Moderator

Feb 18, 2002, 9:07 PM

Post #1 of 3 (41157 views)
Strings-to-Array Can't Post

Here's one from a great site, posted by our very own [ japhy ]. Spoiler at [url=]

Here's the goal: make a function that takes any number of equal-length strings, and returns an array of strings, where each string is the concatenation of the Nth character in each string.

Sample input: qw( jeff john mary )
Sample output: qw( jjm eoa fhr fny )

Here are my attempts:

# 51 chars 
sub s2a{@_=@_;reverse+map{join"",map+chop,@_}1..length$_[0]}

# 51 chars
sub s2a{@_=@_;map{join"",map+s/(.)//s&&$1,@_}1..length$_[0]}

# 48 chars
sub s2a{map{join"",map+s/(.)//s&&$1,@_=@_}1..length$_[0]}

# 47 chars
sub s2a{map{join"",map{s/(.)//s;$1}@_=@_}1..length$_[0]}

# 45 chars
sub s2a{map{join"",map{s/.//s;$&}@_=@_}1..length$_[0]}


Feb 19, 2002, 5:07 AM

Post #2 of 3 (41148 views)
Re: [Jasmine] Strings-to-Array [In reply to] Can't Post

This is the first attempt at golf in my life... BlushBlushBlush

The offered solution surely is inferior to the offered solutions (score: 56),

but there might be some idea that will hopefully help somebody else to score better result Crazy

[perl]sub s2a{for(@a=@_){$_=split//;@r[$_-1].=shift@_ for 1..$_};@_=@r}[/perl]

No use to put it in the spoiler Frown

Jean Spector
SQA Engineer @ Exanet

There are only 10 types of people in the world -
Those who understand binary, and those who don't.

Enthusiast / Moderator

Feb 19, 2002, 2:20 PM

Post #3 of 3 (41141 views)
Re: [Jasmine] Strings-to-Array [In reply to] Can't Post

Just a minor improvement:

# 41 chars 

Can we assume that the input strings do not contain newlines?
Then the /s modifier would not be neccessary.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives