HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Hangman game performance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gameperformancehangman

Problem

I want to change (for better performance) or optimize my code, especially for and while loops, maybe with lambdas or list comprehensions.

```
def getAvailableLetters(lettersGuessed):
'''
lettersGuessed: list, what letters have been guessed so far
returns: string, comprised of letters that represents what letters have not
yet been guessed.
'''
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
alphabet2 = alphabet[:]

def removeDupsBetter(L1, L2):
L1Start = L1[:]
for e in L1:
if e in L1Start:
L2.remove(e)
return ''.join(str(e) for e in L2)

return removeDupsBetter(lettersGuessed, alphabet2)

def hangman(secretWord):
'''
secretWord: string, the secret word to guess.

Starts up an interactive game of Hangman.

* At the start of the game, let the user know how many
letters the secretWord contains.

* Ask the user to supply one guess (i.e. letter) per round.

* The user should receive feedback immediately after each guess
about whether their guess appears in the computers word.

* After each round, you should also display to the user the
partially guessed word so far, as well as letters that the
user has not yet guessed.

Follows the other limitations detailed in the problem write-up.
'''
intro = str(len(secretWord))
lettersGuessed = []
guess = str
mistakesMade = 8
wordGuessed = False

print 'Welcome to the game, Hangman!'
print ('I am thinking of a word that is ') + intro + (' letters long.')
print ('------------')

while mistakesMade > 0 and mistakesMade <= 8 and wordGuessed is False:
if secretWord == getGuessedWord(secretWord, lettersGuessed):
wordGuessed = True
break
print ('You have ') + str(mistakesMade) + (' guesses left.')
print ('Avai

Solution

For the first function you could use set operations and the built-in string module:

import string

ALPHABET = set(string.ascii_lowercase)

def get_available_letters(letters_guessed):
    '''
    letters_guessed: iterable, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    return "".join(sorted(ALPHABET - set(letters_guessed)))


Note that I renamed your function/variables to adhere to Python's official style-guide, PEP8.

If you don't care that the return value is sorted, or a string, you can just return the set right away:

def get_available_letters(letters_guessed):
    '''
    letters_guessed: iterable, what letters have been guessed so far
    returns: set, letters that have not yet been guessed.
    '''
    return ALPHABET - set(letters_guessed)


In your actual game function, here are a few nitpicks:

  • Use str.format for an easier way to put some value somewhere in a text. This changes print ('I am thinking of a word that is ') + intro + (' letters


long.')
to print 'I am thinking of a word that is {} letters
long.'.format(len(secretWord))
and print ('You have ') + str(mistakesMade) + (' guesses left.') to print ('You have {} guesses left.'.format(mistakesMade))

  • while mistakesMade > 0 and mistakesMade



  • Use a set also for lettersGuessed, then if guess in lettersGuessed: becomes \$\mathcal{O}(1)\$, instead of \$\mathcal{O}(n)\$



  • if wordGuessed == True: can just be if wordGuessed:`

Code Snippets

import string

ALPHABET = set(string.ascii_lowercase)

def get_available_letters(letters_guessed):
    '''
    letters_guessed: iterable, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    return "".join(sorted(ALPHABET - set(letters_guessed)))
def get_available_letters(letters_guessed):
    '''
    letters_guessed: iterable, what letters have been guessed so far
    returns: set, letters that have not yet been guessed.
    '''
    return ALPHABET - set(letters_guessed)

Context

StackExchange Code Review Q#160931, answer score: 5

Revisions (0)

No revisions yet.