Jump to content

Digit Words


Vivid Lust

Recommended Posts

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

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.