finish extra/ion-flux-relabeling #16

Merged
seongbeom_park merged 2 commits from extra/ion-flux-relabeling into main 2022-05-05 19:26:12 +00:00
2 changed files with 31 additions and 0 deletions
Showing only changes of commit 22762bbc16 - Show all commits

View File

@ -79,3 +79,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM
### queue-to-do
* Completed in: 9 hrs, 13 mins, 42 secs.
### ion-flux-relabeling
* Completed in: 9 hrs, 33 mins, 36 secs.

View File

@ -0,0 +1,28 @@
def solution(h, q):
return [converter(h, n) for n in q]
def converter(h, n):
if n == 2**h - 1:
return -1
return reduce(n)
def reduce(n):
b = bin(n)[2:]
l = len(b)
c = b.count('1')
if l == c: # 2**l - 1
return 2**(l+1) - 1
fold = 2**(l-1) - 1
m = n - fold
if m == fold: # 2 * (2**(l-1) - 1)
return reduce(m)
return reduce(m) + fold
tests = [
([3, [7, 3, 5, 1]], [-1, 7, 6, 3]),
([5, [19, 14, 28]], [21, 15, 29]),
]
for i, o in tests:
result = solution(*i)
print (i, result == o, result, o)