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:
Phone Number Input Fix Golf

 



fashimpaur
User / Moderator

Feb 12, 2002, 5:46 AM

Post #1 of 9 (46375 views)
Phone Number Input Fix Golf Can't Post

Crazy The problem:

Let's say you have an HTML form out on the Web for information gathering
for a business that will contact the customers requesting information by the
phone number they enter in a single text input box. The box does not restrict
character input in any way. We will assume for argument sake that the person
filling out the form will enter the phone number keeping all the area code digits
together, all the prefix digits together and all the extension digits together. We
will also assume it is a standard US 10-digit phone number. The person filling out
the form can use any number of characters or spaces between the groupings.

Tongue
The challenge:

In the fewest number of characters of perl code, print any phone number entered
to STDOUT always in the format ($_ can equal phone number from form):


sprintf
("(%.3s) %.3s-%.4s", $area, $prefix, $extension);

If all three parts are not present, the script must die (no message necessary for challenge).

To score, count all keystrokes in your code. 1 point for each keystroke. Fewest
points wins!


My best score is 41.

[spoiler]/(\d{3}).*(\d{3}).*(\d{4})/?print"($1) $2-$3":die;[/spoiler]

Good Luck.

P.S. Jasmine - The spoiler mnemonic is not working correctly. The text showed up until I turned it white.


Dennis

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


Paul
Enthusiast

Feb 12, 2002, 6:06 AM

Post #2 of 9 (46373 views)
Re: [fashimpaur] Phone Number Input Fix Golf [In reply to] Can't Post

Isn't that 50 not 41 Wink

How about


Code
split/\D+/;@_==3?print"($_[0]) $_[1]-$_[2]":die;


That is 48 :)


(This post was edited by RedRum on Feb 12, 2002, 6:14 AM)


fashimpaur
User / Moderator

Feb 12, 2002, 7:22 AM

Post #3 of 9 (46364 views)
Re: [RedRum] Phone Number Input Fix Golf [In reply to] Can't Post

My bust! I double quoted then got length() of string.
It did not count backslash characters! ARRGGHHH
Unsure

My score is 50!

Yours, at first blush looks okay. But...

What about when $_ = "1234567890"?

It died a horrible death.

Dennis

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


Paul
Enthusiast

Feb 12, 2002, 7:41 AM

Post #4 of 9 (46362 views)
Re: [fashimpaur] Phone Number Input Fix Golf [In reply to] Can't Post

Doh!

I kinda looked at this and assumed there were characters between the numbers...

>>
The person filling out
the form can use any number of characters or spaces between the groupings.
<<

I forgot that 0 is a number Blush


(This post was edited by RedRum on Feb 12, 2002, 7:41 AM)


Jasmine
Administrator / Moderator

Feb 12, 2002, 9:27 AM

Post #5 of 9 (46348 views)
Re: [fashimpaur] Phone Number Input Fix Golf [In reply to] Can't Post

fashimpaur,

I see that the colors were modified while composing/editing your post, which overrode the white. Perhaps you selected the entire post and changed the color at one or more points?

Here's the source from yours:

<DIV style="MARGIN-LEFT: 30px"><FONT color=white><PRE><BIG></FONT><FONT color=#ff0000><FONT size=2><FONT color=#000000><FONT color=#f3fffd>/(\d{3}).*(\d{3}).*(\d{4})/?print"($1) $2-$3":die</FONT></FONT><FONT color=#f9faff>;</FONT></FONT></FONT></FONT><FONT color=#000000><FONT size=2> </BIG></PRE></FONT></DIV>

Here's the source from a normal spoiler:

<DIV style="MARGIN-LEFT: 30px"><FONT color=white><PRE>text</PRE></FONT></DIV>

Am I mistaken?

Jasmine


unknownSym
stranger

Feb 12, 2002, 11:00 AM

Post #6 of 9 (46338 views)
Re: [RedRum] Phone Number Input Fix Golf [In reply to] Can't Post

I believe this will work: s/\D*//;s/(\d{3})(\d{3})/\($1\) $2-/;print;

43 chars.


Nevermind. I forgot about the length requirement.


(This post was edited by unknownSym on Feb 12, 2002, 11:01 AM)


Paul
Enthusiast

Feb 12, 2002, 11:15 AM

Post #7 of 9 (46334 views)
Re: [unknownSym] Phone Number Input Fix Golf [In reply to] Can't Post

It doesn't die either :)

...btw you don't need to escape () so thats 40.


(This post was edited by RedRum on Feb 12, 2002, 11:15 AM)


Coderifous
journeyman

Feb 12, 2002, 12:02 PM

Post #8 of 9 (46328 views)
Re: [unknownSym] Phone Number Input Fix Golf [In reply to] Can't Post

WinkUh oh. Houston, we have a problem.

Given input with non-digit characters in between digit groups, it doesn't function properly:

111-222-4444 becomes:

111-222-4444

Becuase that \D* is matching "0 or more characters that are not-digits" So when it reaches the first digit, it has satisfied the match.

Solution: Take away the * and add the repitition modifier:

s/\D//g;s/(\d{3})(\d{3})/($1) $2-/;print;

Voila! But the credit still goes to SymAngelic

--Jim


(This post was edited by Coderifous on Feb 12, 2002, 12:03 PM)


fashimpaur
User / Moderator

Feb 13, 2002, 5:15 AM

Post #9 of 9 (46319 views)
Re: [Coderifous] Phone Number Input Fix Golf [In reply to] Can't Post

Nice, but the rules state that if it is not a full ten digit phone
number, the process must die (no message necessary for golf).

Its great to have a low score, but you have to follow the rules.Shocked

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