Skip to content

Instantly share code, notes, and snippets.

@goldsmith
Last active August 29, 2015 14:03
Show Gist options
  • Save goldsmith/b24aaba5d0c11a9ef44e to your computer and use it in GitHub Desktop.
Save goldsmith/b24aaba5d0c11a9ef44e to your computer and use it in GitHub Desktop.

Revisions

  1. goldsmith revised this gist Jul 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion groupby_anagram.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ def group_by(col, hashfn=None):
    if not hashfn:
    hashfn = hash # builtin hash
    group = defaultdict(list)
    [group(hashfn(e)).append(e) for e in col]
    [group[hashfn(e)].append(e) for e in col]

    return group.values()

  2. goldsmith revised this gist Jul 6, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions groupby_anagram.py
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,11 @@
    from collections import Counter, defaultdict


    def group_by(col, hash=None):
    if not hash:
    hash = lambda e: e.__hash__()
    def group_by(col, hashfn=None):
    if not hashfn:
    hashfn = hash # builtin hash
    group = defaultdict(list)
    [group(hash(e)).append(e) for e in col]
    [group(hashfn(e)).append(e) for e in col]

    return group.values()

    @@ -17,5 +17,5 @@ def anagram_hash(word):
    return tuple(sorted(Counter(word)))


    group_by(["hello", "foo", "olleh", "bar", "baz", "arb"], anagram_hash)
    group_by(["hello", "foo", "olleh", "bar", "baz", "arb"], hashfn=anagram_hash)
    # [["hello", "olleh"], ["foo"], ["bar", "arb"], ["baz"]]
  3. goldsmith revised this gist Jul 6, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions groupby_anagram.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    """
    from collections import Counter, defaultdict


    def group_by(col, hash=None):
    if not hash:
    hash = lambda e: e.__hash__()
    @@ -11,8 +12,10 @@ def group_by(col, hash=None):

    return group.values()


    def anagram_hash(word):
    return tuple(sorted(Counter(word)))


    group_by(["hello", "foo", "olleh", "bar", "baz", "arb"], anagram_hash)
    # [["hello", "olleh"], ["foo"], ["bar", "arb"], ["baz"]]
  4. goldsmith created this gist Jul 6, 2014.
    18 changes: 18 additions & 0 deletions groupby_anagram.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    """
    Write a program that takes a list of words and groups together the anagrams.
    """
    from collections import Counter, defaultdict

    def group_by(col, hash=None):
    if not hash:
    hash = lambda e: e.__hash__()
    group = defaultdict(list)
    [group(hash(e)).append(e) for e in col]

    return group.values()

    def anagram_hash(word):
    return tuple(sorted(Counter(word)))

    group_by(["hello", "foo", "olleh", "bar", "baz", "arb"], anagram_hash)
    # [["hello", "olleh"], ["foo"], ["bar", "arb"], ["baz"]]