Avatar of 4d47
0
1
Genius
0
0

4d47's solution

to Word Count in the Perl 6 Track

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

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

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.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use WordCount;
plan 11;

my Version:D $version = v3;

if WordCount.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nWordCount is {WordCount.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;
is-deeply (% = .<input><sentence>.&count-words), |.<expected description> for @($c-data<cases>);

=head2 Canonical Data
=begin code
{
  "exercise": "word-count",
  "version": "1.2.0",
  "comments": [
    "For each word in the input, count the number of times it appears in the",
    "entire sentence."
  ],
  "cases": [
    {
      "description": "count one word",
      "property": "countWords",
      "input": {
        "sentence": "word"
      },
      "expected": {
        "word": 1
      }
    },
    {
      "description": "count one of each word",
      "property": "countWords",
      "input": {
        "sentence": "one of each"
      },
      "expected": {
        "one": 1,
        "of": 1,
        "each": 1
      }
    },
    {
      "description": "multiple occurrences of a word",
      "property": "countWords",
      "input": {
        "sentence": "one fish two fish red fish blue fish"
      },
      "expected": {
        "one": 1,
        "fish": 4,
        "two": 1,
        "red": 1,
        "blue": 1
      }
    },
    {
      "description": "handles cramped lists",
      "property": "countWords",
      "input": {
        "sentence": "one,two,three"
      },
      "expected": {
        "one": 1,
        "two": 1,
        "three": 1
      }
    },
    {
      "description": "handles expanded lists",
      "property": "countWords",
      "input": {
        "sentence": "one,\ntwo,\nthree"
      },
      "expected": {
        "one": 1,
        "two": 1,
        "three": 1
      }
    },
    {
      "description": "ignore punctuation",
      "property": "countWords",
      "input": {
        "sentence": "car: carpet as java: javascript!!&@$%^&"
      },
      "expected": {
        "car": 1,
        "carpet": 1,
        "as": 1,
        "java": 1,
        "javascript": 1
      }
    },
    {
      "description": "include numbers",
      "property": "countWords",
      "input": {
        "sentence": "testing, 1, 2 testing"
      },
      "expected": {
        "testing": 2,
        "1": 1,
        "2": 1
      }
    },
    {
      "description": "normalize case",
      "property": "countWords",
      "input": {
        "sentence": "go Go GO Stop stop"
      },
      "expected": {
        "go": 3,
        "stop": 2
      }
    },
    {
      "description": "with apostrophes",
      "property": "countWords",
      "input": {
        "sentence": "First: don't laugh. Then: don't cry."
      },
      "expected": {
        "first": 1,
        "don't": 2,
        "laugh": 1,
        "then": 1,
        "cry": 1
      }
    },
    {
      "description": "with quotations",
      "property": "countWords",
      "input": {
        "sentence": "Joe can't tell between 'large' and large."
      },
      "expected": {
        "joe": 1,
        "can't": 1,
        "tell": 1,
        "between": 1,
        "large": 2,
        "and": 1
      }
    },
    {
      "description": "multiple spaces not detected as a word",
      "property": "countWords",
      "input": {
        "sentence": " multiple   whitespaces"
      },
      "expected": {
        "multiple": 1,
        "whitespaces": 1
      }
    }
  ]
}
=end code
unit class Word_Counter;

method count_words(Str $phrase --> Hash) {
    $phrase.lc.comb(/\w+/).Bag.Hash
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?