From ef27d7f8e44cbb72f9f05d10af3fef9a81e4ccfa Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sat, 30 Apr 2022 02:17:31 +0900 Subject: [PATCH 1/3] request extra/find-the-access-codes --- extra/find-the-access-codes/constraints.txt | 21 ++++++++++ extra/find-the-access-codes/readme.txt | 45 +++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 extra/find-the-access-codes/constraints.txt create mode 100644 extra/find-the-access-codes/readme.txt diff --git a/extra/find-the-access-codes/constraints.txt b/extra/find-the-access-codes/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/extra/find-the-access-codes/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/find-the-access-codes/readme.txt b/extra/find-the-access-codes/readme.txt new file mode 100644 index 0000000..144dff2 --- /dev/null +++ b/extra/find-the-access-codes/readme.txt @@ -0,0 +1,45 @@ +Find the Access Codes +===================== + +In order to destroy Commander Lambda's LAMBCHOP doomsday device, you'll need access to it. But the only door leading to the LAMBCHOP chamber is secured with a unique lock system whose number of passcodes changes daily. Commander Lambda gets a report every day that includes the locks' access codes, but only the Commander knows how to figure out which of several lists contains the access codes. You need to find a way to determine which list contains the access codes once you're ready to go in. + +Fortunately, now that you're Commander Lambda's personal assistant, Lambda has confided to you that all the access codes are "lucky triples" in order to make it easier to find them in the lists. A "lucky triple" is a tuple (x, y, z) where x divides y and y divides z, such as (1, 2, 4). With that information, you can figure out which list contains the number of access codes that matches the number of locks on the door when you're ready to go in (for example, if there's 5 passcodes, you'd need to find a list with 5 "lucky triple" access codes). + +Write a function solution(l) that takes a list of positive integers l and counts the number of "lucky triples" of (li, lj, lk) where the list indices meet the requirement i < j < k. The length of l is between 2 and 2000 inclusive. The elements of l are between 1 and 999999 inclusive. The solution fits within a signed 32-bit integer. Some of the lists are purposely generated without any access codes to throw off spies, so if no triples are found, return 0. + +For example, [1, 2, 3, 4, 5, 6] has the triples: [1, 2, 4], [1, 2, 6], [1, 3, 6], making the solution 3 total. + +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([1, 1, 1]) +Output: + 1 + +Input: +Solution.solution([1, 2, 3, 4, 5, 6]) +Output: + 3 + +-- Python cases -- +Input: +solution.solution([1, 2, 3, 4, 5, 6]) +Output: + 3 + +Input: +solution.solution([1, 1, 1]) +Output: + 1 + +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 a324af524fcfb8915441391a44e424cd9e49b0ea Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sat, 30 Apr 2022 03:10:53 +0900 Subject: [PATCH 2/3] initial implementation Verifying solution... Test 1 passed! Test 2 passed! Test 3 failed [Hidden] Test 4 failed [Hidden] Test 5 failed [Hidden] --- extra/find-the-access-codes/solution.py | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 extra/find-the-access-codes/solution.py diff --git a/extra/find-the-access-codes/solution.py b/extra/find-the-access-codes/solution.py new file mode 100644 index 0000000..3bcc114 --- /dev/null +++ b/extra/find-the-access-codes/solution.py @@ -0,0 +1,42 @@ +def solution(l): + edges = {n : {'count': 1, 'multiple': set(), 'divisor': set()} for n in set(l)} + for i in range(len(l)): + x = l[i] + if edges[x]['count'] > 1: + continue + for j in range(i+1, len(l)): + y = l[j] + if x == y: + edges[x]['count'] += 1 + elif x > y and x % y == 0: + edges[x]['divisor'].add(y) + edges[y]['multiple'].add(x) + elif y > x and y % x == 0: + edges[x]['multiple'].add(y) + edges[y]['divisor'].add(x) + + result = 0 + for x in edges: + # 1. (d, x, m) + result += len(edges[x]['divisor']) * len(edges[x]['multiple']) + # 2. (d, x, x) (x, x, m) + if edges[x]['count'] >= 2: + result += len(edges[x]['divisor']) + len(edges[x]['multiple']) + # 3. (x, x, x) + if edges[x]['count'] >= 3: + result += 1 + + return result + +tests = [ + ([1, 1, 1], 1), + ([1, 2, 3, 4, 5, 6], 3), + ([2, 3, 4], 0), + ([1, 1, 2, 3, 4, 5, 6], 8), + ([3 for i in range(2000)], 1), + ([i for i in range(1, 2000)], 40777), + ] + +for i, o in tests: + result = solution(i) + print(i, result == o, result, o) -- 2.47.2 From 0d75806e4a5369fdad976468a6caf638d11aa288 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sat, 30 Apr 2022 03:24:09 +0900 Subject: [PATCH 3/3] finish extra/find-the-access-codes Submitting solution... Submission: SUCCESSFUL. Completed in: 1 hr, 6 mins, 52 secs. --- README.md | 8 ++++-- extra/find-the-access-codes/solution.py | 33 ++++++------------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 513c313..d76d93a 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ ## Encrypted message CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= -## Extra +## Extra Level * Requested problems after level 5 clear ### dodge-the-laser @@ -68,5 +68,9 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM * Reference * [Maximum Matching in General Graph](https://www.secmem.org/blog/2020/04/18/Blossom/) -### extra/prepare-the-bunnies-escape +### prepare-the-bunnies-escape * Completed in: 3 hrs, 17 mins, 20 secs. + +### find-the-access-codes +* Completed in: 1 hr, 6 mins, 52 secs. + diff --git a/extra/find-the-access-codes/solution.py b/extra/find-the-access-codes/solution.py index 3bcc114..cbdb8d3 100644 --- a/extra/find-the-access-codes/solution.py +++ b/extra/find-the-access-codes/solution.py @@ -1,30 +1,15 @@ def solution(l): - edges = {n : {'count': 1, 'multiple': set(), 'divisor': set()} for n in set(l)} + nodes = {i: [] for i in range(len(l))} for i in range(len(l)): - x = l[i] - if edges[x]['count'] > 1: - continue for j in range(i+1, len(l)): - y = l[j] - if x == y: - edges[x]['count'] += 1 - elif x > y and x % y == 0: - edges[x]['divisor'].add(y) - edges[y]['multiple'].add(x) - elif y > x and y % x == 0: - edges[x]['multiple'].add(y) - edges[y]['divisor'].add(x) + x, y = l[i], l[j] + if y % x == 0: + nodes[i] += [j] result = 0 - for x in edges: - # 1. (d, x, m) - result += len(edges[x]['divisor']) * len(edges[x]['multiple']) - # 2. (d, x, x) (x, x, m) - if edges[x]['count'] >= 2: - result += len(edges[x]['divisor']) + len(edges[x]['multiple']) - # 3. (x, x, x) - if edges[x]['count'] >= 3: - result += 1 + for i in nodes: + for j in nodes[i]: + result += len(nodes[j]) return result @@ -32,9 +17,7 @@ tests = [ ([1, 1, 1], 1), ([1, 2, 3, 4, 5, 6], 3), ([2, 3, 4], 0), - ([1, 1, 2, 3, 4, 5, 6], 8), - ([3 for i in range(2000)], 1), - ([i for i in range(1, 2000)], 40777), + ([1, 1, 2, 3, 4, 5, 6], 11), ] for i, o in tests: -- 2.47.2