Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Simple but thought-provoking programming tasks [OT]

by Old_Gray_Bear (Bishop)
on Apr 16, 2007 at 03:21 UTC ( [id://610254]=note: print w/replies, xml ) Need Help??


in reply to Simple but thought-provoking programming tasks [OT]

You might introduce them to the concepts of data validation by asking them to determine if a particular string of characters represents a telephone number.
  • 867-5309
  • 555 123-456
  • (555) 456 - 1234
  • +1 555-234-9874
  • 911
  • extra credit for:
    • +66 44-515-0123
    • +47 2291 2345
    • 02/xx xx xx xx (inside Czech Republic)
    • +420 (0)2/xx xx xx xx (outside Czech Republic)
There is an ISO standard (E.164) the details all the possibilities....

----
I Go Back to Sleep, Now.

OGB

  • Comment on Re: Simple but thought-provoking programming tasks [OT]

Replies are listed 'Best First'.
Re^2: Simple but thought-provoking programming tasks [OT]
by GrandFather (Saint) on Apr 16, 2007 at 03:38 UTC

    There is a draft version of the standard document here. Note that this is a download link for a Microsoft Word document.


    DWIM is Perl's answer to Gödel
Re^2: Simple but thought-provoking programming tasks [OT]
by Jenda (Abbot) on Apr 16, 2007 at 09:05 UTC
    • 02/xx xx xx xx (inside Czech Republic)
    • +420 (0)2/xx xx xx xx (outside Czech Republic)

    Actually not. The 0 has been dropped from the phone numbers some years ago. The original scheme was 4 to 8 numbers not starting with 0 for a local number (different districts had different number of phones and thus different number length) or 0 area_code local_number. 02 used to mean Prague. They increased the number of digits as necessary and made the area code required and thus the 0 unnecessary. So 02 12 34 56 78 became 212 345 678.

    Regarding +420 (0)2 ... the original country code was 42 so the numbers were +42 0 area_code local_number. After we split with the slovaks our code was "changed" to 420, they were assigned 421. So the international numbers became +420 area_code local_number. In either case +420 02/xx xx xx xx would have one too many zeroes I believe. (Sorry for this lengthy and largely pointless post.)

    In either case I think this is a bit too tricky task. Especially if you wanted them to accept the special numbers. 911 in US, 112,150,155,158 and probably a few others in Czech Rep and quite a few more in other countries. I think it's best not to be too strict while validating phone numbers. At least unless you can restrict them to a single country.

Re^2: Simple but thought-provoking programming tasks [OT]
by DrHyde (Prior) on Apr 17, 2007 at 10:42 UTC
    555 123 456 is an interesting one. It's obviously invalid because it's too short. But if you make it long enough by adding a digit on the end it's still invalid, because 1 is illegal in the fourth digit of NANP numbers. To be honest, this sort of thing, which requires esoteric domain-specific knowledge to get right, is not really suitable for a student exercise.

    But it does make a good teaching example, by starting with the most basic rules (is it the right length?) then add more and more complex ones - start with why the A and D digits can't be 0 or 1 (getting the students to figure out why this is the case is a useful exercise in itself, even without then encoding the rule in a program); then add the other special cases (eg, the BC digits can't be 11 and again get the students to figure out why) and finally wrap up with "and you don't have to do any of that work if you use perl because there's a CPAN module for it". That module is Number::Phone::NANP.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found