summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthieu <matthieu@developershouse.xyz>2021-10-09 14:28:09 +0400
committerMatthieu <matthieu@developershouse.xyz>2021-10-09 14:28:09 +0400
commit231c5f2d7070dde28c6dec14eb84a213e7c544f6 (patch)
tree53771cf40de7c4a4e4e529f5262331d4221a2a58
parentbba24ba988eb2dd12d16d598532ad8f869968b22 (diff)
add gearing up for destruction
-rw-r--r--gearing-up-for-destruction/constraints.txt21
-rw-r--r--gearing-up-for-destruction/matthieu/solution.py62
-rw-r--r--gearing-up-for-destruction/readme.txt48
-rw-r--r--gearing-up-for-destruction/solution.py0
4 files changed, 131 insertions, 0 deletions
diff --git a/gearing-up-for-destruction/constraints.txt b/gearing-up-for-destruction/constraints.txt
new file mode 100644
index 0000000..f201987
--- /dev/null
+++ b/gearing-up-for-destruction/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/gearing-up-for-destruction/matthieu/solution.py b/gearing-up-for-destruction/matthieu/solution.py
new file mode 100644
index 0000000..6d4eee3
--- /dev/null
+++ b/gearing-up-for-destruction/matthieu/solution.py
@@ -0,0 +1,62 @@
+# coding=utf-8
+from fractions import Fraction
+
+def solution(pegs):
+ # This function is based on the formulat I figured out on paper
+ # using what I could find online about gears, I used the
+ # (number of gears entraining one other) / (number of gears entraned by one other)
+ # Using the distances, I can express the sizes of all gears from the first one
+ # and then, using the formula, I can find x
+
+ # The developed formula shows that the signs + / - are present half of the time
+ # However, the x present in the last part changes if the number is event/pair
+ # (because of the signs, when n is pair, it adds up to the one present in the other branch
+ # of the equation, if n is impair, it removes 2x from the single x on the other branch, resulting
+ # in a negative x in the other branch
+
+ sm = 0
+ # Avoid .append by creating a sized array
+ distances = [pegs[0]] + [0] * (len(pegs) - 1)
+
+ # We calculate the points between the pegs, (except the first one)
+ for i, v in enumerate(pegs):
+ if i != 0:
+ distances[i] = pegs[i] - pegs[i - 1]
+
+ # Derived from the formula
+ for i, v in enumerate(distances[1:]):
+ sm += (-1) ** (i) * (2 * v)
+
+ denom = 1
+
+ # If the number of pegs is pair, we divide by 3
+ if len(pegs) % 2 == 0:
+ denom = 3
+
+ # Avoid .append by creating a sized array
+ sizes = [sm / denom] + [0] * (len(pegs) - 1)
+
+ # calculate all the gear sizes
+ for i, v in enumerate(distances):
+ if v == distances[0]:
+ continue
+
+ size = v - sizes[i - 1]
+ sizes[i] = size
+
+ # all gears must be >=
+ if size < 1:
+ return [-1, -1]
+
+ # used to simplify the fraction
+ f = Fraction(sm, denom)
+
+ # the first gear must be >= 1
+ if f < 1:
+ return [-1, -1]
+
+ return [f.numerator, f.denominator]
+
+
+assert solution([4, 17, 50]) == [-1, -1], "invalid"
+assert solution([4, 30, 50]) == [12, 1], "invalid"
diff --git a/gearing-up-for-destruction/readme.txt b/gearing-up-for-destruction/readme.txt
new file mode 100644
index 0000000..1f57c1d
--- /dev/null
+++ b/gearing-up-for-destruction/readme.txt
@@ -0,0 +1,48 @@
+Gearing Up for Destruction
+==========================
+
+As Commander Lambda's personal assistant, you've been assigned the task of configuring the LAMBCHOP doomsday device's axial orientation gears. It should be pretty simple -- just add gears to create the appropriate rotation ratio. But the problem is, due to the layout of the LAMBCHOP and the complicated system of beams and pipes supporting it, the pegs that will support the gears are fixed in place.
+
+The LAMBCHOP's engineers have given you lists identifying the placement of groups of pegs along various support beams. You need to place a gear on each peg (otherwise the gears will collide with unoccupied pegs). The engineers have plenty of gears in all different sizes stocked up, so you can choose gears of any size, from a radius of 1 on up. Your goal is to build a system where the last gear rotates at twice the rate (in revolutions per minute, or rpm) of the first gear, no matter the direction. Each gear (except the last) touches and turns the gear on the next peg to the right.
+
+Given a list of distinct positive integers named pegs representing the location of each peg along the support beam, write a function solution(pegs) which, if there is a solution, returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form in order to achieve the goal above, such that radius = a/b. The ratio a/b should be greater than or equal to 1. Not all support configurations will necessarily be capable of creating the proper rotation ratio, so if the task is impossible, the function solution(pegs) should return the list [-1, -1].
+
+For example, if the pegs are placed at [4, 30, 50], then the first gear could have a radius of 12, the second gear could have a radius of 14, and the last one a radius of 6. Thus, the last gear would rotate twice as fast as the first one. In this case, pegs would be [4, 30, 50] and solution(pegs) should return [12, 1].
+
+The list pegs will be given sorted in ascending order and will contain at least 2 and no more than 20 distinct positive integers, all between 1 and 10000 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({4, 17, 50})
+Output:
+ -1,-1
+
+Input:
+Solution.solution({4, 30, 50})
+Output:
+ 12,1
+
+-- Python cases --
+Input:
+solution.solution([4, 30, 50])
+Output:
+ 12,1
+
+Input:
+solution.solution([4, 17, 50])
+Output:
+ -1,-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.
+ \ No newline at end of file
diff --git a/gearing-up-for-destruction/solution.py b/gearing-up-for-destruction/solution.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gearing-up-for-destruction/solution.py