cs61a 2021 fall lab03

网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/lab/lab03/#q8-protected-secret
整体没有什么难度,每一道题目和他的标题很有联系

    from operator import add, mul
    from turtle import Turtle
    square = lambda x: x * x
    identity = lambda x: x
    triple = lambda x: 3 * x
    increment = lambda x: x + 1


    def unique_digits(n):
        """Return the number of unique digits in positive integer n.

        >>> unique_digits(8675309) # All are unique
        7
        >>> unique_digits(1313131) # 1 and 3
        2
        >>> unique_digits(13173131) # 1, 3, and 7
        3
        >>> unique_digits(10000) # 0 and 1
        2
        >>> unique_digits(101) # 0 and 1
        2
        >>> unique_digits(10) # 0 and 1
        2
        """
        "*** YOUR CODE HERE ***"
        number = 0
        ans = {}
        s = str(n)
        for i in s:
            if i not in ans:
                ans[i] = 1
                number += 1
            else :
                ans[i] += 1
        return number
        


    def has_digit(n, k):
        """Returns whether K is a digit in N.
        >>> has_digit(10, 1)
        True
        >>> has_digit(12, 7)
        False
        """
        "*** YOUR CODE HERE ***"
        ans = {}
        s = str(n)
        for i in s:
            if i not in ans:
                ans[i] = 1
            else :
                ans[i] += 1
        if k in ans:
            return True
        else:
            return False



    def ordered_digits(x):
        """Return True if the (base 10) digits of X>0 are in non-decreasing
        order, and False otherwise.

        >>> ordered_digits(5)
        True
        >>> ordered_digits(11)
        True
        >>> ordered_digits(127)
        True
        >>> ordered_digits(1357)
        True
        >>> ordered_digits(21)
        False
        >>> result = ordered_digits(1375) # Return, don't print
        >>> result
        False

        """
        "*** YOUR CODE HERE ***"
        s = str(x)
        for i in range(0,len(s)):
            if i == len(s) - 2:
                return True
            if int(s[i]) > int(s[i+1]):
                return False


    def get_k_run_starter(n, k):
        """
        >>> get_k_run_starter(123444345, 0) # example from description
        3
        >>> get_k_run_starter(123444345, 1)
        4
        >>> get_k_run_starter(123444345, 2)
        4
        >>> get_k_run_starter(123444345, 3)
        1
        >>> get_k_run_starter(123412341234, 1)
        1
        >>> get_k_run_starter(1234234534564567, 0)
        4
        >>> get_k_run_starter(1234234534564567, 1)
        3
        >>> get_k_run_starter(1234234534564567, 2)
        2
        """
        i = 0
        final = None
        index = -1
        n = str(n)
        while i <= k:
            while abs(index) + 1 <= len(n) and n[index] > n[index - 1]:
                index -= 1
            final = int(n[index])
            index -= 1
            i += 1
        return final


    def make_repeater(func, n):
        """Return the function that computes the nth application of func.

        >>> add_three = make_repeater(increment, 3)
        >>> add_three(5)
        8
        >>> make_repeater(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
        243
        >>> make_repeater(square, 2)(5) # square(square(5))
        625
        >>> make_repeater(square, 4)(5) # square(square(square(square(5))))
        152587890625
        >>> make_repeater(square, 0)(5) # Yes, it makes sense to apply the function zero times!
        5
        """
        "*** YOUR CODE HERE ***"
        def f(num):
            if func == square and n == 0:
                return num
            for i in range(n):
                num = func(num)
            return num
        return f


    def composer(func1, func2):
        """Return a function f, such that f(x) = func1(func2(x))."""
        def f(x):
            return func1(func2(x))
        return f


    def apply_twice(func):
        """ Return a function that applies func twice.

        func -- a function that takes one argument

        >>> apply_twice(square)(2)
        16
        """
        "*** YOUR CODE HERE ***"
        return make_repeater(func,2)


    def protected_secret(password, secret, num_attempts):
        """
        Returns a function which takes in a password and prints the SECRET if the password entered matches
        the PASSWORD given to protected_secret. Otherwise it prints "INCORRECT PASSWORD". After NUM_ATTEMPTS
        incorrect passwords are entered, the secret is locked and the function should print "SECRET LOCKED".

        >>> my_secret = protected_secret("correcthorsebatterystaple", "I love UCB", 2)
        >>> my_secret = my_secret("hax0r_1") # 2 attempts left
        INCORRECT PASSWORD
        >>> my_secret = my_secret("correcthorsebatterystaple")
        I love UCB
        >>> my_secret = my_secret("hax0r_2") # 1 attempt left
        INCORRECT PASSWORD
        >>> my_secret = my_secret("hax0r_3") # No attempts left
        SECRET LOCKED
        >>> my_secret = my_secret("correcthorsebatterystaple")
        SECRET LOCKED
        """
        def get_secret(password_attempt):
            if num_attempts != 0:
                if password_attempt == password:
                    print(secret)
                    return get_secret
                else:
                    print("INCORRECT PASSWORD")
                    num = num_attempts - 1
                    return protected_secret(password, secret, num)
            else:
                print("SECRET LOCKED")
                return get_secret
        return get_secret
posted @ 2022-03-16 18:47  天然气之子  阅读(332)  评论(0编辑  收藏  举报