lecture 3.2 problem set 3

简介: "Radioactive decay" is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation.

"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(理解为度量的单位即量纲)。如下是一个辐射衰减曲线的样例:

(Click on the pictures to view full-sized images)

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年她所接触的辐射总量。

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在那段时间所接触的辐射量(技术地说,是由于伽玛射线与贝塔粒子,不过这太复杂了,因此我们忽略,抱歉,物理学家们!)

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个,每年一个长方形。

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:

当我们如此做了之后,我们可以非常简便的计算出每个长方形的面积。回忆一下长方形的面积是宽度乘以高度。而这里(红色的)长方形的高度:

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.
    '''

To complete this function you'll need to know what the value of the radioactive decay curve is at various points. There is a function f that will be defined for you that you can call from within your function that describes the radioactive decay curve for the problem.

要完成这个函数的设计,你需要知道辐射曲线在不同点的值。有一个函数f已经为你准备好,你可以在你的函数内部直接调用,以获取辐射衰减曲线对应的问题。

You should implement this function on your own machine. Open a new Canopy Python file and title it "radiationExposure.py". Complete your work inside this file. Test your code well in Canopy, and when you are convinced it is correct, cut and paste your definition into this tutor window.

你应当在自己的机器上实现该函数,新建一个函数并取名为radiationExposure.py,在文件内完成你的工作。在Canopy中充分测试你的代码,当你确信它正确之后,复制粘贴到如下代码窗口。

def radiationExposure(start, stop, step):
    s = 0

    while(start < stop):
        s += f(start) * step
        start += step
    return s
    

2

 IS THE WORD GUESSED?

 
(5/5 分数)

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 andlettersGuessed are lowercase.

函数里,你可以假设secretWord 与 lettersGuessed 都是小写字母.

def isWordGuessed(secretWord, lettersGuessed):
    flag = 1
    for c in secretWord:
        if c in lettersGuessed:
            flag = 1
        else:
            flag = 0
            break

    if flag:
        return True
    else:
        return False

3

PRINTING OUT THE USER'S GUESS

 
(5/5 分数)

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 andlettersGuessed are lowercase.

在函数中你可以假设 secretWord 和lettersGuessed 都是小写字母.

def getGuessedWord(secretWord, lettersGuessed):
    s = ''
    for c in secretWord:
        if c in lettersGuessed:
            s += c
        else:
            s += '_'
    return s

4

PRINTING OUT ALL AVAILABLE LETTERS

 
(5/5 分数)

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):
    import string
    s = string.ascii_lowercase

    for e in lettersGuessed:
        if e in s:
           s = s.replace(e, '')
    return s

5

THE GAME

 
(15/15 分数)

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, isWordGuessedgetGuessedWord, and getAvailableLetters, that you've defined in the previous part.

现在你将开始实现(完整的游戏)程序hangman, 它有一个输入参数 –用户需要去猜测的单词 secretWord(后续程序用到的变量名). 由它开始游戏程序Hangman用户与计算机的交互过程。确保你用到之前部分设计完成的辅助函数isWordGuessed, getGuessedWord, 和 getAvailableLetters.

Hints 提示:

  • 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.

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

    guess = 'A'
    guessInLowerCase = guess.lower()
  • Consider writing additional helper functions if you need them!如果你需要,你可以考虑编写额外的辅助函数

  • 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移除,用户重复猜了的字母,你需要告知用户你已经猜过这个字母。

Sample Output 输出样例


Note that if you choose to use the helper functions isWordGuessedgetGuessedWord, or getAvailableLetters, you do not need to paste your definitions in the box. We have supplied our implementations of these functions for your use in this part of the problem. If you use additional helper functions, you will need to paste those definitions here.

注意你如果用到了辅助函数isWordGuessed, getGuessedWord, 或者getAvailableLetters, 你无需将他们的代码复制到这里的代码窗口。这一部分我们会提供这些函数让你直接使用。但如果你用了其他的辅助函数,你需要将代码也复制在这里的代码窗口。

Your function should include calls to raw_input to get the user's guess.你的函数需要用 raw_input 来获得用户的每次猜测.

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


目录
相关文章
|
机器学习/深度学习 编解码 人工智能
Reading Notes: Human-Computer Interaction System: A Survey of Talking-Head Generation
由于人工智能的快速发展,虚拟人被广泛应用于各种行业,包括个人辅助、智能客户服务和在线教育。拟人化的数字人可以快速与人接触,并在人机交互中增强用户体验。因此,我们设计了人机交互系统框架,包括语音识别、文本到语音、对话系统和虚拟人生成。接下来,我们通过虚拟人深度生成框架对Talking-Head Generation视频生成模型进行了分类。同时,我们系统地回顾了过去五年来在有声头部视频生成方面的技术进步和趋势,强调了关键工作并总结了数据集。 对于有关于Talking-Head Generation的方法,这是一篇比较好的综述,我想着整理一下里面比较重要的部分,大概了解近几年对虚拟人工作的一些发展和
|
7月前
|
人工智能
Big Water Problem
Big Water Problem
41 0
|
机器学习/深度学习 存储 数据采集
DCFEE: A Document-level Chinese Financial Event Extraction System based on Automatically Labeled论文解读
我们提出了一个事件抽取框架,目的是从文档级财经新闻中抽取事件和事件提及。到目前为止,基于监督学习范式的方法在公共数据集中获得了最高的性能(如ACE 2005、KBP 2015)。这些方法严重依赖于人工标注的训练数据。
146 0
|
分布式计算 Spark
《Problem Solving Recipes Learned from Supporting Spark》电子版地址
Problem Solving Recipes Learned from Supporting Spark
71 0
《Problem Solving Recipes Learned from Supporting Spark》电子版地址
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
147 0
|
数据库
When Tech Meets Love – Smarter Ways to NOT be Single
It’s that time of year again. Single’s Day (a.k.a Double 11) is just around the corner, people buying gifts for loved ones.
1634 0
When Tech Meets Love – Smarter Ways to NOT be Single
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1047 0
|
监控 关系型数据库 数据库
知新之--12-factors
作为总的原则,在程序设计上很有高度。。。 参考URL:http://12factor.net/zh_cn/ ========================================== 12-factors I.
1423 0