Avatar of artemkorsakov

artemkorsakov's solution

to Word Count in the C# Track

Published at May 02 2019 · 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

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit WordCount.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

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

WordCountTest.cs

// This file was auto-generated based on version 1.3.0 of the canonical data.

using System.Collections.Generic;
using Xunit;

public class WordCountTest
{
    [Fact]
    public void Count_one_word()
    {
        var actual = WordCount.CountWords("word");
        var expected = new Dictionary<string, int>
        {
            ["word"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Count_one_of_each_word()
    {
        var actual = WordCount.CountWords("one of each");
        var expected = new Dictionary<string, int>
        {
            ["one"] = 1,
            ["of"] = 1,
            ["each"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_occurrences_of_a_word()
    {
        var actual = WordCount.CountWords("one fish two fish red fish blue fish");
        var expected = new Dictionary<string, int>
        {
            ["one"] = 1,
            ["fish"] = 4,
            ["two"] = 1,
            ["red"] = 1,
            ["blue"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Handles_cramped_lists()
    {
        var actual = WordCount.CountWords("one,two,three");
        var expected = new Dictionary<string, int>
        {
            ["one"] = 1,
            ["two"] = 1,
            ["three"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Handles_expanded_lists()
    {
        var actual = WordCount.CountWords("one,\ntwo,\nthree");
        var expected = new Dictionary<string, int>
        {
            ["one"] = 1,
            ["two"] = 1,
            ["three"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Ignore_punctuation()
    {
        var actual = WordCount.CountWords("car: carpet as java: javascript!!&@$%^&");
        var expected = new Dictionary<string, int>
        {
            ["car"] = 1,
            ["carpet"] = 1,
            ["as"] = 1,
            ["java"] = 1,
            ["javascript"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Include_numbers()
    {
        var actual = WordCount.CountWords("testing, 1, 2 testing");
        var expected = new Dictionary<string, int>
        {
            ["testing"] = 2,
            ["1"] = 1,
            ["2"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Normalize_case()
    {
        var actual = WordCount.CountWords("go Go GO Stop stop");
        var expected = new Dictionary<string, int>
        {
            ["go"] = 3,
            ["stop"] = 2
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void With_apostrophes()
    {
        var actual = WordCount.CountWords("First: don't laugh. Then: don't cry.");
        var expected = new Dictionary<string, int>
        {
            ["first"] = 1,
            ["don't"] = 2,
            ["laugh"] = 1,
            ["then"] = 1,
            ["cry"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void With_quotations()
    {
        var actual = WordCount.CountWords("Joe can't tell between 'large' and large.");
        var expected = new Dictionary<string, int>
        {
            ["joe"] = 1,
            ["can't"] = 1,
            ["tell"] = 1,
            ["between"] = 1,
            ["large"] = 2,
            ["and"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_spaces_not_detected_as_a_word()
    {
        var actual = WordCount.CountWords(" multiple   whitespaces");
        var expected = new Dictionary<string, int>
        {
            ["multiple"] = 1,
            ["whitespaces"] = 1
        };
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Alternating_word_separators_not_detected_as_a_word()
    {
        var actual = WordCount.CountWords(",\n,one,\n ,two \n 'three'");
        var expected = new Dictionary<string, int>
        {
            ["one"] = 1,
            ["two"] = 1,
            ["three"] = 1
        };
        Assert.Equal(expected, actual);
    }
}
using System.Collections.Generic;
using System.Text.RegularExpressions;

public static class WordCount
{
    private const string WordPattern = @"[\w\d]+('?[\w\d]+)*";

    public static IDictionary<string, int> CountWords(string phrase)
    {
        phrase = phrase.ToLowerInvariant();
        var dictionary = new Dictionary<string, int>();

        foreach (Match match in Regex.Matches(phrase, WordPattern))
        {
            foreach (Capture capture in match.Captures)
            {
                if (dictionary.ContainsKey(capture.Value))
                {
                    var count = dictionary[capture.Value] + 1;
                    dictionary[capture.Value] = count;
                }
                else
                {
                    dictionary.Add(capture.Value, 1);
                }
            }
        }

        return dictionary;
    }
}

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?