CS61A Project2 Cat 不完全吐槽

Project2 Cat

声明:

本人是初学者,如果有误欢迎指正.本意是想记录自己的学习,并且稍微提炼翻译一下project的要点,写一下自己遇到的问题.

规则:

一个检测打字速度相关的程序.

代码编写:

01:

    "*** YOUR CODE HERE ***"

    for i in range(len(paragraphs)):

        if i <= k and not select(paragraphs[i]):

            k += 1

    if k >= len(paragraphs):

        return ''

    else:

        return paragraphs[k]

主要是注意当k指向的地址不含有元素时,最好单独讨论.

For循环内的逻辑是:i遍历,k增长,如果select返回的值一直都是true,那么k最后就和最大的i相等.

02:

1. 标点符号,大小写不影响判断结果

2. 与任意一个相符就行

"*** YOUR CODE HERE ***"

def helper(x):

    for i in split(remove_punctuation(lower(x))):

        for j in subject:

            if i == j:

                return True

    return False

return helper

把给的三个函数全套上,记得split要在最外层,因为lower和remove函数的形参都是一个字符串,split会把一个字符串分成多个.

03:

1. 一个单词算一个元素,元素之间用空格区分,只要有一个字符不相同就算整个元素错误,为精准度贡献的值为0.0

2. 符号也算在单词中,大小写敏感

(实际上直接使用split就包含了所有情况了,不要用lower和remove函数即可)

    "*** YOUR CODE HERE ***"

    i , j = 0 , 0

    sum = 0

    while i < len(typed_words) and j < len(source_words):

        if typed_words[i] == source_words[j]:

            sum += 100.0

        i += 1

        j += 1

    if len(typed_words) == 0 and len(source_words) == 0:

        return 100.0

    elif len(typed_words) == 0:

        return 0.0

    else:

        return sum / len(typed_words)

# END PROBLEM 3

不知道有没有优化的方法.单独考虑了两者长度都为零和前者长度为零的情况,,防止除零的发生.当然仅是针对我这个算法(加起来再除长度),不过暂时没有去想别的更好的方法了.

一一对应是关键.

04:

    # BEGIN PROBLEM 4

    "*** YOUR CODE HERE ***"

    speed = len(typed)/5*60/elapsed

    return speed

# END PROBLEM 4

 

没什么好说的,注意一下计算公式就行

05:

1. 如果在word_list中有跟typed_word相同的单词,那么直接返回这个单词(易忽略)

2. 如果word_list中有多个单词有着一样的最小difference,那么返回最左边的那个

    "*** YOUR CODE HERE ***"

    for x in word_list:

        if typed_word == x:

            return x

    min_diff , j = diff_function(typed_word , word_list[0] , limit) , 0

    record = 0

    i = 0

    while i < len(word_list):

        temp = diff_function(typed_word , word_list[i] , limit)

        if min_diff > temp:

            min_diff = temp

            record = i

        i += 1

    if min_diff > limit:

        return typed_word

    else:

        return word_list[record]

# END PROBLEM 5

对max,min的语法还比较生疏,所以索性用循环解决了.算是常规题目,写过就还好,如果第一次遇见类似的题目可能会卡住.

06:

    # BEGIN PROBLEM 6

    a , b = len(typed) , len(source)

    def helper1(x , limit , total):

        if x >= min(a , b):

            total += abs(a - b)

            return total

        elif typed[x] != source[x]:

            total += 1

        if total > limit:

            return total  

        return helper1(x + 1 , limit , total)

    return helper1(0 , limit , 0)

    assert False, 'Remove this line'

# END PROBLEM 6

使用recursion的一集,实话说目前为止我感觉recursion的优势就是简洁+装逼,写起来是真的难.有点数列递推的味道.这题要说什么诀窍我也没有,就凭感觉来写的.

07:

这是我目前为止唯一卡住(一卡就是六七小时+,我去**的)and一点思路都没有的题目,也是我千辛万苦解决之后最想问候出题人的一题.要想解决这题,你需要:

1. 熟练使用递归函数

2. 熟悉动态规划(这道题叫做”编辑距离”,具体可以b站找视频看)

3. 在递归函数中加入限制次数,能够及时跳出函数.

代码如下:

    if not typed: # Base cases should go here, you may add more base cases as needed.

        # BEGIN

        "*** YOUR CODE HERE ***"

        return len(source)

        # END

    # Recursive cases should go below here       

    if not source: # Feel free to remove or add additional cases

        # BEGIN

        "*** YOUR CODE HERE ***"

        return len(typed)

        # END

    if limit < 0:

        return limit + 1

    if typed[-1] == source[-1]:

        return minimum_mewtations(typed[:-1] , source[:-1] , limit)

    else:

        add = minimum_mewtations(typed[:-1] , source , limit - 1) + 1 # Fill in these lines

        remove = minimum_mewtations(typed , source[:-1] , limit - 1) + 1

        substitute = minimum_mewtations(typed[:-1] , source[:-1] , limit - 1) + 1

        # BEGIN

        "*** YOUR CODE HERE ***"

        return min(add , remove , substitute , limit + 1)

        # END

如果你觉得看不懂,不是你的问题,你需要先学”编辑距离”(或者叫莱文斯坦距离),一般是使用for循环解决的,然后你再根据for循环修改成递推函数的形式,就是我们的最终代码了.

如果你卡了很久,没关系,这里有个人卡了一天,我不说是谁.

(optional跳过)

08:


featured image


需要注意的是,此函数传入的typed和source不再是一个字符串,而是由多个字符串组成的数组.

09:

 

featured image

需要去了解一下创建一个二维数组的方法.其他的理解题意即可

10:

 

featured image

需要熟悉二维数组的创建,记录最小值并使用的操作,其余的理解好题意就行.

 

总结:

相比hog,难度感觉要上升一些,不过上升的方面是要用比较多不熟的知识点(特别是递归函数和对数组的操作),作为一个初学者,第一次求助了ai,发现ai真的对学习很有帮助,在此强烈推荐各位使用.

附一份我的求助ai记录:

featured image
featured image
featured image

 

featured image
featured image
featured image
featured image
featured image
featured image
featured image
featured image

可以看到,整个提问的过程还是比较长的,一方面ai解释得十分详细易懂,另一方面我自己也就着一个又一个的问题提问.将整个知识点搞明白.


转载请注明出处
原文链接: /posts/beAbyo9y

featured image
featured image
featured image