
|
View Full Version : php challenge
conanqtran 11-03-2006, 09:09 AM 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:
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
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 :)
nnormal 11-03-2006, 11:50 AM 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 ;) )
sasha 11-03-2006, 12:10 PM 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?
conanqtran 11-03-2006, 07:44 PM 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 ;)
mwatkins 11-03-2006, 09:29 PM 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.
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.
conanqtran 11-03-2006, 10:10 PM 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....
mwatkins 11-03-2006, 10:34 PM 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!
Xenatino 11-03-2006, 10:38 PM 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
conanqtran 11-04-2006, 09:00 AM 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 :D had to use decbin, substr_count along with other useful php string function, thanks for all the suggestion guys :agree:
mwatkins 11-04-2006, 10:20 AM Terrific! Good going!
|