Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.