# Problem 3 # # Write a function called deepcopy_242. It is passed a parameter #
ID: 3836220 • Letter: #
Question
# Problem 3
#
# Write a function called deepcopy_242. It is passed a parameter
# x, which may be a list (possibly containing embedded lists), or
# a non-list such as an integer. The function should return a
# "deep copy" of x, as described during Monday's lecture. Your solution
# must be recursive and have no loops. You may
# NOT use the built-in Python deepcopy function. Here
# some examples of proper behavior of deepcopy_242.
#
# >>> a = 1
# >>> deepcopy_242(a)
# 1
# >>> x = [1, [2, 3], 4]
# >>> y = deepcopy_242(x)
# >>> y
# [1, [2, 3], 4]
# >>> x is y
# False
# >>> x == y
# True
# >>> x[0] = 10
# >>> x
# [10, [2, 3], 4]
# >>> y
# [1, [2, 3], 4]
# >>> x == y
# False
# >>> x[1][0] = 0
# >>> x[1]
# [0, 3]
# >>> y[1]
# [2, 3]
###############################################################
def deepcopy_242(x):
return x # replace
Explanation / Answer
def deepcopy_242(x):
return eval(repr(x))
The repr will take the value of x and keep it as it is which is the same as what deepcopy is doing when reading a value
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.