Project 2: CS 61A Autocorrected Typing Software

Problem 1 (1 pt)

Implement choose, which selects which paragraph the user will type. It takes a list of paragraphs (strings), a select function that returns True for paragraphs that can be selected, and a non-negative index k. The choose function return's the kth paragraph for which select returns True. If no such paragraph exists (because k is too large), then choose returns the empty string.

问题解析:

1.注意要求返回满足select函数的第K个段落

2.超出满足select的段落数组长度,返回空字符串('') 

def choose(paragraphs, select, k):
    """Return the Kth paragraph from PARAGRAPHS for which SELECT called on the
    paragraph returns true. If there are fewer than K such paragraphs, return
    the empty string.
    """
    # BEGIN PROBLEM 1
    "*** YOUR CODE HERE ***"
    list_paragraph=[]
    select_cot=0

    for paragraph in paragraphs:
        if select(paragraph):
            list_paragraph.append(paragraph)
            select_cot+=1
    if k>=select_cot:
        return ''
    return list_paragraph[k]
    # END PROBLEM 1T

Problem 2 (2 pt)

Implement about, which takes a list of topic words. It returns a function which takes a paragraph and returns a boolean indicating whether that paragraph contains any of the words in topic. The returned function can be passed to choose as the select argument.

To make this comparison accurately, you will need to ignore case (that is, assume that uppercase and lowercase letters don't change what word it is) and punctuation.

Assume that all words in the topic list are already lowercased and do not contain punctuation.

Hint: You may use the string utility functions in utils.py.

 可以仔细看看utils.py.里的函数,是解决本题的关键:

1.利用lower函数将两个判断的对象均改为小写

2.利用remove_punctuation函数将paragraph里的标点去掉

3.利用split函数将paragraphy分为一个个单词


def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"
    def select(paragraph):
        for topic_element in topic:
            if lower(topic_element) in split(remove_punctuation(lower(paragraph))):
                return True
        return False
    return select
    # END PROBLEM 2

Problem 6 (2 pts)

Implement shifty_shifts, which is a diff function that takes two strings. It returns the minimum number of characters that must be changed in the start word in order to transform it into the goal word. If the strings are not of equal length, the difference in lengths is added to the total.

Here are some examples:

>>> big_limit = 10
>>> shifty_shifts("nice", "rice", big_limit)    # Substitute: n -> r
1
>>> shifty_shifts("range", "rungs", big_limit)  # Substitute: a -> u, e -> s
2
>>> shifty_shifts("pill", "pillage", big_limit) # Don't substitute anything, length difference of 3.
3
>>> shifty_shifts("roses", "arose", big_limit)  # Substitute: r -> a, o -> r, s -> o, e -> s, s -> e
5
>>> shifty_shifts("rose", "hello", big_limit)   # Substitue: r->h, o->e, s->l, e->l, length difference of 1.
5

If the number of characters that must change is greater than limit, then shifty_shifts should return any number larger than limit and should minimize the amount of computation needed to do so.

These two calls to shifty_shifts should take about the same amount of time to evaluate:

>>> limit = 4
>>> shifty_shifts("roses", "arose", limit) > limit
True
>>> shifty_shifts("rosesabcdefghijklm", "arosenopqrstuvwxyz", limit) > limit
True

Important: You may not use while or for statements in your implementation. Use recursion.

Before writing any code, unlock the tests to verify your understanding of the question.

1.题目分析(注意红字):

1.长度不同,直接加上长度差

2.首字母不同,再加一

3.题眼(若递归次数超过limit,直接返回):

        1. 存在增加的数值大于限制(相当于回溯得到的值已经大于limit)

        2.不存在增加的数值大于限制,继续递归

2.答案代码:

        以下

def shifty_shifts(start, goal, limit):
    """A diff function for autocorrect that determines how many letters
    in START need to be substituted to create GOAL, then adds the difference in
    their lengths.
    """
    # BEGIN PROBLEM 6
    end_diff=0
    if len(start)!=len(goal):
        end_diff,min_len=abs(len(start)-len(goal)),min(len(start),len(goal))
        start=start[0:min_len]
        goal=goal[0:min_len]

    if len(start) ==0:
        return 0

    # 判断首位字母(可以代替if - else)
    end_diff+=int(start[0] != goal[0])

    # 存在增加的数值大于限制(相当于回溯得到的值已经大于limit)
    if end_diff>limit:
        return end_diff
    # 不存在增加的数值大于限制,就继续深度递归
    return end_diff+shifty_shifts(start[1:],goal[1:],limit-end_diff)

猜你喜欢

转载自blog.csdn.net/2301_79140115/article/details/134953427
61A
今日推荐