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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf:
String Replacement Golf

 



fashimpaur
User / Moderator

Feb 14, 2002, 7:16 AM

Post #1 of 19 (47775 views)
String Replacement Golf Can't Post

Okay. Since there isn't much happening today at the forum, it's time for some more
fun. I recently ran into a problem where the version of Perl running on a web server
was 5.002. In that version, the substr overload of the type
substr EXPR,OFFSET,LEN,REPLACEMENT was not defined yet. The code that was
being written required a string of substantial length to have a portion of it replaced
with a new value. I wrote a function overload myself for substr to enable this to
be executed in the code.

The Task

Create a method mysubstr to perform the same task as explained above in as
few characters as possible. It must be reusable for any inputs (no hard coding).
The substring replacement is going to be assumed to be placed within the bounds
of the original expression. There is no need to check to see if you will exceed the
length of the original expression. We just don't care.

The Rules

1). The method need only work for any positive OFFSET or LEN (You don't have to work
backwards).


2). The method returns the modified string.

Common Input Parameters: (So we all start fair):

$a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; # EXPR
$b = 15; # OFFSET
$c = "Perl_Is_Fun"; # REPLACEMENT
$d = length $c; # LEN

# the method would be called as #
$e = mysubstr($a, $b, $d, $c);
# test result #
print $e;
# (expecting: abcdefghijklmnoPerl_Is_FunABCDEFGHIJKLMNOPQRSTUVWXYZ) #

You are given the following characters for free:
sub mysubstr{}

All characters between the curly braces are counted.
All manipulation must be done within the curly braces. No cheating.

My answer (in it's worst looking form) took 60 characters.
Someone out there can do better. I am waiting to see your answers.

[spoiler]

sub mysubstr{$s=shift,$s=~s/^(.{$_[0]})(.{$_[1]})(.*)/\1$_[2]\3/,return$s};

[/spoiler]

Good Luck.

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 14, 2002, 8:45 AM

Post #2 of 19 (47771 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

You have:

(.{$_[1]})

....but $_[1] is Perl_Is_Fun isn't it?


(This post was edited by RedRum on Feb 14, 2002, 8:46 AM)


Paul
Enthusiast

Feb 14, 2002, 8:48 AM

Post #3 of 19 (47769 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

I managed to beat you by about 6 characters:


Code
sub mysubstr{$s=shift,$s=~s/^(.{$_[0]})(.{$_[1]})/\1$_[2]/,return$s};



freddo
User

Feb 14, 2002, 9:30 AM

Post #4 of 19 (47764 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

Hi,

Got this for now (43):
[spoiler]sub mysubstr{shift;/(.{$_[0]}).{$_[2]}(.*)/;"$1$_[1]$2";}[/spoiler]

freddo
;---


freddo
User

Feb 14, 2002, 9:36 AM

Post #5 of 19 (47763 views)
Re: [freddo] String Replacement Golf [In reply to] Can't Post

damn, 42, if i remove the last ";" where did my head go?
;---


fashimpaur
User / Moderator

Feb 14, 2002, 12:40 PM

Post #6 of 19 (47756 views)
Re: [RedRum] String Replacement Golf [In reply to] Can't Post

RedRum,

$s was $_[0] and was shift(ed) in.
$_[0] is now OFFSET,
$_[1] is now LEN,
$_[2] is now REPLACEMENT.

I am. Therefore I think?
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 14, 2002, 12:58 PM

Post #7 of 19 (47754 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

[perl]
sub {
#-----------------------------

print "I bet you can't do this\n\n";

@_=qw/1 2 3 4 5/;

print, for @_;

}
[/perl]


fashimpaur
User / Moderator

Feb 14, 2002, 1:21 PM

Post #8 of 19 (47753 views)
Re: [RedRum] String Replacement Golf [In reply to] Can't Post

RedRum,

Improving on freddo I get 39!

[spoiler]

sub mysubstr{$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'}

[/spoiler]

I also see your comment about what was in $_[2] was for freddo. Sorry.

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


fashimpaur
User / Moderator

Feb 14, 2002, 1:27 PM

Post #9 of 19 (47752 views)
Re: [RedRum] String Replacement Golf [In reply to] Can't Post

How do you call that? &();? No. I give. You win!
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 14, 2002, 2:11 PM

Post #10 of 19 (47751 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

Sorry that was unrelated to the thread.

I was trying to point out the new tag available to colorize perl code :)

[perl] some code here [/perl]


freddo
User

Feb 14, 2002, 3:23 PM

Post #11 of 19 (47748 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

Hi Fashimpaur,

we're both wrong... Crazy
first, i just saw i copy/pasted the wrong mysubstr from the perl debugger, it should have been:[spoiler]sub mysubstr{$_=shift;/(.{$_[0]}).{$_[2]}(.*)/;"$1$_[1]$2"}[/spoiler](which makes it 46 bytes)... and secondly you inverted $_[2] & $_[3] Wink

so..... who shorten it next?
freddo

updated: finally pasted the good sub, damn.
;---

(This post was edited by freddo on Feb 14, 2002, 3:31 PM)


Paul
Enthusiast

Feb 15, 2002, 1:57 AM

Post #12 of 19 (47742 views)
Re: [freddo] String Replacement Golf [In reply to] Can't Post

Can I ask what the point of the spoiler tag is?

I really don't get it :)


freddo
User

Feb 15, 2002, 3:49 AM

Post #13 of 19 (47739 views)
Re: [RedRum] String Replacement Golf [In reply to] Can't Post

Hi Redrum,

Well, i think it's useful if some one wants to play, he doesnt see the others solutions unless he wishes to. So the quiz isnt "spoilt", he can still enjoy searching his own answer.

Freddo
;---


Paul
Enthusiast

Feb 15, 2002, 3:50 AM

Post #14 of 19 (47738 views)
Re: [freddo] String Replacement Golf [In reply to] Can't Post

Ahhh now I see :)


fashimpaur
User / Moderator

Feb 15, 2002, 4:37 AM

Post #15 of 19 (47735 views)
Re: [freddo] String Replacement Golf [In reply to] Can't Post

freddo,

Sorry, my friend, but you are mistaken. It is you that has been
inverting $_[2] and $_[3]. If you see in the original post, the
method is called mysubstr($a, $b, $d, $c) not mysubstr($a, $b, $c, $d).
If I invert my input parameters like that, and swap the variables
that you say are swapped, my method is still 39 characters and produces the
desired output. Unsure

Dennis
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 15, 2002, 5:11 AM

Post #16 of 19 (47733 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

I think you have it wrong.....

$a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; # EXPR
$b = 15; # OFFSET
$c = "Perl_Is_Fun"; # REPLACEMENT
$d = length $c; # LEN

sub mysubstr{$s=shift,$s=~s/^(.{$_[0]})(.{$_[1]})(.*)/\1$_[2]\3/,return$s};


If $s is shift then $_[0] is 15 .....thats fine!

However $_[1] is Perl_Is_Fun which cannot go inside curly braces.

Whichever way you do it either $_[0] or $_[1] will be letters which is invalid.


(This post was edited by RedRum on Feb 15, 2002, 5:13 AM)


freddo
User

Feb 15, 2002, 5:56 AM

Post #17 of 19 (47729 views)
Re: [fashimpaur] String Replacement Golf [In reply to] Can't Post

Ooooops,

I'm sorry, you're absolutely right, the fact is i just copied the lines defining the variables and used a, b, c and d in that order... silly me.

btw, i came trying it this way, for now it's quite awful (59 and using a,b,c,d) and i'm just playing with it, if you're tempted give it a look:[spoiler]sub mysubstr{@x=split//,$_[0];@x[0..$_[1]-1],$_[2],@x[$_[1]+$_[3]..$#x]}[/spoiler]

Sorry again for saying you were wrong,
Freddo
;---


fashimpaur
User / Moderator

Feb 15, 2002, 6:07 AM

Post #18 of 19 (47728 views)
Re: [RedRum] String Replacement Golf [In reply to] Can't Post

My last solution was:

sub mysubstr{$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'}

the method is:

mysubstr(EXPR, OFFSET, LEN, REPLACEMENT);

Therefore the code reads:

Find in $_[0] a match of $_[1] number of any type of character, followed
by $_[2] number of any type of character. (The searching of the string
will stop as soon as the first match is found.)

Then concatenate the first parenthesized match and the replacement
($_[3]) and anything that followed the whole match and put the result
in $_. Since mysubstr is called as:

$e = mysubstr($a, $b, $d, $c), the correct result is printed.

Did I miss something here? My whole code test is:


Code
 $a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
# EXPR
$b = 15;
# OFFSET
$c = "Perl_Is_Fun";
# REPLACEMENT
$d = length $c;
# LEN
# the method would be called as #
$e = mysubstr($a, $b, $d, $c);
# test result #
print $e;
# (expecting: abcdefghijklmnoPerl_Is_FunABCDEFGHIJKLMNOPQRSTUVWXYZ) #
print "\n\n";
print length q!$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'!;
sub mysubstr{$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'}


Let me know if there is anything that does not conform to the
rules. I thought I did it right.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


(This post was edited by fashimpaur on Feb 15, 2002, 6:16 AM)


fashimpaur
User / Moderator

Feb 15, 2002, 6:09 AM

Post #19 of 19 (47727 views)
Re: [freddo] String Replacement Golf [In reply to] Can't Post

No problem. After all, this is only supposed to be for fun. Sly
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";

 
 


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

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