0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Adamso
  • 97
  • 1
  • 10

5 Answers5

2

you can also use Counter from collections

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


output: 
[('a', 5), ('r', 2), ('b', 2)]
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
  • thanks Shivkumar but i cant use counter .. is there a different way to solve this without using counter? – Adamso Dec 05 '16 at 13:54
1

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)
Alex
  • 21,273
  • 10
  • 61
  • 73
1

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}
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

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

Community
  • 1
  • 1
0

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)))
Martinbaste
  • 416
  • 2
  • 8