I am doing the following challenge:
Commander Lambda’s space station is HUGE. And huge space stations take a LOT of power. Huge space stations with doomsday devices take even more power. To help meet the station’s power needs, Commander Lambda has installed solar panels on the station’s outer surface. But the station sits in the middle of a quasar quantum flux field, which wreaks havoc on the solar panels. You and your team of henchmen have been assigned to repair the solar panels, but you’d rather not take down all of the panels at once if you can help it, since they do help power the space station and all!You need to figure out which sets of panels in any given array you can take offline to repair while still maintaining the maximum amount of power output per array, and to do THAT, you’ll first need to figure out what the maximum output of each array actually is.
Write a function solution(xs) that takes a list of integers representing the power output levels of each panel in an array, and returns the maximum product of some non-empty subset of those numbers. So for example, if an array contained panels with power output levels of [2, -3, 1, 0, -5], then the maximum product would be found by taking the subset: xs[0] = 2, xs[1] = -3, xs[4] = -5, giving the product 2*(-3)*(-5) = 30. So solution([2,-3,1,0,-5]) will be “30”.
Each array of solar panels contains at least 1 and no more than 50 panels, and each panel will have a power output level whose absolute value is no greater than 1000 (some panels are malfunctioning so badly that they’re draining energy, but you know a trick with the panels’ wave stabilizer that lets you combine two negative-output panels to produce the positive output of the multiple of their power values). The final products may be very large, so give the solution as a string representation of the number.
And the rules for the challenge are follows:
Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function.
Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
Input/output operations are not allowed.
And this is my solution:
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
But my code is still failing.
If this question is off-topic then please tell me in the comments, and I will remove it. I am a beginner and, well, ‘on topic’ is a vague term