Page 1 of 1
Maximum occurance
Posted: Mon Oct 26, 2009 8:30 am
by psaha
echo the word of the sentence whose number of occurrences is maximum..
Just see the code:
Code: Select all
<?php
$sen="I have a pot, yes pot. DO u have pot? no pot. buy some pot more pot. I like pot. pot is now stop.";
$wordcounts = array();
$words = explode( " ", $sen );
foreach( $words as $word )
{
$word = strtolower( $word );
}
$wc = array_keys( $wordcounts );
sort( $wc );
echo $word;
?>
Output should be "pot" . but it shows only the last word..
waiting for reply !!!
*******************
Q2: Is there any function that can find even and odd number?
Q3: How can I display unique numbers?
like:
Code: Select all
<?
$numbers="152363214";
phpfunction($numbers);
echo $numbers;
?>
output will be: 152364
What is the name of that phpfunction?
Re: Maximum occurance
Posted: Mon Oct 26, 2009 10:41 am
by Mark Baker
Look at str_word_count() combined with array_count_values() to count the number of instances of each word in a sentence or paragraph, and to get the most frequently used word.
Code: Select all
$sen="I have a pot, yes pot. DO u have pot? no pot. buy some pot more pot. I like pot. pot is now stop.";
$words = array_count_values(str_word_count($sen,1));
$mostFrequentlyUsed = array_shift(array_keys($words));
Code: Select all
$number = 12345;
if (($number % 2) == 0) {
echo 'Even';
} else {
echo 'Odd';
}
I assume you mean digirts which only appear once in your number
Code: Select all
$numbers ="152363214";
$singleDigits = array_intersect(array_count_values(str_split($numbers)),array_fill(0,9,1));
print_r($singleDigits);
Re: Maximum occurance
Posted: Mon Oct 26, 2009 11:01 am
by AbraCadaver
Looks like homework, but here it goes:
Q1:
Quick hack:
Code: Select all
$sen = "I have a pot, yes pot. DO u have pot? no pot. buy some pot more pot. I like pot. pot is now stop.";
$sen = preg_replace("/[^A-z ]/", '', $sen);
$words = explode( " ", $sen);
$wordcounts = array();
foreach( $words as $word )
{
$word = strtolower( $word );
if(isset($wordcounts[$word])) {
$wordcounts[$word]++;
} else {
$wordcounts[$word] = 1;
}
}
arsort($wordcounts);
$maxword = key($wordcounts);
Q2:
Divide by 2:
Code: Select all
if ($number % 2 == 0) {
echo "EVEN";
} else {
echo "ODD";
}
Q3:
I might use a regex, but here:
Code: Select all
$numbers="152363214";
$digits = implode(array_flip(array_flip(str_split($numbers))));
-Shawn
Re: Maximum occurance
Posted: Mon Oct 26, 2009 1:06 pm
by AbraCadaver
Q3 (alternative):
Code: Select all
$numbers="152363214";
$digits = count_chars($numbers, 3);
-Shawn
Re: Maximum occurance
Posted: Mon Oct 26, 2009 1:10 pm
by John Cartwright
Q2:
Divide by 2:
Code: Select all
if ($number % 2 == 0) {
echo "EVEN";
} else {
echo "ODD";
}
FYI, that's not division, it's the modulus (remainder) operator
Re: Maximum occurance
Posted: Mon Oct 26, 2009 1:13 pm
by AbraCadaver
John Cartwright wrote:Q2:
Divide by 2:
Code: Select all
if ($number % 2 == 0) {
echo "EVEN";
} else {
echo "ODD";
}
FYI, that's not division, it's the modulus (remainder) operator
Correct on the operator, however it is dividing by 2 and returning the remainder.
Re: Maximum occurance
Posted: Mon Oct 26, 2009 1:29 pm
by Mirge
AbraCadaver wrote:John Cartwright wrote:Q2:
Divide by 2:
Code: Select all
if ($number % 2 == 0) {
echo "EVEN";
} else {
echo "ODD";
}
FYI, that's not division, it's the modulus (remainder) operator
Correct on the operator, however it is dividing by 2 and returning the remainder.
So? $number %2 and $number / 2 are very different operations
