Vivid Lust Posted December 9, 2008 Share Posted December 9, 2008 Had this problem for Informatic Olympics, Couldnt do it!!! Any ideas on how to do it with php?? Problem: A digit word is where, after possibly removing some letters, you are left with one of the single digits: ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE. For example: BOUNCE and ANNOUNCE are digit words, since they contain the digit ONE ENCODE is not a digit word, even though it contains an O, N and E, since they are not in order. Write a program which reads in a single upper-case word (with at the most 15 letters) and determines if it is a digit word. If the word is not a digit word, you should outpud NO, If the word is a digit word, you should output the digit it contains, as a number. Sample run 1 BOUNCE 1 Sample run 2 ENCODE NO Link to comment https://forums.phpfreaks.com/topic/136194-digit-words/ Share on other sites More sharing options...
Adam Posted December 9, 2008 Share Posted December 9, 2008 I actually have something very similar to this.. I've modded it a bit for you but you'll probs need to change it some more as at the moment it returns an array of the matched digits... <?php function matchDigits($input) { $input = strtoupper($input); $digits = array('ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'); foreach ($digits as $digit) { $chars = str_split(strtoupper($digit)); foreach ($chars as $key => $char) { $pos = strpos($input, $char); $prev = -1; if ($pos === false || $pos < $prev) { break; } else { if ((count($chars) - 1) == $key) { $matched[] = $digit; } else { $prev = $pos; } } } } return $matched; } print '<pre>'; print_r(matchDigits('one two three')); print_r(matchDigits('announce')); print '</pre>'; ?> Adam Link to comment https://forums.phpfreaks.com/topic/136194-digit-words/#findComment-710434 Share on other sites More sharing options...
Adam Posted December 9, 2008 Share Posted December 9, 2008 Somebody out there might be able to offer you a quicker solution, perhaps using regular expressions? I don't know! Link to comment https://forums.phpfreaks.com/topic/136194-digit-words/#findComment-710436 Share on other sites More sharing options...
Mark Baker Posted December 9, 2008 Share Posted December 9, 2008 <?php $numberWords = array( 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN' ); $wordList = array( 'BOUNCE', 'ANNOUNCE', 'ENCODE', 'GAZEBO', 'OFTEN', 'OFTENNER' ); foreach($wordList as $testWord) { echo 'Word is '.$testWord.'<br />'; $testWordArray = str_split(strtoupper($testWord)); $results = array(); foreach($numberWords as $numberWord) { $numberWordArray = str_split(strtoupper($numberWord)); $intersected = array_intersect($testWordArray,$numberWordArray); $testChar = 0; foreach($intersected as $key => $letter) { if ($letter != $numberWordArray[$testChar]) { unset($intersected[$key]); } else { $testChar++; } } if (implode($intersected) == implode($numberWordArray)) { $results[] = $numberWord; } } $fullResults = array(); if (count($results) > 0) { foreach($results as $result) { $fullResults[] = array_search($result,$numberWords) + 1; } echo implode(',',$fullResults); } else { echo 'No'; } echo '<hr />'; } ?> Link to comment https://forums.phpfreaks.com/topic/136194-digit-words/#findComment-710439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.