其他分享
首页 > 其他分享> > 2021 fall cs61a hw09

2021 fall cs61a hw09

作者:互联网

网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/hw/hw09/#q3-cs-classes
problem1:了解正则表达式规则就可以写了
problem2:
匹配[IVXLCDM],不能匹配出现在其他单词里面的用 \b,出现至少一次用+

        return re.findall(r"\b[IVXLCDM]\b+", text)

problem3:

        return bool(re.search(r'cs|CS ?\d+[(a|A)|(b|B)|(c|C)]{1}', post))

probelm4:

        return re.findall(r'([01]?\d|2[0-3]):[0-5][0-9](AM)?', text)

problem5:

        return re.findall(r'(?:\()?(\d{3})(?:\)?)(?: |-)?\d{3}(?: |-)?\d{4}\b', text)

    def most_common_code(text):
        """
        Takes in an input string which contains at least one phone number (and
        may contain more) and returns the most common area code among all phone
        numbers in the input. If there are multiple area codes with the same
        frequency, return the first one that appears in the input text.

        >>> most_common_code('(501) 333 3333')
        '501'
        >>> input_text = '''
        ... (123) 000 1234 and 12454, 098-123-0941, 123 451 0951 and 410-501-3021 has
        ... some phone numbers. '''
        >>> most_common_code(input_text)
        '123'
        """
        "*** YOUR CODE HERE ***"
        area_codes_list = area_codes(text)
        cnts = [area_codes_list.count(e) for e in area_codes_list]
        maxnum = cnts.index(max(cnts))
        return area_codes_list[maxnum]

标签:codes,return,area,text,hw09,cs61a,code,2021,input
来源: https://www.cnblogs.com/echoT/p/16194401.html