MIT 6.00.1x 计算机科学和Python编程导论 Set 3

Radiation Exposure

感谢 glhezjnucn 同学贡献的翻译!
“Radioactive decay” is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation. Exposure to radiation can be dangerous and is very important to measure to ensure that one is not exposed to too terribly much of it.辐射衰变是不稳定原子失去能量并释放电离子的过程-这通常被称为辐射。接触辐射(暴露于辐射)可能是危险的,同时检测并确保人们不暴露于过量辐射(的环境)中变得重要。
The radioactivity of a material decreases over time, as the material decays. A radioactive decay curve describes this decay. The x-axis measures time, and the y-axis measures the amount of activity produced by the radioactive sample. ‘Activity’ is defined as the rate at which the nuclei within the sample undergo transititions - put simply, this measures how much radiation is emitted at any one point in time. The measurement of activity is called the Becquerel (Bq). Here is a sample radioactive decay curve:物质的辐射活性随着时间的流逝而减少,因为辐射物质在衰减。辐射衰减曲线用于描述这样的衰减过程。x轴表示时间,y轴表示辐射物质样品产生的辐射活性量。辐射的活性量定义为处于物质迁变的核的比率,它给出某一时刻所释放的辐射量。活性的度量称为Bq(理解为度量的单位即量纲)。如下是一个辐射衰减曲线的样例:
MIT 6.00.1x 计算机科学和Python编程导论 Set 3_第1张图片
Now here’s the problem we’d like to solve. Let’s say Sarina has moved into a new apartment. Unbeknownst to her, there is a sample of Cobalt-60 inside one of the walls of the apartment. Initially that sample had 10 MBq of activity, but she moves in after the sample has been there for 5 years. She lives in the apartment for 6 years, then leaves. How much radiation was she exposed to?
现在我们需要解决的问题是,假若Sarina搬进了新的房子,并不为她所知的是,房子的墙里有一个放射性钴-60样品。初始时,该样品具有10MBq(百万Bq)的辐射活性,但她是那个放射性物质在那里5年后才住进去的,她在那里住了6年,然后离开了。现在请问她接触了多少辐射?
We can actually figure this out using the radioactive decay curve from above. What we want to know is her total radiation exposure from year 5 to year 11.
我们当然可以从上图的辐射曲线中计算出来。我们所需要知道的是从第5年到第11年她所接触的辐射总量。
MIT 6.00.1x 计算机科学和Python编程导论 Set 3_第2张图片
Total radiation exposure corresponds to the area between the two green lines at time = 5 and time = 11, and under the blue radioactive decay curve. This should make intuitive sense - if the x axis measures time, and the y axis measures activity, then the area under the curve measures (time * activity) = MBq*years, or, approximately the total number of MBq Sarina was exposed to in her time in the radioactive apartment (technically, this result is the combination of gamma rays and beta particles she was exposed to, but this gets a bit complicated, so we’ll ignore it. Sorry, physicists!).
辐射总量对应的是时间从5到11所对应的两条绿色线所对应区域的面积。这可以利用直觉-如果x轴是时间,y轴是活性度量,那么曲线下的面积(时间辐射活性)=MBq年,或者,近似的,就是Sarina在那段时间所接触的辐射量(技术地说,是由于伽玛射线与贝塔粒子,不过这太复杂了,因此我们忽略,抱歉,物理学家们!)
MIT 6.00.1x 计算机科学和Python编程导论 Set 3_第3张图片
So far, so good. But, how do we calculate this? Unlike a simple shape - say a square, or a circle - we have no easy way to tell what the area under this curve is.
到此一切都不错。但是我们怎么来计算呢?不像简单的形状-如正方形,圆-我们没有简单的方法获得曲线下区域的面积。
However, we have learned a technique that can help us here - approximation. Let’s use an approximation algorithm to estimate the area under this curve! We’ll do so by first splitting up the area into equally-sized rectangles (in this case, six of them, one rectangle per year):
不过,我们学过一种技术可以帮助我们-近似。我们用近似的方法来估计曲线下区域的面积!我们将这么来做,先将区域切成等尺寸(意指其中一边等长)的长方形,如下情形,是6个,每年一个长方形。
MIT 6.00.1x 计算机科学和Python编程导论 Set 3_第4张图片
Once we’ve done that, we can figure out the area of each rectangle pretty easily. Recall that the area of a rectangle is found by multiplying the height of the rectangle by its width. The height of this rectangle:
当我们如此做了之后,我们可以非常简便的计算出每个长方形的面积。回忆一下长方形的面积是宽度乘以高度。而这里(红色的)长方形的高度:
MIT 6.00.1x 计算机科学和Python编程导论 Set 3_第5张图片
is the value of the curve at 5.0. If the curve is described by a function, f, we can obtain the value of the curve by asking for f(5.0).
是曲线在5.0处的值(函数值).如果曲线由函数, f, 给出,我们可以用f(5.0)得到函数值.
f(5.0) = 5.181
The width of the rectangle is 1.0. So the area of this single rectangle is 1.0*5.181 = 5.181. To approximate how much radiation Sarina was exposed to, we next calculate the area of each successive rectangle and then sum up the areas of each rectangle to get the total. When we do this, we find that Sarina was exposed to nearly 23 MBq of radiation (technically, her apartment was bombarded by 23e6 * 3.154e6 = 7.25e13 neutrons, for those interested…).
长方形的宽为1,因此面积(指红色的那个)为1.0*5.181 = 5.181. 为近似的计算Sarina接触的辐射总量,我们将逐次的面积加起来即可。当我们这么假设我们发现Sarina接触的辐射总量差不多是23 MBq (技术的说, 她的房子被大约23e6 * 3.154e6 = 7.25e13 个中子所轰炸, 如果有人感兴趣的话。).
Whether or not this will kill Sarina depends exactly on the type of radiation she was exposed to (see this link which discusses more about the ways of measuring radiation). Either way, she should probably ask her landlord for a substantial refund.
这是否足以使Sarina致命,这取决于她所接触辐射的类型(查看链接可以获知更多关于辐射量计算的信息)。或许她可以向房东索取大量的赔偿。
In this problem, you are asked to find the amount of radiation a person is exposed to during some period of time by completing the following function:
这个问题中,要求你计算某人在某个时间段所接触的辐射量,完成如下函数的设计:

def radiationExposure(start, stop, step):
    '''
    Computes and returns the amount of radiation exposed
    to between the start and stop times. Calls the 
    function f (defined for you in the grading script)
    to obtain the value of the function at any point.

    start: integer, the time at which exposure begins
    stop: integer, the time at which exposure ends
    step: float, the width of each rectangle. You can assume that
      the step size will always partition the space evenly.

    returns: float, the amount of radiation exposed to 
      between start and stop times.
    '''
    # FILL IN YOUR CODE HERE...
    totalRadiation=0
    while start    return totalRadiation

Hangman Part 1: Is The Word Guessed?

Please read the Hangman Introduction before starting this problem. The helper functions you will be creating in the next three exercises are simply suggestions, but you DO have to implement them if you want to get points for this Hangman Problem Set. If you’d prefer to structure your Hangman program in a different way, feel free to redo this Problem Set in a different way. However, if you’re new to programming, or at a loss of how to construct this program, we strongly suggest that you implement the next three helper functions before continuing on to Hangman Part 2.
开始这个问题之前请阅读前面关于猜单词游戏Hangman的介绍说明。在接下来的三个练习中,你将创建辅助函数,建议的方法是简单的,但你想获得这个Hangman问题集的计分的话,你必须完成它们。如果你想以另外的方式构建Hangman游戏程序,你可以在别的地方重做设计。如果你是新手,或者对构建这个程序没有头绪的话,强烈建议你在进入游戏设计第二部分之前,实现下面的3个辅助函数。
We’ll start by writing 3 simple functions that will help us easily code the Hangman problem. First, implement the function isWordGuessed that takes in two parameters - a string, secretWord, and a list of letters, lettersGuessed. This function returns a boolean - True if secretWord has been guessed (ie, all the letters of secretWord are in lettersGuessed) and False otherwise.
我们从编写3个简单的辅助函数开始,这对完整解决游戏设计有帮助。首先实现函数isWordGuessed, 它有2个参数输入- 一个字符串, secretWord, 一个字符列表, lettersGuessed. 函数返回逻辑值 - True 如果 secretWord 已经被猜中 (ie, secretWord 中的所有字母都在lettersGuessed) 或 False 否则.
Example Usage:

>>> secretWord = 'apple' 
>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print isWordGuessed(secretWord, lettersGuessed)
False

For this function, you may assume that all the letters in secretWord and lettersGuessed are lowercase.
函数里,你可以假设secretWord 与 lettersGuessed 都是小写字母.

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE...
    result=None
    for letter in secretWord:
        if letter in lettersGuessed:
            result=True
        else:
            result=False
            break
    return result

Printing Out The User’s Guess

Next, implement the function getGuessedWord that takes in two parameters - a string, secretWord, and a list of letters, lettersGuessed. This function returns a string that is comprised of letters and underscores, based on what letters in lettersGuessed are in secretWord. This shouldn’t be too different from isWordGuessed!
接着, 设计函数getGuessedWord,它有2个输入参数- 一个字符串, secretWord, 一个字母列表, lettersGuessed. 函数返回一个字符串,该返回串由字母与下划线组成,内容取决于lettersGuessed 中的字母是否是 secretWord的字母. 这个函数不应该与前面编写的代码isWordGuessed相差太大!
Example Usage:

>>> secretWord = 'apple' 
>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print getGuessedWord(secretWord, lettersGuessed)
'_ pp_ e'

When inserting underscores into your string, it’s a good idea to add at least a space after each one, so it’s clear to the user how many unguessed letters are left in the string (compare the readability of __ with _ _ _ _ ). This is called usability - it’s very important, when programming, to consider the usability of your program. If users find your program difficult to understand or operate, they won’t use it!
当在字符串中加入下划线时,一个好主意是后面加至少一个空格,这样用户看还有多少个字母未猜中会更清楚。 (比较一下形式 _ 与 _ _ _ ). 这是可用性( usability )- 这对编程是很重要的, 编程是要考虑可用性. 要是用户发现你的程序难以理解或操作,他们可能不会用它!
For this problem, you are free to use spacing in any way you wish - our grader will only check that the letters and underscores are in the proper order; it will not look at spacing. We do encourage you to think about usability when designing.
对这里的问题,你可以随意地使用空格-我们的检测程序只顺序地检测字母与下划线。我们鼓励你编程时考虑可用性。
For this function, you may assume that all the letters in secretWord and lettersGuessed are lowercase.
在函数中你可以假设 secretWord 和lettersGuessed 都是小写字母.

def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    wordStr=''
    for letter in secretWord:
        if letter in lettersGuessed:
            wordStr+=letter
        else:
            wordStr+='_ '
    return wordStr

Printing Out All Available Letters

Next, implement the function getAvailableLetters that takes in one parameter - a list of letters, lettersGuessed. This function returns a string that is comprised of lowercase English letters - all lowercase English letters that are not in lettersGuessed.
接着,实现函数getAvailableLetters,它接受一个输入参数- 一个字母列表lettersGuessed. 函数返回所有没有被猜测过(即不在lettersGuessed中)的小写字母按字母顺序组成的字符串。
Example Usage:

>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print getAvailableLetters(lettersGuessed)
abcdfghjlmnoqtuvwxyz

Note that this function should return the letters in alphabetical order, as in the example above.注意返回的字符串要按字母顺序,如上所示。
For this function, you may assume that all the letters in lettersGuessed are lowercase.这里你可以假设lettersGuessed中的字母都是小写的。
Hint: You might consider using string.ascii_lowercase, which is a string comprised of all lowercase letters:提示: 你可以考虑使用 string.ascii_lowercase,来获得所有小写字母:

>>> import string
>>> print string.ascii_lowercase
abcdefghijklmnopqrstuvwxyz
def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
    import string
    availableLetters=string.ascii_lowercase
    for letter in lettersGuessed:
        if letter in availableLetters:
            availableLetters=availableLetters.replace(letter,'')
    return availableLetters

Hangman Part 2: The Game

感谢 glhezjnucn 童鞋的给力翻译!!
Now you will implement the function hangman, which takes one parameter - the secretWord the user is to guess. This starts up an interactive game of Hangman between the user and the computer. Be sure you take advantage of the three helper functions, isWordGuessed, getGuessedWord, and getAvailableLetters, that you’ve defined in the previous part.
现在你将开始实现(完整的游戏)程序hangman, 它有一个输入参数 –用户需要去猜测的单词 secretWord(后续程序用到的变量名). 由它开始游戏程序Hangman用户与计算机的交互过程。确保你用到之前部分设计完成的辅助函数isWordGuessed, getGuessedWord, 和 getAvailableLetters.
Hints 提示:

  1. You should start by noticing where we’re using the provided functions (at the top of ps3_hangman.py) to load the words and pick a random one. Note that the functions loadWords and chooseWord should only be used on your local machine, not in the tutor. When you enter in your solution in the tutor, you only need to give your hangman function.
    你需要注意到我们何时用到提供的函数 (程序 ps3_hangman.py的开始处) 来装入词库以及随机取词. 注意函数loadWords 和 chooseWord 只能用于你本地机器调试时,在提交代码中不用到. 当你提交解答代码时,你只须给出函数hangman.

  2. Consider using lower() to convert user input to lower case. For example:考虑用函数 lower() 来将用户的输入转换为小写。
    guess = 'A'
    guessInLowerCase = guess.lower()

  3. Consider writing additional helper functions if you need them!如果你需要,你可以考虑编写额外的辅助函数

  4. There are four important pieces of information you may wish to store:4个重要的信息你可能需要考虑保存

    1. secretWord: The word to guess.猜测的目标单词.
    2. lettersGuessed: The letters that have been guessed so far.追踪用户猜测过的字母
    3. mistakesMade: The number of incorrect guesses made so far.记录用户猜测错误的次数
    4. availableLetters: The letters that may still be guessed. Every time a player guesses a letter, the guessed letter must be removed from availableLetters (and if they guess a letter that is not in availableLetters, you should print a message telling them they’ve already guessed that - so try again!).还可以用来被猜测的字母,用户猜测过的字母需要从availableLetters移除,用户重复猜了的字母,你需要告知用户你已经猜过这个字母。
def hangman(secretWord):
    '''
    secretWord: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many 
      letters the secretWord contains.

    * Ask the user to supply one guess (i.e. letter) per round.

    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computers word.

    * After each round, you should also display to the user the 
      partially guessed word so far, as well as letters that the 
      user has not yet guessed.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE...
    print 'Welcome to the game Hangman!'
    print 'I am thinking of a word that is '+str(len(secretWord))+' letters long'
    print '-------------'
    guessTime = 8
    wordGuessed = ''
    lettersGuessed = ()
    while (guessTime > 0):
        print 'You have '+str(guessTime)+' guesses left'
        print 'Available letters: '+getAvailableLetters(lettersGuessed)
        inputLetter = raw_input('Please guess a letter: ')
        aLetter = inputLetter.lower()
        if aLetter in lettersGuessed:
            print 'Oops! You\'ve already guessed that letter: '+getGuessedWord(secretWord, lettersGuessed)
            print '------------'
        else:
            lettersGuessed += (aLetter,)
            if aLetter in secretWord:
                print 'Good guess: '+getGuessedWord(secretWord, lettersGuessed)
                print '------------'
                if getGuessedWord(secretWord,lettersGuessed).count('_ ') ==0:
                    print 'Congratulations,you won!'
                    break
            else:
                guessTime -= 1
                print 'Oops! That letter is not in my word: '+getGuessedWord(secretWord, lettersGuessed)
                print '------------'
    if guessTime == 0:
        print 'Sorry,you ran out of guesses.The word was '+str(secretWord)+'.'

If you’re with us,then be with us. ——Storm

你可能感兴趣的:(MIT,python,分享)