🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of giuseppegargani

giuseppegargani's solution

to Word Count in the PHP Track

Published at Dec 09 2020 · 0 comments
Instructions
Test suite
Solution

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar word-count/word-count_test.php
    

Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

word-count_test.php

<?php

class WordCountTest extends PHPUnit\Framework\TestCase
{
    public static function setUpBeforeClass() : void
    {
        require_once 'word-count.php';
    }

    public function testCountOneWord() : void
    {
        $this->assertEquals(['word' => 1], wordCount('word'));
    }

    public function testCountOneOfEachWord() : void
    {
        $this->assertEquals(['one' => 1, 'of' => 1, 'each' => 1], wordCount('one of each'));
    }

    public function testMultipleOccurrencesOfAWord() : void
    {
        $this->assertEquals(
            ['one' => 1, 'fish' => 4, 'two' => 1, 'red' => 1, 'blue' => 1],
            wordCount('one fish two fish red fish blue fish')
        );
    }

    public function testIgnorePunctuation() : void
    {
        $this->assertEquals(
            ['car' => 1, 'carpet' => 1, 'as' => 1, 'java' => 1, 'javascript' => 1],
            wordCount('car : carpet as java : javascript!!&@$%^&')
        );
    }

    public function testIncludeNumbers() : void
    {
        $this->assertEquals(['1' => 1, '2' => 1, 'testing' => 2], wordCount('testing, 1, 2 testing'));
    }

    public function testNormalizeCase() : void
    {
        $this->assertEquals(['go' => 3, 'stop' => 2], wordCount('go Go GO Stop stop'));
    }

    public function testCountsMultiline() : void
    {
        $this->assertEquals(['hello' => 1, 'world' => 1], wordCount("hello\nworld"));
    }

    public function testCountsTabs() : void
    {
        $this->assertEquals(['hello' => 1, 'world' => 1], wordCount("hello\tworld"));
    }

    public function testCountsMultipleSpacesAsOne() : void
    {
        $this->assertEquals(['hello' => 1, 'world' => 1], wordCount('hello  world'));
    }

    public function testDoesNotCountLeadingOrTrailingWhitespace() : void
    {
        $this->assertEquals(['introductory' => 1, 'course' => 1], wordCount("\t\tIntroductory Course      "));
    }
}
<?php
//PRINT_R  stampa anche array!!!! con gli elementi
//EXPLODE NEL MANUALE DI PHP https://www.php.net/manual/en/function.explode.php
//il fatto è che non cerca le singole parole ma le sottostringhe
//MEGLIO USARE LE ESPRESSIONI REGOLARI PER SPLIT UNA STRINGA!!! PREG_SPLIT
//https://www.php.net/manual/en/function.preg-split.php
//una espressione Regex che usa un operatore OR
//https://www.php.net/manual/en/function.array-count-values.php

function wordcount(string $input){
  //sostituisce questi elementi con uno spazio
  $filtrSpazi= preg_replace("/\\[tn]/", ' ', $input); //correttamente sostituisce

  //elimina spazi iniziali senza funzione specifica, punteggiatura e spazi finali
  $filtrata= strtolower(preg_replace('/^\s+||[^a-z0-9\s]||\s+$/i', '', $filtrSpazi));
  return array_count_values(preg_split("/\s+/",$filtrata));
}
?>

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?