美文网首页
python编程导论 week4

python编程导论 week4

作者: 猪蹄炖粥 | 来源:发表于2018-07-26 23:53 被阅读7次

    problem set 4

    总结:

    • python2和python3的切换有点心累,如何在python3中一行print出2个操作数.....所以用anaconda切换成python2.7.13来做了
    • anaconda切换python版本的教程https://www.cnblogs.com/wxshi/p/6805120.html
    • 实现代码如下:(桉顺序),其中3,6两个逻辑有空再看看。
    def getWordScore(word,n)
    L = []
    for i in word:
        L.append(SCRABBLE_LETTER_VALUES[i])
    point = sum(L) * len(word)
    if len(word) == n :
        point += 50
    return point
    
    def updateHand(hand, word):
        import  copy
        res=copy.deepcopy(hand)
        for letter in word:
            res[letter]-=1
            if res[letter]==0:
                del res[letter]
        return res
    
    
    def isValidWord(word, hand, wordList):
        word = word.lower()
        cpyHand = hand.copy()
        for letter in word:
    
    def calculateHandlen(hand):
        return sum(hand.values())
    
    def playHand(hand, wordList, n):
        score = 0
        while calculateHandlen(hand) != 0: 
            print 'Current Hand:', displayHand(hand)
            word = str(raw_input('Enter word, or a "." to indicate that you are finished: '))
            if isValidWord(word, hand, wordList):
                score += getWordScore(word, n)
            if word == '.':
                print 'Goodbye! Total score:', score, 'points.'
                break
            else:
                if not isValidWord(word, hand, wordList):
                    print 'Invalid word, please try again.'
                    print
                else:
                    print '"' + word + '" earned',  getWordScore(word, n), 'points. Total:', score, 'points'
                    hand = updateHand(hand, word)
                    print
        if calculateHandlen(hand) == 0:
            print 'Run out of letters. Total score:', score, 'points.'
    
    def playGame(wordList):
    hand = ''
    #HAND_SIZE = 7
    while 1:
        g = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
        if g == 'e':
            return
        if g == 'n':
            hand = dealHand(HAND_SIZE) #暂时设为7
            playHand(hand, wordList, HAND_SIZE)
        elif g == 'r' and hand != '':
            playHand(hand, wordList, HAND_SIZE)
        elif g == 'r':
            print('You have not played a hand yet. Please play a new hand first!')
        else:
             print('Invalid command.')
             g = 'n'
    
    def compChooseWord(hand, wordList, n):
        CoWord={}
        BestWord=None
        for i in wordList:
            if True==isValidWord(i, hand, wordList):#
                CoWord[i]=getWordScore(i, n)
                BestWord=max(CoWord.items(), key=lambda x: x[1])[0]
        return BestWord
    
    def compPlayHand(hand, wordList, n):
        handn, score, word = hand.copy(), 0, ''
        while word != None:
            word = compChooseWord(handn, wordList, n)
            print '\nCurrent Hand: ',
            displayHand(handn)
            if isValidWord(word, handn, wordList):
                score += getWordScore(word, n)
                print '"%s" earned %d points. Total: %d points.' % (word, getWordScore(word, n), score)
                handn = updateHand(handn, word)
                if calculateHandlen(handn) == 0:
                    print ''
                    break
        print 'Total score: %d points.' % (score)
    
    def compChooseWord(hand, wordList, n):
        result = None
        score = 0
        for i in wordList:
            if len(i) <= n:
                if isValidWord(i, hand, wordList):
                    score1 = getWordScore(i, n)
                    if score1 > score:
                        score = score1
                        result = i
        return result
    
    def playGame(wordList):
        oldhand = {}        
        while True:
            choose2 =''
            choose = raw_input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
            if choose == 'e':
                break
            elif choose == 'n':
                hand = dealHand(HAND_SIZE)
                while choose2 not in ('u','c'):
                    choose2 = raw_input('Enter u to have yourself play, c to have the computer play: ')
                    oldhand = hand              
                    if choose2 == 'u':  
                        playHand(hand, wordList, HAND_SIZE)
                    elif choose2 =='c':
                        compPlayHand(hand, wordList, HAND_SIZE)
                    else:
                        print "Invalid command\n"
            elif choose == 'r':
                if oldhand =={}:
                    print "You have not played a hand yet. Please play a new hand first!\n"
                else:
                    while choose2 not in ('u','c'):
                        choose2 = raw_input('Enter u to have yourself play, c to have the computer play: ')             
                        if choose2 == 'u':  
                            playHand(oldhand, wordList, HAND_SIZE)
                        elif choose2 =='c':
                            compPlayHand(oldhand, wordList, HAND_SIZE)
                        else:
                            print "Invalid command\n"
            else:
                print "Invalid command\n"
    

    期中考试
    //b和x搞混

    def myLog(x, b):
        '''
        x: a positive integer
        b: a positive integer; b >= 2
    
        returns: log_b(x), or, the logarithm of x relative to a base b.
        '''
        # Your Code Here
        count = 0
        while b<=x:
            x = x/b
            count+=1
        return count
    
    print myLog(16,2)
    

    相关文章

      网友评论

          本文标题:python编程导论 week4

          本文链接:https://www.haomeiwen.com/subject/cqnimftx.html