mooc课程mit 6.00.1x--problem set3解决方法

本文探讨了如何使用编程解决两个问题:计算辐射暴露和实现一个猜单词游戏。通过迭代计算来评估不同时间段内的辐射暴露,并通过分步骤实现猜单词游戏的规则,包括加载词汇列表、选择单词、检查猜测字母是否正确、获取已猜字母的单词展示等。此文章深入介绍了编程技巧和逻辑思维在解决实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

     

转载于:https://www.cnblogs.com/honoka/p/4795895.html

内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值