From 2533d1f2eee044512ee8b0435d831dbaae312d6e Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Thu, 5 May 2022 19:26:12 +0000 Subject: [PATCH] finish extra/ion-flux-relabeling --- README.md | 3 ++ extra/ion-flux-relabeling/constraints.txt | 21 ++++++++++ extra/ion-flux-relabeling/readme.txt | 50 +++++++++++++++++++++++ extra/ion-flux-relabeling/solution.py | 28 +++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 extra/ion-flux-relabeling/constraints.txt create mode 100644 extra/ion-flux-relabeling/readme.txt create mode 100644 extra/ion-flux-relabeling/solution.py diff --git a/README.md b/README.md index ca0aec3..e5abfe6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/extra/ion-flux-relabeling/constraints.txt b/extra/ion-flux-relabeling/constraints.txt new file mode 100644 index 0000000..393f89f --- /dev/null +++ b/extra/ion-flux-relabeling/constraints.txt @@ -0,0 +1,21 @@ +va +==== +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/ion-flux-relabeling/readme.txt b/extra/ion-flux-relabeling/readme.txt new file mode 100644 index 0000000..9886649 --- /dev/null +++ b/extra/ion-flux-relabeling/readme.txt @@ -0,0 +1,50 @@ +Ion Flux Relabeling +=================== + +Oh no! Commander Lambda's latest experiment to improve the efficiency of the LAMBCHOP doomsday device has backfired spectacularly. The Commander had been improving the structure of the ion flux converter tree, but something went terribly wrong and the flux chains exploded. Some of the ion flux converters survived the explosion intact, but others had their position labels blasted off. Commander Lambda is having her henchmen rebuild the ion flux converter tree by hand, but you think you can do it much more quickly -- quickly enough, perhaps, to earn a promotion! + +Flux chains require perfect binary trees, so Lambda's design arranged the ion flux converters to form one. To label them, Lambda performed a post-order traversal of the tree of converters and labeled each converter with the order of that converter in the traversal, starting at 1. For example, a tree of 7 converters would look like the following: + + 7 + 3 6 +1 2 4 5 + +Write a function solution(h, q) - where h is the height of the perfect tree of converters and q is a list of positive integers representing different flux converters - which returns a list of integers p where each element in p is the label of the converter that sits on top of the respective converter in q, or -1 if there is no such converter. For example, solution(3, [1, 4, 7]) would return the converters above the converters at indexes 1, 4, and 7 in a perfect binary tree of height 3, which is [3, 6, -1]. + +The domain of the integer h is 1 <= h <= 30, where h = 1 represents a perfect binary tree containing only the root, h = 2 represents a perfect binary tree with the root and two leaf nodes, h = 3 represents a perfect binary tree with the root, two internal nodes and four leaf nodes (like the example above), and so forth. The lists q and p contain at least one but no more than 10000 distinct integers, all of which will be between 1 and 2^h-1, inclusive. + +Languages +========= + +To provide a Java solution, edit Solution.java +To provide a Python solution, edit solution.py + +Test cases +========== +Your code should pass the following test cases. +Note that it may also be run against hidden test cases not shown here. + +-- Java cases -- +Input: +Solution.solution(5, {19, 14, 28}) +Output: + 21,15,29 + +Input: +Solution.solution(3, {7, 3, 5, 1}) +Output: + -1,7,6,3 + +-- Python cases -- +Input: +solution.solution(3, [7, 3, 5, 1]) +Output: + -1,7,6,3 + +Input: +solution.solution(5, [19, 14, 28]) +Output: + 21,15,29 + +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. + diff --git a/extra/ion-flux-relabeling/solution.py b/extra/ion-flux-relabeling/solution.py new file mode 100644 index 0000000..f9d6b89 --- /dev/null +++ b/extra/ion-flux-relabeling/solution.py @@ -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)