做功课,但没有得到正确的结果。
我正在尝试编写一个函数,该函数从包含列表的列表或元组或元组返回最大值。 面对难以确定如何精确地循环元组以及是否有列表查看它,并且当完成所有操作时,递归地找到最大值。请阅读我的意思。
def max_val(t):
""" t, tuple or list
Each element of t is either an int, a tuple, or a list
No tuple or list is empty
Returns the maximum int in t or (recursively) in an element of t """
# Your code here
例如,
•max_val((5,(1,2),[[1],[2]]))返回5。 •max_val((5,(1,2),[[1],[9]]))返回9.
到目前为止,我已尝试过max和sort t,但这并没有给出正确答案。
答案 0 :(得分:4)
(免责声明:正如你在问题中提到的那样,这是一项家庭作业,我强烈建议你先“自己”解决问题,然后“偷看”下面的解决方案(虽然有很多方法可以做到这一点)学习解决复杂问题的好方法是首先将其分解为较小的“块”,提出一些非常简单的输入和预期输出,然后实现自己的代码。使用Google搜索查找提示/提示。我在这里把自己的解决方案纯粹用于激发灵感。可能有更优雅/有效的方法来做到这一点。)
为了便于理解,我将其分解为两个步骤:
int
,则只返回包含int
个列表的列表。max
函数查找展平列表的最大整数。在尝试实现代码之前,我总是发现“可视化”一些非常简单的示例输入和预期输出是有益的。让我们想象一些场景。
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
6
(1, 2, 3, 4, 5, 6)
[1, 2, 3, 4, 5, 6]
6
[1, [2, [3, 4, 5, 6]]]
[1, 2, 3, 4, 5, 6]
6
(1, (2, (3, 4, 5, 6)))
[1, 2, 3, 4, 5, 6]
6
[1, (2, [3, 4, 5, 6])]
[1, 2, 3, 4, 5, 6]
6
(1, [2, (3, 4, 5, 6)])
[1, 2, 3, 4, 5, 6]
6
6
[6]
6
[]
[]
None
()
[]
None
这是我的代码(适用于上面的一些测试用例场景。根据需要进行调整以使其更加健壮)。
def deep_flatten(t):
# if t is an int, just return a list with that int in it. No recursions needed.
if isinstance(t, int):
return [t]
# if t is a list or tuple, do some recursions to deep-flatten it:
elif isinstance(t, (list, tuple)):
# initialise result
result = []
# define a recursive function
def update_result(t):
for item in t:
if isinstance(item,(list, tuple)):
update_result(item)
else:
result.append(item)
# mutate result by performing recursions
update_result(t)
# return it
return result
这是使用deep_flatten
辅助函数的最终代码。
def max_val(t):
""" t, tuple or list
Each element of t is either an int, a tuple, or a list
No tuple or list is empty
Returns the maximum int in t or (recursively) in an element of t """
# use our helper deep flattening function to output a flatten list
t_flatten = deep_flatten(t)
# empty tuple / list case
if len(t_flatten) == 0:
return None
# return the max element in a non-empty list / tuple input
return max(t_flatten)
针对方案进行一些测试。打印出变量以检查结果是否符合预期。
t1 = [1, 2, 3, 4, 5, 6]
t1_flatten = deep_flatten(t1) # -> [1, 2, 3, 4, 5, 6]
t1_max = max_val(t1) # -> 6
t2 = (1, 2, 3, 4, 5, 6)
t2_flatten = deep_flatten(t2) # -> [1, 2, 3, 4, 5, 6]
t2_max = max_val(t2) # -> 6
t3 = [1, [2, [3, 4, 5, 6]]]
t3_flatten = deep_flatten(t3) # -> [1, 2, 3, 4, 5, 6]
t3_max = max_val(t3) # -> 6
t4 = (1, (2, (3, 4, 5, 6)))
t4_flatten = deep_flatten(t4) # -> [1, 2, 3, 4, 5, 6]
t4_max = max_val(t4) # -> 6
t5 = [1, (2, [3, 4, 5, 6])]
t5_flatten = deep_flatten(t5) # -> [1, 2, 3, 4, 5, 6]
t5_max = max_val(t5) # -> 6
t6 = (1, [2, (3, 4, 5, 6)])
t6_flatten = deep_flatten(t6) # -> [1, 2, 3, 4, 5, 6]
t6_max = max_val(t6) # -> 6
t7 = 6
t7_flatten = deep_flatten(t7) # -> [6]
t7_max = max_val(t7) # -> 6
t8 = []
t8_flatten = deep_flatten(t8) # -> []
t8_max = max_val(t8) # -> None
t9 = ()
t9_flatten = deep_flatten(t9) # -> []
t9_max = max_val(t9) # -> None
print( max_val((5, (1,2), [[1],[2]])) ) # -> 5
print( max_val((5, (1,2), [[1],[9]])) ) # -> 9
答案 1 :(得分:1)
我不打算为你做功课......但生病给你一个暗示
def max_val(t):
""" t, tuple or list
Each element of t is either an int, a tuple, or a list
No tuple or list is empty
Returns the maximum int in t or (recursively) in an element of t """
current_max = float("-inf")
for item in t:
if isinstance(item,(list,tuple)):
#do one thing here
else:
#do a different thing here
return current_max
答案 2 :(得分:0)
def max_val(t):
weird_list = t
weird_string = str(weird_list)
weird_string = weird_string.replace('[','').replace(']','').replace(' ','').replace('(','').replace(')','')
nice_list = list(map(int, weird_string.split(',')))
max_number = max(nice_list)
return max_number
一种方法是首先将元组转换为字符串。
答案 3 :(得分:0)
关于这个问题的主要想法是递归思考:
def max_val(t):
""" t, tuple
Each element of t is either an int, a tuple, or a list
No tuple or list is empty
Returns the maximum int in t or (recursively) in an element of t """
# Your code here
result = 0
for e in t:
try:
if e > result:
result = e
except:
if max_val(e) > result:
result = max_val(e)
return result
print(max_val((5, (1, 2), [[1], [2]])))
print(max_val((5, (1, 2), [[1], [9]])))
将分别返回5和9。