selfteaching-learning-notes / selfteaching-learning-notes.github.io

自学营学员学习笔记
https://selfteaching-learning-notes.github.io
15 stars 83 forks source link

1901050017-MIT-psets 0-5 #27

Open kagamiqyah opened 5 years ago

kagamiqyah commented 5 years ago

学员信息

学习笔记

  1. 要求:

    Write a program that does the following in order:

    • Asks the user to enter a number “x”
    • Asks the user to enter a number “y”
    • Prints out the number “x”, raised to the power “y”.
    • Prints out the log (base 2) of “x”.
  2. 思路:

    • input来完成ask要求
    • 需要引入模块math来完成剩下的要求
  3. 代码:

    import math
    x = int(input('Enter number x:'))
    y = int(input('Enter number y:'))
    print('x**y=',x**y)
    print('log(x)=',math.log(x,2))
kagamiqyah commented 5 years ago

学员信息

学习笔记

  1. 要求: Must save money to pay a house down payment. Write a program to calculate how long it will be done. Given the following assumptions:

    • Call the cost of your dream home total_cost.
    • Call the portion of the cost needed for a down payment portion_down_payment. For simplicity, assume that portion_down_payment = 0.25 (25%).
    • Call the amount that you have saved thus far current_savings. You start with a current savings of $0.
    • Assume that you invest your current savings wisely, with an annual return of r (in other words, at the end of each month, you receive an additional current_savings*r/12 funds to put into your savings – the 12 is because r is an annual rate). Assume that your investments earn a return of r = 0.04 (4%).
    • Assume your annual salary is annual_salary.
    • Assume you are going to dedicate a certain amount of your salary each month to saving for the down payment. Call that portion_saved. This variable should be in decimal form (i.e. 0.1 for 10%).
    • At the end of each month, your savings will be increased by the return on your investment, plus a percentage of your monthly salary (annual salary / 12). Ask the user to enter the following variables:
    • The starting annual salary (annual_salary)
    • The portion of salary to be saved (portion_saved)
    • The cost of your dream home (total_cost)
  2. 思路: 大多数变量都已给出。所以需要想的只有初始化存钱金额,每一次循环迭代后,存钱金额增加,增加的金额还需乘以利率。满足首付要求时,退出循环,输出所需月份。

  3. 代码:

    
    """
    house hunting
    save some money to get a dream house.
    """
    annual_salary = float(input('Enter your annual salary:'))
    portion_saved = float(input('Enter the percent of your salary to save, as a decimal:'))
    total_cost = float(input('Enter the cost of your dream home:'))

portion_down_payment = 0.25 monthly_salary = annual_salary / 12 r = 0.04 cost = total_cost * portion_down_payment

initialize state variables

current_savings = 0 num_month = 1 current_savings += monthly_salary portion_saved while current_savings < cost: current_savings += current_savings r/12 current_savings += monthly_salary * portion_saved num_month +=1 print('Number of months:',num_month)

kagamiqyah commented 5 years ago

学员信息

学习笔记

  1. 要求: Assume every six months get an increase raise,then to calculate how many months you will be done to pay enough down payment.

  2. 思路: 用取模%方法来判断是否符合六个月加薪要求。

  3. 代码:

    
    """
    how many months it will take you save up enough money for a downpayment.
    assume every six months get a increase raise.
    """
    annual_salary = float(input('Enter your annual salary:'))
    portion_saved = float(input('Enter the percent of your salary to save, as a decimal:'))
    total_cost = float(input('Enter the cost of your dream home:'))
    semi_annual_raise = float(input('Enter the semi annual raise, as a decimal:'))

portion_down_payment = 0.25 monthly_salary = annual_salary / 12 r = 0.04 cost = total_cost * portion_down_payment

initialize state variables

current_savings = 0 num_month = 1 current_savings += monthly_salary portion_saved while current_savings < cost: current_savings += current_savings r/12 if num_month % 6 ==0: monthly_salary += monthly_salary * semi_annual_raise

current_savings += monthly_salary * portion_saved
num_month +=1

print('Number of months:',num_month)

kagamiqyah commented 5 years ago

学员信息

学习笔记

  1. 要求: Use the bisection search to find the best rate of savings to achieve a down payment on a $1M house in 36 months.

  2. 思路: 把 36个月的储存薪水方案打包成一个函数,方便作为实参带入到二分法里,然后用二分法来寻找最适合的存钱比例。如果百分百把薪水存起来在36个月的时候都无法完成首付,还要给出一个交代。

  3. 代码:

    
    """
    find the best rate of savings to achieve a down payment on a $1M house in 36 months.
    """
    annual_salary = int(input('Enter the starting salary:'))
    #use the bisection research to find best saving rate
    epsilon = 100
    low = 0
    high = 10000
    guess = int((high + low)/2)

initialize state variables

to simply things,assume semi raise,return,downpayment,house cost are all fixed.

current_savings = 0 num_month = 1 portion_down_payment = 0.25 total_cost = 1000000 cost = total_cost * portion_down_payment

define a best saving rate function, to reuse it.

def current_savings_func(i): semi_annual_raise = 0.07 #given some fixed data r = 0.04 monthly_salary = annual_salary /12 num_month =1 current_savings = 0 current_savings += monthly_salaryi/10000 #i/10000 is assume a savingrate while num_month <36: current_savings += current_savings r/12 if num_month % 6 == 0: monthly_salary = 1 +semi_annual_raise current_savings += monthly_salary i/10000 num_month +=1 return current_savings

assume all monthly salary is saved

if current_savings_func(high) < cost: print('It is not possible to pay the down payment in three years.')

use the bisection research to get the saving rate

num_guesses = 1 while abs(current_savings_func(guess)-cost) >= epsilon: if current_savings_func(guess) < cost: low = guess else: high = guess guess = int((high + low)/2) num_guesses +=1 print('Best saving rate:',guess/10000) print('Steps in bisection search:', num_guesses)

kagamiqyah commented 5 years ago

学员信息

学习心得

  1. 实现目标: 写一个猜单词游戏Hang­man. 开始时系统会随机选择一个英文单词,user有一定数量的机会,如 6 次机会。每轮猜一个字母,如果猜的字母是对的,则系统会写出此字母在单词中的位置;否则user的剩余机会减少一次。如果user猜出单词,则胜利,显示得分;如果机会用完,则失败,会把正确的单词显示出来。

  2. 思路及代码:

    Part 1 Basic Hangman

    直接运行已给出代码,看看是否正确运行。找到‘打开文件’那句代码,把要打开的文件的所在位置添上去,我的是mac,如果不给地址,会显示找不到文件。 运行正确后,会出现结果

    python hangman.py Loading word list from file... 55900 words loaded.

Part 2 Hangman: Three helper functions

a. 要求编写is_word_guessed函数。 功能是判断user 猜过的字母是否能够组成完整的单词。 该函数接受的参数:一个字符串secret_word 表示要猜的单词,一个字符串构成的列表letters_guessed表示user已经猜过的字母。 返回的是布尔值,猜出单词返回 True,否则返回False

实现该函数的思路是,遍历 secret_word 中的每个字母,判断它们是否都在 letters_guessed中,全部符合条件,返回 True;其他,则返回 False。

def is_word_guessed(secret_word, letters_guessed):
    for letter in secret_word:
        if letter not in letters_guessed:
            return False  

    return True  

b. 要求编写 get_guessed_word 函数。 功能是返回一个字符串,由已猜出的字母和下划线加空格组成。 该函数接受的参数,一个字符串 secret_word 表示要猜的单词,一个字符串构成的列表 letters_guessed表示user已经猜过的字母。

该函数的思路是,遍历secret_word中的每个字母,如果该字母在 letters_guessed 里,则添加到字符串里;如果没有,向字符串添加 '_ '。

def get_guessed_word(secret_word, letters_guessed):
    guessed_word = ''  
    for letter in secret_word:
        if letter not in letters_guessed:
            guessed_word += '_ '  
        else:
            guessed_word += letter  
    return guessed_word

c. 要求编写get_available_letters 函数。 功能是获取user还没猜过的字母。 该函数接收参数为列表letters_guessed.

思路是用string.ascii_lowercase 创建 26 个小写字母的列表(a-z),之后剔除letters_guessed里的字母,用string.join将列表拼接成字符串。

def get_available_letters(letters_guessed):
    # available_letters: list
    available_letters = list(string.ascii_lowercase) 
    for letter in letters_guessed:
        if letter in available_letters:
            available_letters.remove(letter)  

    return ''.join(available_letters)  
kagamiqyah commented 5 years ago

学员信息

学习心得

  1. 要求:
    • 编写hangman函数。这个函数有一个参数secret_word,表示要猜测的单词,在函数中需要使用 input 等函数和user进行交互。
    • 编写match_with_gaps函数。这个函数的功能是将一个部分猜出的单词和一个完整单词相比较,返回布林值
  2. 思路:
    • 提示user剩余猜测次数,以及打印尚未猜过的字母
    • 提示并接收user输入
    • 如果user什么都没输入就按下了回车,或者输入了超过 1 个字符,那么这些输入被认为是不合法的,应该打印一条错误信息,并立即要求user重新输入
    • 按 User In­put Re­quire­ments来检查user 输入是否符合要求
    • 将部分猜出的字母分别和完整单词中的字母进行位置比较,返回布林值。

3.代码:

def hangman(secret_word):
    '''
    secret_word: 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 secret_word contains and how many guesses s/he starts with.

    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.

    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!

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

    * After each guess, you should display to the user the
      partially guessed word so far.

    Follows the other limitations detailed in the problem write-up.
    '''

    def isvowel(letter):
        '''
        letter: one single ahphabetical letter.
        returns: bollean, True if the letter is a vowel; False otherwise.
        '''
        if len(letter) == 1 and letter in 'aeiou':
            return True
        else:
            return False

    guesses_remaining = 6  # remaining guess times
    warnings_remaining = 3  # remaining warning times
    letters_guessed = []  # a list of letters are alreay be guessed

    # welcome information
    print('Welcome to the game Hangman!')
    print('I am thinking of a word that is', len(secret_word), 'letters long.')
    print('You have', warnings_remaining, 'warnings'
          if warnings_remaining > 1 else 'warning', 'left.')
    print('----------------')

    # quit loop when remainning times equals zero
    while guesses_remaining > 0 and not is_word_guessed(
            secret_word, letters_guessed):
        print('You have', guesses_remaining, 'guesses'
              if guesses_remaining > 1 else 'guess', 'left.')
        print('Available letters:', get_available_letters(letters_guessed))
        current_guess = input('Pleaes guess a letter: ').lower()  

        if len(current_guess) != 1:
            print('You must input exactly one letter! Try again.')
            print('----------------')
            continue

        # test input requirements
        # not alpha
        if not current_guess.isalpha():
            print('Opps! That is not a valid letter.', end=' ')
            if warnings_remaining > 0:  # 剩余警告次数不为0
                warnings_remaining -= 1
                print(
                    'You have',
                    warnings_remaining,
                    'warnings' if warnings_remaining > 1 else 'warning',
                    'left:',
                    end=' ')
            else:  # remaining warning time = 0
                guesses_remaining -= 1
                print(
                    'You have no warnings left so you lose one guess:',
                    end=' ')
            print(get_guessed_word(secret_word, letters_guessed))
        # input a letter is already guesses
        elif current_guess in letters_guessed:
            print('Oops! You have already guessed that letter.', end=' ')
            if warnings_remaining > 0:  
                warnings_remaining -= 1
                letters_guessed.append(current_guess)
                print(
                    'You have',
                    warnings_remaining,
                    'warnings' if warnings_remaining > 1 else 'warning',
                    'left:',
                    end=' ')
            else: 
                guesses_remaining -= 1
                print(
                    'You have no warnings left so you lose one guess:',
                    end=' ')
            print(get_guessed_word(secret_word, letters_guessed))
        # input a new letter
        else:
            letters_guessed.append(current_guess)  
            if current_guess in secret_word:
                print('Good guess:',
                      get_guessed_word(secret_word, letters_guessed))
            else:
                print('Oops! That letter is not in my word:',
                      get_guessed_word(secret_word, letters_guessed))
                if isvowel(current_guess):
                    guesses_remaining -= 2 
                else:
                    guesses_remaining -= 1  

        print('----------------')
        time.sleep(0.5)  # enhance the interactive experience

    # show the result
    if is_word_guessed(secret_word, letters_guessed):
        total_score = guesses_remaining * len(set(secret_word))  
        print('Congratulations, you won!')
        print('Your total score for this game is:', total_score)
    else:
        print('Sorry, you ran out of guesses. The word was',
              '`' + secret_word + '`.')

# When you've completed your hangman function, scroll down to the bottom
# of the file and uncomment the first two lines to test
# (hint: you might want to pick your own
# secret_word while you're doing your own testing)

# -----------------------------------

def match_with_gaps(my_word, other_word):
    '''
    my_word: string with _ characters, current guess of secret word
    other_word: string, regular English word
    returns: boolean, True if all the actual letters of my_word match the
        corresponding letters of other_word, or the letter is the special symbol
        _ , and my_word and other_word are of the same length;
        False otherwise:
    '''

    my_word = my_word.replace(' ', '')  # remove the space
    if len(my_word) != len(other_word):
        return False  
    else:
        for i in range(len(my_word)):
            if my_word[i].isalpha() and my_word[i] != other_word[i]:
                return False  
            if my_word[i] == '_' and other_word[i] in my_word:
                return False 
        return True  

def show_possible_matches(my_word):
    '''
    my_word: string with _ characters, current guess of secret word
    returns: nothing, but should print out every word in wordlist that matches my_word
             Keep in mind that in hangman when a letter is guessed, all the positions
             at which that letter occurs in the secret word are revealed.
             Therefore, the hidden letter(_ ) cannot be one of the letters in the word
             that has already been revealed.

    '''

    # iterate wordlist to find a word that matches the secret word
    possible_maches = [
        word for word in wordlist if match_with_gaps(my_word, word)
    ]
    if len(possible_maches):
        for match in possible_maches:
            print(match, end=' ')
    else:
        print('No matches found')
    print()  

def hangman_with_hints(secret_word):
    '''
    secret_word: 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 secret_word contains and how many guesses s/he starts with.

    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.

    * Ask the user to supply one guess per round. Make sure to check that the user guesses a letter

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

    * After each guess, you should display to the user the
      partially guessed word so far.

    * If the guess is the symbol *, print out all words in wordlist that
      matches the current guessed word.

    Follows the other limitations detailed in the problem write-up.
    '''

    def isvowel(letter):
        '''
        letter: one single ahphabetical letter.
        returns: bollean, True if the letter is a vowel; False otherwise.
        '''
        if len(letter) == 1 and letter in 'aeiou':
            return True
        else:
            return False

    guesses_remaining = 6  
    warnings_remaining = 3  
    letters_guessed = []  

    print('Welcome to the game Hangman!')
    print('I am thinking of a word that is', len(secret_word), 'letters long.')
    print('You have', warnings_remaining, 'warnings'
          if warnings_remaining > 1 else 'warning', 'left.')

    while guesses_remaining > 0 and not is_word_guessed(secret_word, letters_guessed):
        print('----------------')

        print('You have', guesses_remaining,
              'guesses' if guesses_remaining > 1 else 'guess', 'left.')
        print('Available letters:', get_available_letters(letters_guessed))
        current_guess = input('Pleaes guess a letter: ').lower()  

        if len(current_guess) != 1:
            print('You must input exactly one letter! Try again.')
            continue

        if current_guess == '*':
            print('Possible word matches are:')
            show_possible_matches(
                get_guessed_word(secret_word, letters_guessed))
            continue

        if not current_guess.isalpha():
            print('Opps! That is not a valid letter.', end=' ')
            if warnings_remaining > 0:  
                warnings_remaining -= 1
                print('You have', warnings_remaining,
                      'warnings' if warnings_remaining > 1 else 'warning', 'left:', end=' ')
            else:  
                guesses_remaining -= 1
                print(
                    'You have no warnings left so you lose one guess:',
                    end=' ')
            print(get_guessed_word(secret_word, letters_guessed))

        elif current_guess in letters_guessed:
            print('Oops! You have already guessed that letter.', end=' ')
            if warnings_remaining > 0:  
                warnings_remaining -= 1
                letters_guessed.append(current_guess)
                print(
                    'You have',
                    warnings_remaining,
                    'warnings' if warnings_remaining > 1 else 'warning',
                    'left:',
                    end=' ')
            else:  
                guesses_remaining -= 1
                print(
                    'You have no warnings left so you lose one guess:',
                    end=' ')
            print(get_guessed_word(secret_word, letters_guessed))

        else:
            letters_guessed.append(current_guess)  
            if current_guess in secret_word:
                print('Good guess:',
                      get_guessed_word(secret_word, letters_guessed))
            else:
                print('Oops! That letter is not in my word:',
                      get_guessed_word(secret_word, letters_guessed))
                if isvowel(current_guess):
                    guesses_remaining -= 2  
                else:
                    guesses_remaining -= 1  

        time.sleep(0.5)  

    print('----------------')

    if is_word_guessed(secret_word, letters_guessed):
        total_score = guesses_remaining * len(
            set(secret_word))  
        print('Congratulations, you won!')
        print('Your total score for this game is:', total_score)
    else:
        print('Sorry, you ran out of guesses. The word was',
              '`' + secret_word + '`.')

# When you've completed your hangman_with_hint function, comment the two similar
# lines above that were used to run the hangman function, and then uncomment
# these two lines and run this file to test!
# Hint: You might want to pick your own secret_word while you're testing.

if __name__ == "__main__":
    # To test part 2, comment out the pass line above and
    # uncomment the following two lines.

    #secret_word = choose_word(wordlist)
    # hangman(secret_word)

    ###############

    # To test part 3 re-comment out the above lines and
    # uncomment the following two lines.

    secret_word = choose_word(wordlist)
    hangman_with_hints(secret_word)