From 49aad951de6cf1a38d047995140f02b9602ec2c3 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 8 May 2022 03:27:34 +0900 Subject: [PATCH 1/4] request extra/power-hungry --- extra/power-hungry/constraints.txt | 21 +++++++++++++++ extra/power-hungry/readme.txt | 43 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 extra/power-hungry/constraints.txt create mode 100644 extra/power-hungry/readme.txt diff --git a/extra/power-hungry/constraints.txt b/extra/power-hungry/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/extra/power-hungry/constraints.txt @@ -0,0 +1,21 @@ +Java +==== +Your code will be compiled using standard Java 8. All tests will be run by calling the solution() method inside the Solution class + +Execution time is limited. + +Wildcard imports and some specific classes are restricted (e.g. java.lang.ClassLoader). You will receive an error when you verify your solution if you have used a blacklisted class. + +Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. + +Python +====== +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. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. diff --git a/extra/power-hungry/readme.txt b/extra/power-hungry/readme.txt new file mode 100644 index 0000000..22ed6a3 --- /dev/null +++ b/extra/power-hungry/readme.txt @@ -0,0 +1,43 @@ +Power Hungry +============ + +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. + +Languages +========= + +To provide a Python solution, edit solution.py +To provide a Java solution, edit Solution.java + +Test cases +========== +Your code should pass the following test cases. +Note that it may also be run against hidden test cases not shown here. + +-- Python cases -- +Input: +solution.solution([2, 0, 2, 2, 0]) +Output: + 8 + +Input: +solution.solution([-2, -3, 4, -5]) +Output: + 60 + +-- Java cases -- +Input: +Solution.solution({2, 0, 2, 2, 0}) +Output: + 8 + +Input: +Solution.solution({-2, -3, 4, -5}) +Output: + 60 + +Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder. -- 2.47.2 From 422abb63637631c30d9eae501e32ecf36e59bdd0 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 8 May 2022 03:37:37 +0900 Subject: [PATCH 2/4] first try Verifying solution... Test 1 passed! Test 2 passed! Test 3 passed! [Hidden] Test 4 failed [Hidden] Test 5 failed [Hidden] --- extra/power-hungry/solution.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 extra/power-hungry/solution.py diff --git a/extra/power-hungry/solution.py b/extra/power-hungry/solution.py new file mode 100644 index 0000000..cf349bf --- /dev/null +++ b/extra/power-hungry/solution.py @@ -0,0 +1,25 @@ +def solution(xs): + result = 1 + max_negative = 0 + for power in xs: + if power == 0: + continue + result *= power + if power < 0: + if max_negative == 0: + max_negative = power + else: + max_negative = max(max_negative, power) + if max_negative < 0 and result < 0: + result /= max_negative + return str(result) + +tests = [ + ([2, 0, 2, 2, 0], "8"), + ([-2, -3, 4, -5], "60"), + ([2, -3, 1, 0, -5], "30"), + ] + +for i, o in tests: + result = solution(i) + print (i, result == o, result, o) -- 2.47.2 From e5833ae10cefb4c3819a1de89e3c37ab4d97d9b7 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 8 May 2022 03:39:09 +0900 Subject: [PATCH 3/4] single negative panel Verifying solution... Test 1 passed! Test 2 passed! Test 3 passed! [Hidden] Test 4 passed! [Hidden] Test 5 failed [Hidden] --- extra/power-hungry/solution.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extra/power-hungry/solution.py b/extra/power-hungry/solution.py index cf349bf..f7f04e2 100644 --- a/extra/power-hungry/solution.py +++ b/extra/power-hungry/solution.py @@ -1,4 +1,6 @@ def solution(xs): + if len(xs) == 1: + return str(xs[0]) result = 1 max_negative = 0 for power in xs: -- 2.47.2 From 53116f6ff5fdf026ddc00632b9a8b80a9b73ffbe Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 8 May 2022 03:44:17 +0900 Subject: [PATCH 4/4] finish extra/power-hungry Submitting solution... Submission: SUCCESSFUL. Completed in: 16 mins, 49 secs. --- README.md | 3 +++ extra/power-hungry/solution.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index e4ff0f8..2efbb04 100644 --- a/README.md +++ b/README.md @@ -85,3 +85,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM ### en-route-salute * Completed in: 7 mins, 15 secs. + +### power-hungry +* Completed in: 16 mins, 49 secs. diff --git a/extra/power-hungry/solution.py b/extra/power-hungry/solution.py index f7f04e2..247d0bc 100644 --- a/extra/power-hungry/solution.py +++ b/extra/power-hungry/solution.py @@ -3,9 +3,12 @@ def solution(xs): return str(xs[0]) result = 1 max_negative = 0 + non_positive_only = True for power in xs: if power == 0: continue + if power > 0: + non_positive_only = False result *= power if power < 0: if max_negative == 0: @@ -14,6 +17,8 @@ def solution(xs): max_negative = max(max_negative, power) if max_negative < 0 and result < 0: result /= max_negative + if non_positive_only: + result = 0 return str(result) tests = [ -- 2.47.2