finish extra/gearing-up-for-destruction #22

Merged
seongbeom_park merged 4 commits from extra/gearing-up-for-destruction into main 2022-05-08 23:51:21 +00:00
2 changed files with 32 additions and 7 deletions
Showing only changes of commit a34a74167d - Show all commits

View File

@ -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.

View File

@ -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]),
]