提问人:umar mnaq 提问时间:11/11/2023 更新时间:11/11/2023 访问量:14
最大可能的子集质询失败
Largest possible subset challenge failing
问:
我正在做以下挑战:
Lambda指挥官的空间站很大。巨大的空间站需要大量的电力。带有世界末日装置的巨大空间站需要更多的能量。为了帮助满足空间站的电力需求,Commander Lambda在空间站的外表面安装了太阳能电池板。但该站位于类星体量子通量场的中间,对太阳能电池板造成了严重破坏。你和你的追随者团队被指派修理太阳能电池板,但如果你能帮忙,你宁愿不要一次拆除所有的电池板,因为它们确实有助于为空间站和所有东西供电!您需要弄清楚任何给定阵列中的哪些面板可以脱机进行维修,同时仍保持每个阵列的最大功率输出,为此,您首先需要弄清楚每个阵列的最大输出实际是多少。
编写一个函数 solution(xs),该函数采用表示数组中每个面板的功率输出电平的整数列表,并返回这些数字的某个非空子集的最大乘积。因此,例如,如果一个数组包含功率输出电平为 [2, -3, 1, 0, -5] 的面板,则通过取子集来求出最大乘积:xs[0] = 2, xs[1] = -3, xs[4] = -5,得到乘积 2*(-3)*(-5) = 30。所以 solution([2,-3,1,0,-5]) 将为 “30”。
每个太阳能电池板阵列包含至少 1 个且不超过 50 个电池板,每个电池板的功率输出水平绝对值不大于 1000(有些电池板故障严重,以至于它们正在消耗能量,但你知道电池板的波浪稳定器的一个技巧,它可以让您将两个负输出电池板组合在一起,以产生其功率值倍数的正输出)。最终产品可能非常大,因此请将解决方案作为数字的字符串表示形式给出。
挑战的规则如下:
您的代码将在 Python 2.7.13 沙盒中运行。所有测试都将通过调用 solution() 函数来运行。
支持标准库,但 bz2、crypt、fcntl、mmap、pwd、pyexpat、select、signal、termios、thread、time、unicodedata、zipimport、zlib 除外。
不允许输入/输出操作。
这是我的解决方案:
def solution(xs):
# Define function multiply list together
def get_product(numbers):
total = 1
for i in numbers:
total *= i
return total
# Get list of combinations
def get_possible_combinations(parent_list):
def generate_combinations(subset, index, current_combination, result):
if index == len(subset):
result.append(current_combination)
return
generate_combinations(subset, index + 1, current_combination + [subset[index]], result)
generate_combinations(subset, index + 1, current_combination, result)
result = []
generate_combinations(parent_list, 0, [], result)
return result
max_reached = 0
possible_combinations = get_possible_combinations(xs)
# Loop over all possible combinations
for combination in possible_combinations:
product = get_product(combination)
if product > max_reached:
max_reached = product
return max_reached
但我的代码仍然失败。
如果这个问题偏离主题,请在评论中告诉我,我会删除它。我是一个初学者,嗯,“主题”是一个模糊的术语
答: 暂无答案
评论