
Week5 of CS61A
this week, I have 3 classes, 1 lab and 1 project class: 1. 11. Sequences 2. 12. Containers 3. 13. Data Abstraction lab: 4. lab 03 project: 5. cats
11. Sequences
knowledge of this lecture:
List, range, loop, slice and recursion
pay attention to list comprehensions and recursion
list comprehensions:

recursion:

the others are simple
the difficult point of lab3
def ten_pairs(n):
"""Return the number of ten-pairs within positive integer n.
>>> ten_pairs(7823952) # 7+3, 8+2, and 8+2
3
>>> ten_pairs(55055)
6
>>> ten_pairs(9641469) # 9+1, 6+4, 6+4, 4+6, 1+9, 4+6
6
>>> # ban iteration
>>> from construct_check import check
>>> check(SOURCE_FILE, 'ten_pairs', ['While', 'For'])
True
"""
"*** YOUR CODE HERE ***"
if n < 10:
return 0
else:
return ten_pairs(n//10) + count_digit(n//10, 10 - n%10)ten_pairs(55055)
├── count_digit(5505, 5) = 3
└── ten_pairs(5505)
├── count_digit(550, 5) = 2
└── ten_pairs(550)
├── count_digit(55, 10) = 0
└── ten_pairs(55)
├── count_digit(5, 5) = 1
└── ten_pairs(5)
└── 012. Containers
more deeper knowledge of list in python

and
tree recursion with string
2 examples


difficulty of disc04


the difficulty of cats
def minimum_mewtations(typed: str, source: str, limit: int) -> int:
"""A diff function for autocorrect that computes the edit distance from TYPED to SOURCE.
This function takes in a string TYPED, a string SOURCE, and a number LIMIT.
Arguments:
typed: a starting word
source: a string representing a desired goal word
limit: a number representing an upper bound on the number of edits
>>> big_limit = 10
>>> minimum_mewtations("cats", "scat", big_limit) # cats -> scats -> scat
2
>>> minimum_mewtations("purng", "purring", big_limit) # purng -> purrng -> purring
2
>>> minimum_mewtations("ckiteus", "kittens", big_limit) # ckiteus -> kiteus -> kitteus -> kittens
3
"""
if limit < 0: # Base cases should go here, you may add more base cases as needed.
# BEGIN
"*** YOUR CODE HERE ***"
return 1
# END
if typed == "" or source == "":
return len(typed) + len(source)
# Recursive cases should go below here
if typed[0] == source[0]: # Feel free to remove or add additional cases
# BEGIN
"*** YOUR CODE HERE ***"
return minimum_mewtations(typed[1:], source[1:], limit)
# END
else:
add = 1 + minimum_mewtations(typed, source[1:], limit - 1)
remove = 1 + minimum_mewtations(typed[1:], source, limit - 1)
substitute = 1 + minimum_mewtations(typed[1:], source[1:], limit - 1)
# BEGIN
"*** YOUR CODE HERE ***"
return min(add, remove, substitute)
# END################
# Phase 4 (EC) #
################
def memo(f):
"""A general memoization decorator."""
cache = {}
def memoized(*args):
immutable_args = deep_convert_to_tuple(args) # convert *args into a tuple representation
if immutable_args not in cache:
result = f(*immutable_args)
cache[immutable_args] = result
return result
return cache[immutable_args]
return memoized
def memo_diff(diff_function):
"""A memoization function."""
cache = {}
def memoized(typed, source, limit):
# BEGIN PROBLEM EC
"*** YOUR CODE HERE ***"
key = (typed, source, limit)
if key not in cache:
cache[key] = diff_function(typed, source, limit)
return cache[key]
# END PROBLEM EC
return memoized13. Data Abstraction
Learning dictionary of python

and revise recursion with list
to combine recursion and dictionary

data abstraction

to sum two points are significant by my thinking
- make a max times to limit recursion to recurse what protect function to never overflow and program break stack
- constructor and selector are key of great program