Results 1 to 10 of 10

Thread: php challenge

  1. #1
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217

    php challenge

    Hi guys,

    I'm facing the programming challange and was hoping to get some help

    I need in my script to check if one string is an abbreviation of another, so i write a function to return value like this:

    PHP Code:
    function is_abbreviation($string1$string2)
    {
        
    $split_text preg_split ("/\s+/"$string2);
        
        if (
    is_array($split_text))
        {
            foreach (
    $split_text as $word)
            {
                
    $simple_result .= $word{0};
            }

            if (
    strcmp($string1,$simple_result)==0)
            {
                
    $result TRUE;
                
            }else{
                
                
    $result FALSE;
            }
        }
        
        return 
    $result;


    The problem with the function above is that it only work like this:

    say if you run

    PHP Code:
    is_abbreviation("CSI""Crime Scene Investigation" 
    that would work fine, however if you enter FedEx and agument 1 and Federal Express as agurment too, it would need to work too. So basically string1 may consist one or more letters from the beginning of each word in the list of string 2

    also string1 they may take any part of any word of string2 as long as the letters appear in the original presented order

    How would I extend this function do to all that?

    Any help, suggestions are much appreciated

  2. #2
    What are the rules exactly? The first $n number of characters of the abbreviation after the capitol letter have to be in the same as those in the words in the full name? Also how would you deal with a recursive abbreviation like PHP Hypertext Processor? (sorry about that last one but I had to throw it out there )

  3. #3
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3,100
    also string1 they may take any part of any word of string2 as long as the letters appear in the original presented order
    So In that case FedEx would be accepted abbreviation for FEDEral boX too , correct ?

    More questions:

    Does case matter ?
    If it does above would not be correct

    Does abbreviation have to include at least one character from every word?

  4. #4
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217
    well, my function need to return true or false with the return type too, i forgot to mention that, so basically if:

    - The abbreviation is formed by taking the first letter of each word in the list => return true with the type SIMPLE

    - The abbreviation consist of one or more letters from the beginning of each word in the list. => return true with type COMPLEX

    - Substring abbreviations, they may take any part of any word as long as the letters appear in the original presented order. => return true with type SUBSTRING

    so my function would return an array like this: $anwser('result' => TRUE, 'type' => SIMPLE) depending on the agurment 1 and 2 passed to the function

    hope that clear enough for you guys, at first it seems to be quite easy but as i drive into it, it doesn't seek so at all

  5. #5
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    First off I'd break it down into smaller chunks. There's no reason why your "function" can't call several smaller ones along the way. Here's the first chunk in Python.

    Code:
    def is_simple(abbr, phrase):
        a_list = [c for c in abbr.lower()]
        p_initials = [w[0] for w in phrase.lower().split()]
        return a_list == p_initials
    
    ->> is_simple('CSI', 'Crime Scene Investigation')
    True
    What the above does is turn both arguments into lists
    a_list == ['C','S','I']
    p_initials == ['C','S','I']
    a_list == p_initials

    The complex and substring matches are just variations on the same theme. Might be easier to write them as seperate functions, and then, once you have that visualized or working, consider if it even makes sense to consolidate to one general purpose function.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  6. #6
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217
    sasha

    So In that case FedEx would be accepted abbreviation for FEDEral boX too , correct ?
    that would not be accepted actually because:

    The abbreviation consist of one or more letters from the beginning of each word in the lis
    Does case matter ?
    For now, i'm only doing case-insensitive

    nnormal, please see my second post for the rules

    mwatkins, i think the complex and substring are not as simple though, the first string would need to be broken down to a list as you suggested, then compare each of the letter to each of the word in the second string, they would need to match and also match the order that they appear.

    For example, each of the letter in FedExp would need to match each of the letter in the sentence Federal Express, so the Fed and Fede would match the first word, then we have to look at the second word and see that Exp match since its the begining of Express...

    I hope this make sense to everyone....

  7. #7
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Just scan the phrase with a string build up incrementally from the abbr. Watch how the | bar moves along, with the until the very *first* match of both halves.

    Given FedEx, Federal Express

    F|edEx - True|False
    Fe|dEx - True|False
    Fed|Ex - True|True

    Once you've determined how to solve this for a two-component abbreviation, I believe you'll find it easy to extend to n-components.

    Can't do the whole thing for you right

    keep going!
    Last edited by mwatkins; 11-03-2006 at 10:43 PM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  8. #8
    Join Date
    Oct 2004
    Location
    UK
    Posts
    487
    Quote Originally Posted by nnormal
    What are the rules exactly? The first $n number of characters of the abbreviation after the capitol letter have to be in the same as those in the words in the full name? Also how would you deal with a recursive abbreviation like PHP Hypertext Processor? (sorry about that last one but I had to throw it out there )
    Just to correct you, PHP actually stands for Hypertext Preprocessor

  9. #9
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217
    Quote Originally Posted by mwatkins
    Just scan the phrase with a string build up incrementally from the abbr. Watch how the | bar moves along, with the until the very *first* match of both halves.

    Given FedEx, Federal Express

    F|edEx - True|False
    Fe|dEx - True|False
    Fed|Ex - True|True

    Once you've determined how to solve this for a two-component abbreviation, I believe you'll find it easy to extend to n-components.

    Can't do the whole thing for you right

    keep going!
    Hi mwatkins, i took your approach and finally got it had to use decbin, substr_count along with other useful php string function, thanks for all the suggestion guys

  10. #10
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Terrific! Good going!
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •