Maximum occurance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psaha
Forum Newbie
Posts: 16
Joined: Mon Oct 26, 2009 4:11 am

Maximum occurance

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Maximum occurance

Post 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);
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Maximum occurance

Post by AbraCadaver »

Looks like homework, but here it goes: 8O

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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Maximum occurance

Post by AbraCadaver »

Q3 (alternative):

Code: Select all

$numbers="152363214";
$digits = count_chars($numbers, 3);
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Maximum occurance

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Maximum occurance

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Maximum occurance

Post 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 :)
Post Reply