From a34a74167def454a70b9e8b0ea3c79bc49e5f89a Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 9 May 2022 08:48:23 +0900 Subject: [PATCH] check all gears size are larger than 1 Submitting solution... Submission: SUCCESSFUL. Completed in: 1 hr, 19 mins, 28 secs. --- README.md | 3 ++ extra/gearing-up-for-destruction/solution.py | 36 ++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 657a2f8..73df309 100644 --- a/README.md +++ b/README.md @@ -96,3 +96,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM * Completed in: 37 mins, 56 secs. * Feedback > [elevator-maintenance] Sample output of the test cases in readme.txt for python language is not match with the return type. Changing first output from `0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0` to `["0.1", "1.1.1", "1.2", "1.2.1", "1.11", "2", "2.0", "2.0.0"]` and changing second output from `1.0,1.0.2,1.0.12,1.1.2,1.3.3` to `["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]` would be great. + +### gearing-up-for-destruction +* Completed in: 1 hr, 19 mins, 28 secs. diff --git a/extra/gearing-up-for-destruction/solution.py b/extra/gearing-up-for-destruction/solution.py index dfd9359..6f47464 100644 --- a/extra/gearing-up-for-destruction/solution.py +++ b/extra/gearing-up-for-destruction/solution.py @@ -1,19 +1,20 @@ def solution(pegs): + a, b = -1, -1 odd, even = sum_odd_even(pegs) if len(pegs) % 2 == 0: s = 2 * odd - 2 * even - pegs[-1] + pegs[0] - if 2*s < 3: - return [-1, -1] if s % 3 == 0: - return [2*s/3, 1] + a, b = 2*s/3, 1 else: - return [2*s, 3] + a, b = 2*s, 3 else: s = 2 * odd - 2 * even + pegs[-1] + pegs[0] - if 2*s < 1: - return [-1, -1] - return [2*s, 1] + a, b = 2*s, 1 + + if check_all_gear_size(a, b, pegs): + return [a, b] + return [-1, -1] def sum_odd_even(pegs): odd, even = 0, 0 @@ -24,11 +25,32 @@ def sum_odd_even(pegs): even += peg return odd, even +def check_all_gear_size(a, b, pegs): + if a < 2*b: + return False + + p = list(pegs) if b == 1 else [b*p for p in pegs] + + last_peg = p[0] + last_gear = a + for i in range(1, len(p)): + peg = p[i] + gap = peg - last_peg + if gap - last_gear < b: + return False + last_gear = gap - last_gear + last_peg = peg + + return True + tests = [ ([4, 30, 50], [12, 1]), ([4, 17, 50], [-1, -1]), ([1, 2], [-1, -1]), + ([1, 3], [-1, -1]), + ([1, 4], [2, 1]), + ([1, 5], [8, 3]), ([1, 2, 3], [-1, -1]), ]