Jump to content

Directory Enquiries Challenge


Recommended Posts

Not such a difficult challenge, but I thought this would make an interesting diversion for some of you budding developers. The object of the challenge is simply to search for a phone number within a 1D array of international phone numbers, with or without country codes. The tricky part is that you don't know anything about the number's location, user location or number formatting. False positive matches are therefore allowed. With, or without, research; you are required to produce an algorithm that gets it right most of the time. Use as few lines of code as you can and any trick you can think of to simplify the task. Can you come up with the best approach? :think:

Edited by czardas
Link to comment
Share on other sites

  • Moderators

Good challenge. Perhaps, if you are targeting some of the more junior forum members, you should provide the array so they can work out only the search function. Just my 2¢

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Okay, an array can be provided. First I would like people to think about it for a short while - including myself. Perhaps you could PM me some typical American numbers that I wouldn't know. The only American number I know is 911. I also need to research this a little myself. :tv_horror:

[that was quick :D]

Edited by czardas
Link to comment
Share on other sites

As a start, I have generated a sample array with some random phone numbers (mostly ficticious):

#include <Array.au3>

Local $aArray = _
    ['+262 692 12 03 00', '1800 251 996',    '+1 994 951 0197', _
    '091 535 98 91 61',   '2397865',         '08457 128276', _
    '348476300192',       '05842 361774',    '0-800-022-5649', _
    '15499514891',        '0096 363 0949',   '04813137349', _
    '06620 220168',       '07766 554433',    '047 845 44 22 94', _
    '0435 773 4859',      '(01) 882 8565',   '00441619346434', _
    '09314 367090',       '0 164 268 0887',  '0590995603', _
    '991',                '0267 746 3393',   '064157526153', _
    '0 719 829 7756',     '+1-541-754-3012', '+441347543010', _
    '03890 978398',       '(31) 10 7765420', '020 8568 6646', _
    '0161 934 6534',      '0 637 915 1283',  '+44 207 882 8565', _
    '0800 275002',        '0750 646 9746',   '982-714-3119', _
    '000 300 74 52 40',   '023077529227',    '1 758 441 0611', _
    '0183 233 0151',      '02047092863',     '+44 20 7946 0321', _
    '04935 410618',       '048 257 67 60 79']

    _ArrayDisplay($aArray)

The basic challenge, for those of you who have only been coding for a year or two, is as follows: Write a piece of code which attempts to find the following phone numbers by searching through the array.

882 8565
123 8762
7543010
07843 543287
00441619346534
+44208.....missing numbers [optional task]
0015417543012

The challenge is open to anyone, but I hope more advanced coders will give beginners a chance to solve this before suggesting possible alternatives. Discussion is allowed. It is not intended that you study all country dialing codes, although you might want to take a look here to give you some ideas:
https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers.
I am looking for a simple solution with just a few lines of code, rather than a complicated script. The method you choose is as important as your coding skills. Notice that one of the numbers you are meant to find (optional task) is incomplete - perhaps found on a torn piece of paper.

Good luck to everyone.

Edited by czardas
slight modification
Link to comment
Share on other sites

So the challenge is to fuzzy-search for anything that sort of resembles a phone number? Doesn't this come down more to some smart pattern definitions than to a scripting solution?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

5 hours ago, SadBunny said:

So the challenge is to fuzzy-search for anything that sort of resembles a phone number? Doesn't this come down more to some smart pattern definitions than to a scripting solution?

There must be several ways to approach the problem. It is typical of the types of problem encountered with search engine design, only a bit simpler.
 

4 hours ago, Somerset said:

What do we win, besides an illicit love affair with the phone operator that is now a freaking robot? :huh:

You won't win! :P

Edited by czardas
Link to comment
Share on other sites

I just made searching for the incomplete number +44208..... an optional task. The dialing code gets you through to London, but finding matches for numbers which are half missing is not the main point of this exercise. Perhaps I should have left it out. Just try to find the others.

Link to comment
Share on other sites

I've been scripting for a fair few years, and I don't even understand the question :(

None of those numbers to search are in the list.

Could you expand? you know for all those noobs out there :huh2:

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

@JohnOne the first number is found at array index 16. The prefix (01) might not be required for an internal call, but there is no way to know which country the number is in for this particular example.

Hopefully someone will drop a hint or two before long. :)

Edited by czardas
Link to comment
Share on other sites

Wow, this is going to be a hard task for people with Obsessive compulsive disorder, OCD people like to do it perfectly and neatly :P.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Fuzzy search (lookup search feature here) and regexp will smash such a  mosquito.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I think coming up with an arrangement of numbers that cant be a phone number is difficult.  Even saying 'less than this many digits' is bound to be undone by some weird country code and pin requirements.  even sticking with purely public numbers you could at max maybe get away with a single criteria of a minimum of 3 digits (in the U.S. those are generally  "*11").

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I guess there will always be some peculiar exceptions (ignore internal extensions unless you want to include them). After looking through the lists of standard number formats, I would say that any number less than 7 digits 6 or maybe 4 digits should only return an exact match (to be dialed within the territory). You wouldn't expect someone to dial the emergency services in the US if they are calling from Japan. I'm not sure it would even be possible to do this. Beyond 7 digits, the number is likely to contain some outer-regional code possibly preceded by a country code etc. Sometimes the digit zero is dropped immediately after the country dialing code (I'm not sure if this is always the case), so there are a few things to latch on to and watch out for.

Edited by czardas
Link to comment
Share on other sites

#include <array.au3>
#include <file.au3>

Opt ('MustDeclareVars', 1)

Local $aPhoneNumbers = _
    ['+262 692 12 03 00', '1800 251 996',    '+1 994 951 0197', _
    '091 535 98 91 61',   '2397865',         '08457 128276', _
    '348476300192',       '05842 361774',    '0-800-022-5649', _
    '15499514891',        '0096 363 0949',   '04813137349', _
    '06620 220168',       '07766 554433',    '047 845 44 22 94', _
    '0435 773 4859',      '(01) 882 8565',   '00441619346434', _
    '09314 367090',       '0 164 268 0887',  '0590995603', _
    '991',                '0267 746 3393',   '064157526153', _
    '0 719 829 7756',     '+1-541-754-3012', '+441347543010', _
    '03890 978398',       '(31) 10 7765420', '020 8568 6646', _
    '0161 934 6534',      '0 637 915 1283',  '+44 207 882 8565', _
    '0800 275002',        '0750 646 9746',   '982-714-3119', _
    '000 300 74 52 40',   '023077529227',    '1 758 441 0611', _
    '0183 233 0151',      '02047092863',     '+44 20 7946 0321', _
    '04935 410618',       '048 257 67 60 79']

; Reference: http://www.studyabroad.com/resources/international-telephone-country-codes / Country codes saved in countries.txt CSV format (;) seperator is used.
Local $aPhoneCountry
_FileReadToArray (@ScriptDir & "countries.txt", $aPhoneCountry, $FRTA_NOCOUNT)
if @error Then
    Msgbox (0, "_FileReadToArray", @Error & " occured while trying to read: " & @ScriptDir & "countries.txt")
    Exit 1
EndIf

Local $aCountryCode, $strPhoneNumber, $bPrint = False
For $i = 0 To UBound ($aPhoneNumbers) - 1
    $strPhoneNumber = StringReplace ( $aPhoneNumbers [$i], "+", "" )
    $strPhoneNumber = StringReplace ( $strPhoneNumber, " ", "" )
    $strPhoneNumber = StringReplace ( $strPhoneNumber, "-", "" )
    For $j = 0 to UBound ( $aPhoneCountry ) - 1
        $aCountryCode = StringSplit ($aPhoneCountry[$j], ";")
        If StringInStr ( $strPhoneNumber, $aCountryCode [1] ) = 1 Then
            $bPrint = True
            ConsoleWrite ( "==> " & $aCountryCode [1] & " " & $aCountryCode [2] & "Phone Number: " & StringRight ( $strPhoneNumber, StringLen ( $strPhoneNumber ) - StringLen ( $aCountryCode [1] ) ) & @CRLF )
        EndIf
    Next
    If $bPrint = False Then ConsoleWrite ( "> " & $strPhoneNumber & @CRLF )
    $bPrint = False
Next

Ok newbie One (me) :P starts: this is totally not perfect but a approach. Namely first detect the Country dial-numbers. Everything after the Country number is likely to be a phone number.

 

 

Link to comment
Share on other sites

Gief me an array that includes a bunch of number strings you dont believe are phone numbers, and ensure those are excluded, plz.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@pluto41 Thanks for your effort. There is no guarantee that a number has a country code included. You should probably start the search from the other end, backtracking through the digits. :idea: That's all the tips I have at my disposal right now! :)

@iamtheky What are you on about?

4 hours ago, czardas said:

I would say that any number less than 7 digits should only return an exact match

Note to self - Seven digits is too many: perhaps less than six digits should return an exact match - both numbers being five or less digits. In some places you might not need to dial an area code at all, so this is debatable.

Edited by czardas
Link to comment
Share on other sites

@pluto41 Thanks for your effort. There is no guarantee that a number has a country code included. You should probably start the search from the other end, backtracking through the digits. :idea: That's all the tips I have at my disposal right now! :)

@iamtheky What are you on about?

4 hours ago, czardas said:

I would say that any number less than 7 digits should only return an exact match

Note to self - Seven digits is too many: perhaps less than six digits should return an exact match - both numbers being five or less digits. In some places you might not need to dial an area code at all, so this is debatable.

Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...