From 94fbaef1daa8e556cb4e3b356cc8ee9cb4054eff Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 9 Jan 2022 14:40:34 +0900 Subject: [PATCH] finish level 2 --- level2/hey-i-already-did-that/constraints.txt | 21 +++++++ level2/hey-i-already-did-that/readme.txt | 52 ++++++++++++++++ level2/hey-i-already-did-that/score.txt | 1 + level2/hey-i-already-did-that/solution.py | 34 +++++++++++ .../constraints.txt | 21 +++++++ .../please-pass-the-coded-messages/readme.txt | 41 +++++++++++++ .../please-pass-the-coded-messages/score.txt | 1 + .../solution.py | 60 +++++++++++++++++++ 8 files changed, 231 insertions(+) create mode 100644 level2/hey-i-already-did-that/constraints.txt create mode 100644 level2/hey-i-already-did-that/readme.txt create mode 100644 level2/hey-i-already-did-that/score.txt create mode 100644 level2/hey-i-already-did-that/solution.py create mode 100644 level2/please-pass-the-coded-messages/constraints.txt create mode 100644 level2/please-pass-the-coded-messages/readme.txt create mode 100644 level2/please-pass-the-coded-messages/score.txt create mode 100644 level2/please-pass-the-coded-messages/solution.py diff --git a/level2/hey-i-already-did-that/constraints.txt b/level2/hey-i-already-did-that/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/level2/hey-i-already-did-that/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/level2/hey-i-already-did-that/readme.txt b/level2/hey-i-already-did-that/readme.txt new file mode 100644 index 0000000..ca714ef --- /dev/null +++ b/level2/hey-i-already-did-that/readme.txt @@ -0,0 +1,52 @@ +Hey, I Already Did That! +======================== + +Commander Lambda uses an automated algorithm to assign minions randomly to tasks, in order to keep minions on their toes. But you've noticed a flaw in the algorithm -- it eventually loops back on itself, so that instead of assigning new minions as it iterates, it gets stuck in a cycle of values so that the same minions end up doing the same tasks over and over again. You think proving this to Commander Lambda will help you make a case for your next promotion. + +You have worked out that the algorithm has the following process: + +1) Start with a random minion ID n, which is a nonnegative integer of length k in base b +2) Define x and y as integers of length k. x has the digits of n in descending order, and y has the digits of n in ascending order +3) Define z = x - y. Add leading zeros to z to maintain length k if necessary +4) Assign n = z to get the next minion ID, and go back to step 2 + +For example, given minion ID n = 1211, k = 4, b = 10, then x = 2111, y = 1112 and z = 2111 - 1112 = 0999. Then the next minion ID will be n = 0999 and the algorithm iterates again: x = 9990, y = 0999 and z = 9990 - 0999 = 8991, and so on. + +Depending on the values of n, k (derived from n), and b, at some point the algorithm reaches a cycle, such as by reaching a constant value. For example, starting with n = 210022, k = 6, b = 3, the algorithm will reach the cycle of values [210111, 122221, 102212] and it will stay in this cycle no matter how many times it continues iterating. Starting with n = 1211, the routine will reach the integer 6174, and since 7641 - 1467 is 6174, it will stay as that value no matter how many times it iterates. + +Given a minion ID as a string n representing a nonnegative integer of length k in base b, where 2 <= k <= 9 and 2 <= b <= 10, write a function solution(n, b) which returns the length of the ending cycle of the algorithm above starting with n. For instance, in the example above, solution(210022, 3) would return 3, since iterating on 102212 would return to 210111 when done in base 3. If the algorithm reaches a constant, such as 0, then the length is 1. + +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('210022', 3) +Output: + 3 + +Input: +Solution.solution('1211', 10) +Output: + 1 + +-- Python cases -- +Input: +solution.solution('1211', 10) +Output: + 1 + +Input: +solution.solution('210022', 3) +Output: + 3 + +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/level2/hey-i-already-did-that/score.txt b/level2/hey-i-already-did-that/score.txt new file mode 100644 index 0000000..29d6383 --- /dev/null +++ b/level2/hey-i-already-did-that/score.txt @@ -0,0 +1 @@ +100 diff --git a/level2/hey-i-already-did-that/solution.py b/level2/hey-i-already-did-that/solution.py new file mode 100644 index 0000000..928d781 --- /dev/null +++ b/level2/hey-i-already-did-that/solution.py @@ -0,0 +1,34 @@ +import numpy + +def solution(n, b): + k = len(n) + memo = {} + index = 0 + search = True + while search: + x, y, z = get_x_y_z(n, k, b) + if x in memo: + print(memo) + return index - memo[x] + memo[x] = index + n = z + index+=1 + +def get_x_y_z(n, k, b): + pre_x = ''.join(sorted(n, reverse=True)) + x = int(pre_x, b) + y = int(pre_x[::-1], b) + z = numpy.base_repr(x-y, base=b).rjust(k, '0') + return x, y, z + +tests = [ + ['1211', 10], + ['210022', 3], + ['01', 2], + ['10', 2], + ['111111111', 2], + ['999999999', 10], + ] + +for test in tests: + print(test, solution(test[0], test[1])) diff --git a/level2/please-pass-the-coded-messages/constraints.txt b/level2/please-pass-the-coded-messages/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/level2/please-pass-the-coded-messages/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/level2/please-pass-the-coded-messages/readme.txt b/level2/please-pass-the-coded-messages/readme.txt new file mode 100644 index 0000000..bd4d964 --- /dev/null +++ b/level2/please-pass-the-coded-messages/readme.txt @@ -0,0 +1,41 @@ +Please Pass the Coded Messages +============================== + +You need to pass a message to the bunny workers, but to avoid detection, the code you agreed to use is... obscure, to say the least. The bunnies are given food on standard-issue plates that are stamped with the numbers 0-9 for easier sorting, and you need to combine sets of plates to create the numbers in the code. The signal that a number is part of the code is that it is divisible by 3. You can do smaller numbers like 15 and 45 easily, but bigger numbers like 144 and 414 are a little trickier. Write a program to help yourself quickly create large numbers for use in the code, given a limited number of plates to work with. + +You have L, a list containing some digits (0 to 9). Write a function solution(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the solution. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once. + +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({3, 1, 4, 1}) +Output: + 4311 + +Input: +Solution.solution({3, 1, 4, 1, 5, 9}) +Output: + 94311 + +-- Python cases -- +Input: +solution.solution([3, 1, 4, 1]) +Output: + 4311 + +Input: +solution.solution([3, 1, 4, 1, 5, 9]) +Output: + 94311 + +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/level2/please-pass-the-coded-messages/score.txt b/level2/please-pass-the-coded-messages/score.txt new file mode 100644 index 0000000..e373ee6 --- /dev/null +++ b/level2/please-pass-the-coded-messages/score.txt @@ -0,0 +1 @@ +50 diff --git a/level2/please-pass-the-coded-messages/solution.py b/level2/please-pass-the-coded-messages/solution.py new file mode 100644 index 0000000..5bdd4ce --- /dev/null +++ b/level2/please-pass-the-coded-messages/solution.py @@ -0,0 +1,60 @@ +def solution(l): + remain = sum(l) % 3 + if remain == 0: + largest_number = get_largest_number(l) + return largest_number + + (category_0, category_1, category_2) = split_digits_by_remain_of_three(l) + if remain == 1: + category_1, category_2 = remove_one_or_two_digits(category_1, category_2) + else: + category_2, category_1 = remove_one_or_two_digits(category_2, category_1) + if (category_1 == False) or (category_2 == False): + return 0 + largest_number = get_largest_number(category_0 + category_1 + category_2) + return largest_number + +def split_digits_by_remain_of_three(nums): + result = ([], [], []) + for n in nums: + result[n%3].append(n) + return result + +def remove_one_or_two_digits(list1, list2): + if len(list1) >= 1: + return sorted(list1)[1:], list2 + if len(list2) >= 2: + return list1, sorted(list2)[2:] + return False, False + +def get_largest_number(nums): + if len(nums) == 0: + return 0 + return int(''.join([str(x) for x in sorted(nums, reverse=True)])) + +test_cases=[ + [3, 1, 4, 1], + [3, 1, 4, 1, 5, 9], + [5, 5], + [0, 0, 9,9,9,9,9,9,9,9,7], + [0, 0, 0,0,0,0], + [0, 0, 0,0,0,0, 1, 1, 1], + [1], + [1, 1], + [1, 2], + [1, 1, 1], + [], + [0], + [1, 2, 3], + [1, 2, 3, 4], + [1, 2, 3, 4, 5], + [3, 3, 3, 0, 1, 1, 2, 2], + [3, 3, 3, 0, 1, 2, 2], + [3, 3, 3, 0, 1, 1, 2], + [1, 0, 0, 0], + [2, 0, 0, 0], + [3, 0, 0, 0, 6], + [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] + ] +for test in test_cases: + print(test, solution(test))