Adamso
Adamso

Reputation: 107

Finding most common letter in a word list

I am writing a code for the game hangman. I am stuck in the part where I want to help the user to get a hint.

I need to create a function that accepts a word list, and the current pattern(for example:"___e_") and returns the most common letter in the word list.

I know I need to count the letters in each word and then return the maximum of that letter list but I don't quite know how to actually perform this.

I started by writing this code:

def choose_letter(words, pattern):
    new_list = [0] * 26
    for i in range(0, 26):
        for n in range(0, len(num_list)):
            if i == num_list[n]:
                new_list[i] += 1

    return new_list

but I get stuck because I don't know how to actually compare it to a letter.

I would love to hear some advice or guidance that will help me proceed.

*we didnt learn about dictionary yet

Upvotes: 0

Views: 4683

Answers (5)

Martinbaste
Martinbaste

Reputation: 416

The obvious answer is to use a dictionary with the letters as keys. You can also use ord(char) to get an integer from a character, and use that integer as index for your list. Because your list is length 26 and the index for letters start at 97 (for lowercase 'a'), you can do something like:

def choose_letter(words, pattern): 
    new_list = [0] * 26
    for word in words:
        word = word.lower()
        for letter in word:
            index = ord(letter) -97
            new_list[index] += 1
    return new_list

To get the count for any given letter on the list you can:

print(new_list[ord(letter)])

To get the most common letter (note that if multiple letters have the highest value, only the first one will be returned):

chr(new_list.index(max(new_list)))

Upvotes: 0

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can try:

>>> data = "Harsha Biyani"
>>> d = {}
>>> letters = set(data)
>>> for letter in letters :
    d[letter] = data.count(letter)


>>> key, value = max(d.items(), key=lambda x:x[1])  #in python 3
>>> key, value = max(d.iteritems(), key=lambda x:x[1])  # in python 2
>>> key
'a'
>>> value
3
>>> d
{'y': 1, 'H': 1, 'h': 1, ' ': 1, 'n': 1, 'B': 1, 's': 1, 'a': 3, 'i': 2, 'r': 1}

Upvotes: 1

Alex
Alex

Reputation: 21766

You can use a combination of count and set to run the most frequent letter that is not contained in pattern, see below. The first line counts the number of occurrence for each letter, the second statement returns (one of) the most frequent letter(s).

def choose_letter(words, pattern):
    frequencies = [(letter, words.count(letter)) for letter in set(words) if letter not in pattern and letter!=' ']
    most_frequent_letter = max(frequencies, key=lambda x: x[1])[0]
    return most_frequent_letter


word = 'hangman is great fun'
pattern = '____a_____'
print choose_letter(word,pattern)

Upvotes: 1

Shivkumar kondi
Shivkumar kondi

Reputation: 6762

you can also use Counter from collections

from collections import Counter
Counter('abracadabra').most_common(3)


output: 
[('a', 5), ('r', 2), ('b', 2)]

Upvotes: 2

user6238663
user6238663

Reputation:

You can use the method count() of a string to count the occurence of a char in a string : Count occurrence of a character in a string

Upvotes: 0

Related Questions