• RADIATION EXPOSURE
    挺简单的一道题,计算函数和算法过程都已经给出,做一个迭代计算就行了。

     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...
    area = 0
    while start < stop:
    area = area + f(start) * step
    start += step return area
  • A WORDGAME: HANGMAN
    一个猜单词游戏,个人感觉略有难度,游戏规则见这里。将其分步实现相应的函数。

     # 6.00 Problem Set 3
    #
    # Hangman game
    # # -----------------------------------
    # Helper code
    # You don't need to understand this helper code,
    # but you will have to know how to use the functions
    # (so be sure to read the docstrings!) import random
    import string WORDLIST_FILENAME = "words.txt" def loadWords():
    """
    Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may
    take a while to finish.
    """
    print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = string.split(line)
    print " ", len(wordlist), "words loaded."
    return wordlist def chooseWord(wordlist):
    """
    wordlist (list): list of words (strings)
    Returns a word from wordlist at random
    """
    return random.choice(wordlist) # end of helper code
    # ----------------------------------- # Load the list of words into the variable wordlist
    # so that it can be accessed from anywhere in the program
    wordlist = loadWords() 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...
    for c in secretWord:
    if c not in lettersGuessed:
    return False return True 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...
    guess = ""
    for c in secretWord:
    if c in lettersGuessed:
    guess += c
    else:
    guess += "_ " return guess 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...
    available = ""
    for c in string.ascii_lowercase:
    if c not in lettersGuessed:
    available += c return available 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", len(secretWord), "letters long."
    print "-------------"
    lettersGuessed = []
    mistakesMade = 0
    availableLetters = string.ascii_lowercase while mistakesMade < 8 and not isWordGuessed(secretWord, lettersGuessed):
    print "You have", 8 - mistakesMade, "guesses left."
    print "Available letters:", availableLetters
    guess = raw_input("Please guess a letter: ").lower() if guess in lettersGuessed:
    print("Oops! You've already guessed that letter: " +
    getGuessedWord(secretWord, lettersGuessed))
    elif guess in secretWord:
    lettersGuessed.append(guess)
    print "Good guess:", getGuessedWord(secretWord, lettersGuessed)
    else:
    mistakesMade += 1
    lettersGuessed.append(guess)
    print("Oops! That letter is not in my word: " +
    getGuessedWord(secretWord, lettersGuessed)) availableLetters = getAvailableLetters(lettersGuessed) print "------------" if isWordGuessed(secretWord, lettersGuessed):
    print "Congratulations, you won!"
    else:
    print("Sorry, you ran out of guesses. The word was " + secretWord +
    ".") # When you've completed your hangman function, uncomment these two lines
    # and run this file to test! (hint: you might want to pick your own
    # secretWord while you're testing) secretWord = chooseWord(wordlist).lower()
    hangman(secretWord)

mooc课程mit 6.00.1x--problem set3解决方法的更多相关文章

  1. mooc课程mit 6.00.1x--problem set2解决方法

    PAYING THE MINIMUM 计算每月信用卡最低还款额及剩余应还款额 balance = 4842 #还款额 annualInterestRate = 0.2 #年利息比率 monthlyPa ...

  2. Mysql 时间格式默认空串 '0000-00-00 00:00:00' select抛出异常的解决方法

    Mysql 时间格式默认插入值为空时,会以'0000-00-00 00:00:00'填充,这时如果select时会抛出SQLExecption如下: java.sql.SQLException: Va ...

  3. ubuntu 12.04 ubuntu System program problem detected 解决方法

    1. ubuntu System program problem detected本人操作系统是ubuntu12.04,不知道是系统出了问题还是装的软件有问题,每次开机都出现:System progr ...

  4. LoadRunner11.00安装问题及解决方法

    1.安装提示:“安装程序已确定正在等待重新启动....” 解决方法:打开安装包,找到:\Additional Components\IDE Add-Ins\MS Visual Studio .NET\ ...

  5. 我的首个MOOC课程《面向对象软件开发实践》

    我的首个MOOC课程<面向对象软件开发实践> 我将在网易云课堂开讲MOOC课<面向对象软件开发实践>(http://mooc.study.163.com/course/YOOK ...

  6. 《Linux内核 》MOOC 课程

    姬梦馨 原创微博 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 学习笔记 一:什么是冯诺依曼体系结构? ...

  7. MOOC课程信息D3.js动态可视化

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/83153693 - 写在前面 好久没更新博客了,主要还是最近 ...

  8. 中国大学MOOC课程信息之数据分析可视化二

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...

  9. 中国大学MOOC课程信息之数据分析可视化一

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82263391 9月2日更:中国大学MOOC课程信息之数据分 ...

随机推荐

  1. Algorithm | Random

    随机生成[0,n)中不重复的m个数. class Random { public: Random(int n, int m):n(n), m(m) {} void generate() { srand ...

  2. 最短Hamilton路径

    题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每 ...

  3. windows 下使用github客户端报错:Failed to publish this branch

    在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...

  4. iOS进行单元测试OCUnit+xctool

    单元测试 什么是单元测试 wiki解释 简单说来就是为你的方法多专门写一个测试函数.以保证你的方法在不停的修改开发中.保持正确.如果出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量. 我 ...

  5. iOS5可能会删除本地文件储存

    文/ Nick (iphoneincubator) 关于iOS 5的本地文件储存Marco(Instapaper 的开发者)写过一篇很好的帖子阐述过相关问题,有兴趣的同学可以先阅读下他的文章然后再看下 ...

  6. Android开发初期之后怎么提升?怎么才能叫精通?方向在哪?

    hi大头鬼hi Android开发专家     先mark一下,好多人我发现始终停留在两三年的水平上没有突破. 另外还有一个误区就是越底层越牛逼 第三个就是,我认识的大部分所谓的做过rom开发的对fr ...

  7. struts2拦截器实现session超时返回登录页面(iframe下跳转到其父页面)

    需求:session超时时,返回登录页面,由于页面嵌套在iframe下,因此要跳转到登录页面的父页面,但是首页,登录页面等不需要进行跳转 实现: java文件:SessionIterceptor.ja ...

  8. MySQL开发36条军规

    转载地址:http://blog.itpub.net/22664653/viewspace-723506/ 写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过 ...

  9. Injection of resource dependencies failed解决办法总结

    今天调试项目代码,出现的引resource的报错,查原因查了好长时间才找到,现在这里总结一下,以免以后忘掉以及给大家参考. 报错大致内容入下: org.springframework.beans.fa ...

  10. Linux下的定时任务Crontab

    通过crontab -e写入定时任务的指令,一行为一项任务. 任务模式是时间克龙表达式+命令形式. 如: 2 0,6,12,18 * * * perl /root/restarttomcat.pl p ...