Skip to content

Instantly share code, notes, and snippets.

@saltavenger
saltavenger / hangman
Created October 23, 2012 00:28
Python Hangman
intro = str(len(secretWord))
mistakesMade = 8
lettersGuessed = []
guess = str
availableLetters = str(getAvailableLetters(lettersGuessed))
print 'Welcome to the game, Hangman!'
print ('I am thinking of a word that is ') + intro + (' letters long.')
if mistakesMade >= 0:
@saltavenger
saltavenger / hangman
Created October 23, 2012 14:45
python hangman
# 6.00 Problem Set 3
#
# Hangman game
#
# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)
@saltavenger
saltavenger / isWordValid
Created October 29, 2012 23:27
scrabble word validate
count = 0
newHand= str(hand)
if count <= len(word):
for c in word:
if c in newHand:
count += 1
else:
return False
if word in wordList:
@saltavenger
saltavenger / playHand
Created October 30, 2012 17:22
scrabble
# Keep track of two numbers: the number of letters left in your hand and the total score
score = 0
numbersLeft = int(calculateHandlen(hand))
# As long as there are still letters left in the hand:
while numbersLeft > 0:
# Display the hand
print 'Current Hand: ',
displayHand(hand)
@saltavenger
saltavenger / playGame
Created October 30, 2012 18:30
scrabble 2
def playGame(wordList):
"""
Allow the user to play an arbitrary number of hands.
1) Asks the user to input 'n' or 'r' or 'e'.
* If the user inputs 'n', let the user play a new (random) hand.
* If the user inputs 'r', let the user play the last hand again.
* If the user inputs 'e', exit the game.
* If the user inputs anything else, tell them their input was invalid.
@saltavenger
saltavenger / compChooseWord
Created October 31, 2012 00:49
scrabble3
def compChooseWord(hand, wordList):
"""
Given a hand and a wordList, find the word that gives
the maximum value score, and return it.
This word should be calculated by considering all the words
in the wordList.
If no words in the wordList can be made from the hand, return None.
import string
def buildCoder(shift):
"""
Returns a dict that can apply a Caesar cipher to a letter.
The cipher is defined by the shift value. Ignores non-letter characters
like punctuation, numbers, and spaces.
shift: 0 <= int < 26
returns: dict
@saltavenger
saltavenger / crypt2
Created November 8, 2012 23:17
crypt2
# Problem 2: Decryption
#
def findBestShift(wordList, text):
"""
Finds a shift key that can decrypt the encoded text.
text: string
returns: 0 <= int < 26
"""
#1. Set the maximum number of real words found to 0.
@saltavenger
saltavenger / trigger
Last active October 12, 2015 19:18
trigger
# TODO: WordTrigger
class WordTrigger(Trigger):
def __init__(self, word):
self.word = word.lower()
def isWordIn(self, text):
newtext=[]
self.text = text.lower()
for c in string.punctuation:
self.text = self.text.replace(c, ' ').lower()
newText = self.text.split(' ')
@saltavenger
saltavenger / triggers 2
Created November 14, 2012 21:51
triggers2
# Enter your code for WordTrigger, TitleTrigger,
class WordTrigger(Trigger):
def __init__(self, word):
self.word = word.lower()
def isWordIn(self, text):
newtext=[]
self.text = text.lower()
for c in string.punctuation:
self.text = self.text.replace(c, ' ').lower()
newText = self.text.split(' ')