mooc课程mit 6.00.1xproblem set3解决方法

  • RADIATION EXPOSURE
    挺简单的一道题,计算函数和算法过程都已经给出,做一个迭代计算就行了。
     1 def radiationExposure(start, stop, step):
     2     '''
     3     Computes and returns the amount of radiation exposed
     4     to between the start and stop times. Calls the 
     5     function f (defined for you in the grading script)
     6     to obtain the value of the function at any point.
     7 
     8     start: integer, the time at which exposure begins
     9     stop: integer, the time at which exposure ends
    10     step: float, the width of each rectangle. You can assume that
    11       the step size will always partition the space evenly.
    12 
    13     returns: float, the amount of radiation exposed to 
    14       between start and stop times.
    15     '''
    16     # FILL IN YOUR CODE HERE...
    17     area = 0
    18     while start <  stop:
    19         area = area + f(start) * step
    20         start += step
    21 
    22     return area
  • A WORDGAME: HANGMAN
    一个猜单词游戏,个人感觉略有难度,游戏规则见这里。将其分步实现相应的函数。
      1 # 6.00 Problem Set 3
      2 # 
      3 # Hangman game
      4 #
      5 
      6 # -----------------------------------
      7 # Helper code
      8 # You don't need to understand this helper code,
      9 # but you will have to know how to use the functions
     10 # (so be sure to read the docstrings!)
     11 
     12 import random
     13 import string
     14 
     15 WORDLIST_FILENAME = "words.txt"
     16 
     17 def loadWords():
     18     """
     19     Returns a list of valid words. Words are strings of lowercase letters.
     20 
     21     Depending on the size of the word list, this function may
     22     take a while to finish.
     23     """
     24     print "Loading word list from file..."
     25     # inFile: file
     26     inFile = open(WORDLIST_FILENAME, 'r', 0)
     27     # line: string
     28     line = inFile.readline()
     29     # wordlist: list of strings
     30     wordlist = string.split(line)
     31     print "  ", len(wordlist), "words loaded."
     32     return wordlist
     33 
     34 def chooseWord(wordlist):
     35     """
     36     wordlist (list): list of words (strings)
     37     Returns a word from wordlist at random
     38     """
     39     return random.choice(wordlist)
     40 
     41 # end of helper code
     42 # -----------------------------------
     43 
     44 # Load the list of words into the variable wordlist
     45 # so that it can be accessed from anywhere in the program
     46 wordlist = loadWords()
     47 
     48 def isWordGuessed(secretWord, lettersGuessed):
     49     '''
     50     secretWord: string, the word the user is guessing
     51     lettersGuessed: list, what letters have been guessed so far
     52     returns: boolean, True if all the letters of secretWord are in 
     53         lettersGuessed; False otherwise
     54     '''
     55     # FILL IN YOUR CODE HERE...
     56     for c in secretWord:
     57         if c not in lettersGuessed:
     58             return False
     59 
     60     return True
     61 
     62 def getGuessedWord(secretWord, lettersGuessed):
     63     '''
     64     secretWord: string, the word the user is guessing
     65     lettersGuessed: list, what letters have been guessed so far
     66     returns: string, comprised of letters and underscores that represents
     67       what letters in secretWord have been guessed so far.
     68     '''
     69     # FILL IN YOUR CODE HERE...
     70     guess = ""
     71     for c in secretWord:
     72         if c in lettersGuessed:
     73             guess += c
     74         else:
     75             guess += "_ "
     76 
     77     return guess
     78 
     79 def getAvailableLetters(lettersGuessed):
     80     '''
     81     lettersGuessed: list, what letters have been guessed so far
     82     returns: string, comprised of letters that represents what letters have not
     83       yet been guessed.
     84     '''
     85     # FILL IN YOUR CODE HERE...
     86     available = ""
     87     for c in string.ascii_lowercase:
     88         if c not in lettersGuessed:
     89             available += c
     90 
     91     return available
     92 
     93 def hangman(secretWord):
     94     '''
     95     secretWord: string, the secret word to guess.
     96     Starts up an interactive game of Hangman.
     97     * At the start of the game, let the user know how many 
     98       letters the secretWord contains.
     99     * Ask the user to supply one guess (i.e. letter) per round.
    100     * The user should receive feedback immediately after each guess 
    101       about whether their guess appears in the computers word.
    102     * After each round, you should also display to the user the 
    103       partially guessed word so far, as well as letters that the 
    104       user has not yet guessed.
    105     Follows the other limitations detailed in the problem write-up.
    106     '''
    107     # FILL IN YOUR CODE HERE...
    108     print "Welcome to the game Hangman!"
    109     print "I am thinking of a word that is", len(secretWord), "letters long."
    110     print "-------------"
    111     lettersGuessed = []
    112     mistakesMade = 0
    113     availableLetters = string.ascii_lowercase
    114 
    115     while mistakesMade < 8 and not isWordGuessed(secretWord, lettersGuessed):
    116         print "You have", 8 - mistakesMade, "guesses left."
    117         print "Available letters:", availableLetters
    118         guess = raw_input("Please guess a letter: ").lower()
    119 
    120         if guess in lettersGuessed:
    121             print("Oops! You've already guessed that letter: " +
    122                 getGuessedWord(secretWord, lettersGuessed))
    123         elif guess in secretWord:
    124             lettersGuessed.append(guess)
    125             print "Good guess:", getGuessedWord(secretWord, lettersGuessed)
    126         else:
    127             mistakesMade += 1
    128             lettersGuessed.append(guess)
    129             print("Oops! That letter is not in my word: " + 
    130                 getGuessedWord(secretWord, lettersGuessed))
    131 
    132         availableLetters = getAvailableLetters(lettersGuessed)
    133 
    134         print "------------"
    135 
    136     if isWordGuessed(secretWord, lettersGuessed):
    137         print "Congratulations, you won!"
    138     else:
    139         print("Sorry, you ran out of guesses. The word was " + secretWord + 
    140             ".")
    141 
    142 # When you've completed your hangman function, uncomment these two lines
    143 # and run this file to test! (hint: you might want to pick your own
    144 # secretWord while you're testing)
    145 
    146 secretWord = chooseWord(wordlist).lower()
    147 hangman(secretWord)
本文为博主辛苦创作,转载请注明 http://honoka.cnblogs.com。
原文地址:https://www.cnblogs.com/honoka/p/4795895.html