<reponame>lilsweetcaligula/MIT6.00.1x
"""
Problem 3.

In this problem, you'll be asked to read through an object-oriented 
implementation of the hand from the word game problem of Problem Set 4. 
You'll then be asked to implement one of its methods. Note that the 
implementation of the object-oriented version of the hand is a bit 
different than how we did things with the functional implementation; 
pay close attention to doc strings and read through the implementation 
carefully.

To begin: Download L12_hand.py and read through the file: 

[ref.href] https://d37djvu3ytnwxt.cloudfront.net/asset-v1:MITx+6.00.1x_6+2T2015+type@asset+block/files_finger_exercises_L12_hand.py

Be sure to understand what's going on in the file. Make a few instances 
of the Hand class, and play around with the existing methods.

When you have completed reading through the file, implement the update 
method.
"""

import random 

class Hand(object):
    def __init__(self, n):
        '''
        Initialize a Hand.

        n: integer, the size of the hand.
        '''
        assert type(n) == int
        self.HAND_SIZE = n
        self.VOWELS = 'aeiou'
        self.CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

        # Deal a new hand
        self.dealNewHand()

    def dealNewHand(self):
        '''
        Deals a new hand, and sets the hand attribute to the new hand.
        '''
        # Set self.hand to a new, empty dictionary
        self.hand = {}

        # Build the hand
        numVowels = self.HAND_SIZE / 3
    
        for i in range(numVowels):
            x = self.VOWELS[random.randrange(0,len(self.VOWELS))]
            self.hand[x] = self.hand.get(x, 0) + 1
        
        for i in range(numVowels, self.HAND_SIZE):    
            x = self.CONSONANTS[random.randrange(0,len(self.CONSONANTS))]
            self.hand[x] = self.hand.get(x, 0) + 1
            
    def setDummyHand(self, handString):
        '''
        Allows you to set a dummy hand. Useful for testing your implementation.

        handString: A string of letters you wish to be in the hand. Length of this
        string must be equal to self.HAND_SIZE.

        This method converts sets the hand attribute to a dictionary
        containing the letters of handString.
        '''
        assert len(handString) == self.HAND_SIZE, "Length of handString ({0}) must equal length of HAND_SIZE ({1})".format(len(handString), self.HAND_SIZE)
        self.hand = {}
        for char in handString:
            self.hand[char] = self.hand.get(char, 0) + 1


    def calculateLen(self):
        '''
        Calculate the length of the hand.
        '''
        ans = 0
        for k in self.hand:
            ans += self.hand[k]
        return ans
    
    def __str__(self):
        '''
        Display a string representation of the hand.
        '''
        output = ''
        hand_keys = self.hand.keys()
        hand_keys.sort()
        for letter in hand_keys:
            for j in range(self.hand[letter]):
                output += letter
        return output

    def update(self, word):
        """
        Does not assume that self.hand has all the letters in word.

        Updates the hand: if self.hand does have all the letters to make
        the word, modifies self.hand by using up the letters in the given word.

        Returns True if the word was able to be made with the letter in
        the hand; False otherwise.
        
        word: string
        returns: Boolean (if the word was or was not made)
        """

        if len(word) == 0:
            return False
        goodWord = True
        chars = []
        for char in word:
            if char not in chars:
                chars.append(char)
                if char in self.hand:
                    if word.count(char) > self.hand[char]:
                        goodWord = False
                else:
                    goodWord = False
        if goodWord == True:
            for char in chars:
                charCount = word.count(char)
                if charCount >= self.hand[char]:
                    self.hand[char] = 0
                else:
                    self.hand[char] -= charCount
        return goodWord

"""
Problem 3.

In this problem, you'll be asked to read through an object-oriented 
implementation of the hand from the word game problem of Problem Set 4. 
You'll then be asked to implement one of its methods. Note that the 
implementation of the object-oriented version of the hand is a bit 
different than how we did things with the functional implementation; 
pay close attention to doc strings and read through the implementation 
carefully.

To begin: Download L12_hand.py and read through the file: 

[ref.href] https://d37djvu3ytnwxt.cloudfront.net/asset-v1:MITx+6.00.1x_6+2T2015+type@asset+block/files_finger_exercises_L12_hand.py

Be sure to understand what's going on in the file. Make a few instances 
of the Hand class, and play around with the existing methods.

When you have completed reading through the file, implement the update 
method.
"""

import random 

class Hand(object):
    def __init__(self, n):
        '''
        Initialize a Hand.

        n: integer, the size of the hand.
        '''
        assert type(n) == int
        self.HAND_SIZE = n
        self.VOWELS = 'aeiou'
        self.CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

        # Deal a new hand
        self.dealNewHand()

    def dealNewHand(self):
        '''
        Deals a new hand, and sets the hand attribute to the new hand.
        '''
        # Set self.hand to a new, empty dictionary
        self.hand = {}

        # Build the hand
        numVowels = self.HAND_SIZE / 3
    
        for i in range(numVowels):
            x = self.VOWELS[random.randrange(0,len(self.VOWELS))]
            self.hand[x] = self.hand.get(x, 0) + 1
        
        for i in range(numVowels, self.HAND_SIZE):    
            x = self.CONSONANTS[random.randrange(0,len(self.CONSONANTS))]
            self.hand[x] = self.hand.get(x, 0) + 1
            
    def setDummyHand(self, handString):
        '''
        Allows you to set a dummy hand. Useful for testing your implementation.

        handString: A string of letters you wish to be in the hand. Length of this
        string must be equal to self.HAND_SIZE.

        This method converts sets the hand attribute to a dictionary
        containing the letters of handString.
        '''
        assert len(handString) == self.HAND_SIZE, "Length of handString ({0}) must equal length of HAND_SIZE ({1})".format(len(handString), self.HAND_SIZE)
        self.hand = {}
        for char in handString:
            self.hand[char] = self.hand.get(char, 0) + 1


    def calculateLen(self):
        '''
        Calculate the length of the hand.
        '''
        ans = 0
        for k in self.hand:
            ans += self.hand[k]
        return ans
    
    def __str__(self):
        '''
        Display a string representation of the hand.
        '''
        output = ''
        hand_keys = self.hand.keys()
        hand_keys.sort()
        for letter in hand_keys:
            for j in range(self.hand[letter]):
                output += letter
        return output

    def update(self, word):
        """
        Does not assume that self.hand has all the letters in word.

        Updates the hand: if self.hand does have all the letters to make
        the word, modifies self.hand by using up the letters in the given word.

        Returns True if the word was able to be made with the letter in
        the hand; False otherwise.
        
        word: string
        returns: Boolean (if the word was or was not made)
        """

        if len(word) == 0:
            return False
        goodWord = True
        chars = []
        for char in word:
            if char not in chars:
                chars.append(char)
                if char in self.hand:
                    if word.count(char) > self.hand[char]:
                        goodWord = False
                else:
                    goodWord = False
        if goodWord == True:
            for char in chars:
                charCount = word.count(char)
                if charCount >= self.hand[char]:
                    self.hand[char] = 0
                else:
                    self.hand[char] -= charCount
        return goodWord
Problem 3.  In this problem, you'll be asked to read through an object-oriented  implementation of the hand from the word game problem of Problem Set 4.  You'll then be asked to implement one of its methods. Note that the  implementation of the object-oriented version of the hand is a bit  different than how we did things with the functional implementation;  pay close attention to doc strings and read through the implementation  carefully.  To begin: Download L12_hand.py and read through the file:   [ref.href] https://d37djvu3ytnwxt.cloudfront.net/asset-v1:MITx+6.00.1x_6+2T2015+type@asset+block/files_finger_exercises_L12_hand.py  Be sure to understand what's going on in the file. Make a few instances  of the Hand class, and play around with the existing methods.  When you have completed reading through the file, implement the update  method. Initialize a Hand.          n: integer, the size of the hand. # Deal a new hand Deals a new hand, and sets the hand attribute to the new hand. # Set self.hand to a new, empty dictionary # Build the hand Allows you to set a dummy hand. Useful for testing your implementation.          handString: A string of letters you wish to be in the hand. Length of this         string must be equal to self.HAND_SIZE.          This method converts sets the hand attribute to a dictionary         containing the letters of handString. Calculate the length of the hand. Display a string representation of the hand. Does not assume that self.hand has all the letters in word.          Updates the hand: if self.hand does have all the letters to make         the word, modifies self.hand by using up the letters in the given word.          Returns True if the word was able to be made with the letter in         the hand; False otherwise.                  word: string         returns: Boolean (if the word was or was not made)