12:55 PM

Hangman game through Python



Simple python code to implement the hangman game.The code assumes that you have a file named 'words.txt' which contains n number of words.The program randomly selects one word from the file. The user is given 8 chances to guess the word.Each word guessed by the user is added to the lettersGuessed list.Procedure isWordGuessed() is used to determine whether the guessed letter is in the secretWord.If not the number of guesses remaining is reduced by one.Procedure getGuessedWord is used to print present correctly guessed words and thier locations...

===================================================import

import random
import string

WORDLIST_FILENAME = "words.txt"

def loadWords():
        print "Loading word list from file..."
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    line = inFile.readline()
        wordlist = string.split(line)
        print "  ", len(wordlist), "words loaded."
       return wordlist

def chooseWord(wordlist):
       return random.choice(wordlist)

def isWordGuessed(secretWord, lettersGuessed):
    for letter in secretWord:
        if letter in lettersGuessed:
            continue
        else:
            return False
    return True

def getGuessedWord(secretWord, lettersGuessed):
    result = ""
    for letter in secretWord:
        if letter in lettersGuessed:
            result += letter
        else:
            result +=  '_ '
    return result
   


def getAvailableLetters(lettersGuessed):
    result = ""
    for letter in string.ascii_lowercase:
        if letter not in lettersGuessed:
            result += letter
    return result
   

def hangman(secretWord):
    lettersGuessed = []
    mistakesMade = 8
    guess=""
    print "Welcome to the game, Hangman!"
    print "I am thinking of a word that is" ,len(secretWord)," letters long."
    while mistakesMade:
        print "--------------------------------------------------------"
        print "You have",mistakesMade," guesses left."
        print "Available letters:",getAvailableLetters(lettersGuessed)
        print "Please guess a letter:"
        guess=raw_input()
        guess=guess.lower()
        if guess in lettersGuessed:
            print "You've already guessed that letter" ,getGuessedWord(secretWord,lettersGuessed)
            continue
        else:
            lettersGuessed.append(guess)
        if isWordGuessed(secretWord,[guess]):
            print "Good guess:",getGuessedWord(secretWord,lettersGuessed)
        else:
            print "Oops! That letter is not in my word:",getGuessedWord(secretWord,lettersGuessed)
            mistakesMade -= 1   
        if  '_' not in getGuessedWord(secretWord,lettersGuessed):
            print "Congratulations, you won!"
    print  "Sorry, you ran out of guesses. The word was ",secretWord,"."

def main():
    wordlist = loadWords()
    secretWord = chooseWord(wordlist)
    hangman(secretWord)

if __name__ == '__main__':
    main()

==================================================

12:45 PM

Python program to find solution of a polynomial using Newtons method



The Newtons method is used to find the root of an n digree polynomial by successive guesses.The simple algorithm to find the root using Newtons method is.
  1. Take initial guess as x_0
  2. Now evaluate the value of the polynomial at x_0
  3. Check if the value is near to zero within a small epsilon (say .01). If yes we call x_0 as the root 
  4. calculate new value of x_0 as x_0=x_0-[f(x)/f'(x)] 
  5. Check if this new value is close to root and so on until you find a root


Here is a simple python program to find the root of a polynomial.The polynomial is represented as a list with the index of each number as the power of x.Three procedures are used to find the value of the polynomial,its derivative and to compute the root of the polynomial

The program returns a list with root and number of itterations performed to find the root

=================================================== 



def evaluate(poly, x):
    value=0.0
    for index in range(len(poly)):
        value  = value+(x**index)*poly[index]
    return value

def Deriv(poly):
    for index in range(len(poly)):
            poly[index]*=index
    poly.pop(0)
    if len(poly) == 0:
        poly.insert(0,0.0)
    return poly

def Root(poly, x_0, epsilon):
    value = (evaluate(poly,x_0))
    poly1=[]
    for num  in poly:
            poly1.append(num)   
    Deriv(poly1)
    count = 0
    while abs(value)  >= epsilon:
        derivalue=(evaluate(poly1,x_0))
        if derivalue == 0:
            x_0 = 0
            break
        x_0 = x_0 - (value/derivalue)
        value=(evaluate(poly,x_0))
        count += 1
    return [x_0,count]
   
  def main():
    poly = [-13.39, 0.0, 17.5, 3.0, 1.0]   
    #poly=[1]
    x_0 = 0.1
    epsilon = .0001
    print computeRoot(poly, x_0, epsilon)   
           
if __name__ == '__main__':
    main()


===================================================
===fa