Reputation: 135
I'm completing a google foobar challenge, but for the life of me, I can't pass the 4th test case, here is the challenge:
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 answer(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 answer([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 answer as a string representation of the number.
Here are some given test cases:
Inputs:
(int list) xs = [2, 0, 2, 2, 0]
Output:
(string) "8"
Inputs:
(int list) xs = [-2, -3, 4, -5]
Output:
(string) "60"
And here is my code:
def product_of_values(lst):
product = 1
if len(lst) > 0:
for x in lst:
product *= x
return product
def answer(xs):
# two seperate list for positive and negative values
positive_list = [x for x in xs if x > 0]
negative_list = [x for x in xs if x < 0]
pos_product = product_of_values(pos_list)
# multiplication of an even number of negatives == positive value
if len(negative_list) % 2 == 0:
negative_product = product_of_values(negative_list)
# if length of negative_list is odd, pop value closest to zero
else:
neg_list.remove(max(neg_list))
neg_product = product_of_values(neg_list)
# If there is only one negative value in the negative_list, return 0.
if len(pos_list) < 1 and len(neg_list) <= 1:
return '0'
else:
return str(neg_product * pos_product)
am I missing something obvious?
Upvotes: 0
Views: 5158
Reputation: 11
@edi_allen I am also facing the same problem. I have written the code in java, visible test cases are passing in my compiler, but failing at foobar. How did you overcome this? Below is my code:
public static String solution(int[] xs) {
if(xs.length < 1 || xs.length > 50) {
return "0";
}
Arrays.parallelSort(xs);
for(int i = 1; i < xs.length ; i++) {
if(xs[i] < 0 && xs[i-1] < 0) {
xs[i-1] = Math.abs(xs[i-1]);
xs[i] = Math.abs(xs[i]);
}
}
BigInteger prod = null;
for(int i = 0; i < xs.length ; i++) {
if(Math.abs(xs[i]) > 1000) {
return "0";
}
else if(xs[i] <= 0) {
continue;
}
else if(prod == null){
prod = new BigInteger(String.valueOf(xs[i]));
}
else {
prod = prod.multiply(new BigInteger(String.valueOf(xs[i])));
}
}
if(prod == null) {
return "0";
}
return prod.toString();
}
Upvotes: 1
Reputation: 43
Maybe its the time complexity that is problem here.
For the product try this -
from operator import mul
reduce(mul, list, 1)
Upvotes: 0