Skip to content

word_count: test mixed case #234

Closed
Closed

Description

@behrtam

I just saw a solution to the word_count exercise where my feeling was that this shouldn't pass all tests. There is already one mixed case test, but it has a specific word order so that this solution works.

def word_count(input):
    list = {}
    for word in input.split():
        if word in list:
            list[word]+=1
        elif word.lower() in list:
            list[word.lower()]+=1
        else:
            list[word]=1
    return list

>>> word_count('GO go Go')
{'GO': 1, 'go': 2}
>>> word_count('go Go GO')
{'go': 3}
>>>

Activity

  1. Dog commented on Nov 10, 2015

    @Dog
    Contributor

    The reason for this is that a change was applied to the test to normalize case.

    0314cbf
    #207

    It was justified because other language tracks expect normalization - specifically ruby and clojure in the pull request. I also noticed Haskell does this normalization.

  2. behrtam commented on Nov 10, 2015

    @behrtam
    ContributorAuthor

    The problem is not that normalization is tested. The problem is also not that the test is not opinionated about how to normalize case.
    The problem is that the test is so minimalistic that a user wrote code that passed the test suite while the solution only works if the function gets the words in the right order.

    My simple solution to the mixed case test is to expand the test string "go Go GO" to
    "go Go GO Stop stop" and assume as result a dict with the values 3, 2. That would still not force the user to only normalize to lower case.

    def test_mixed_case(self):
        self.assertEqual([2, 3], sorted(list(word_count('go Go GO Stop stop').values())))
  3. kytrinyx commented on Nov 12, 2015

    @kytrinyx
    Member

    I actually don't mind which direction the user normalizes to, and I think that it would be nice to leave that up to the person solving the problem. I like this suggested approach and wouldn't mind changing the other tracks to use it.

  4. behrtam commented on Nov 12, 2015

    @behrtam
    ContributorAuthor

    Okay, did it for the Python track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions