mooc课程mit 6.00.1x--problem set3解决方法
2024-08-29 07:18:17
- 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)
最新文章
- POJ 1144
- BI领导驾驶舱的功能特点
- tomcat7+jdk1.8一键安装脚本
- ecstore-lnmp环境下crontab不执行原因
- 矩阵乘法的MapReduce实现
- CF 55D. Beautiful numbers(数位DP)
- coreseek实战(一):windows下coreseek的安装与测试
- CentOS 编译安装 mysql
- 使用Perl5获取有道词典释义
- Linux源代码情景分析读书笔记 物理页面的分配
- C#:继承多态的方法实现数的简单加减乘除运算
- Jmeter 参数化请求实例
- imp、exp命令导出优化
- 爬虫学习--http请求详解
- 【Java】 剑指offer(26) 树的子结构
- win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please install the Redistributable then run this installer again 的解决办法
- 【Cuda编程】加法归约
- 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
- 转 springboot 教程
- CentOS 7运维管理笔记(12)----PHP页面失去焦点后变成空白的解决方法