Merge branch 'level3/doomsday-fuel' into 'main'

finish level3/doomsday-fuel

See merge request seongbeom_park/google_foobar_challenge!1
This commit is contained in:
Seongbeom Park 2022-01-11 14:46:40 +00:00
commit c6406fd934
4 changed files with 188 additions and 0 deletions

View File

@ -1,2 +1,7 @@
# Google Foobar Challenge # Google Foobar Challenge
## Level3
### doomsday-fuel
* Reference
* L26.6 Absorption Probabilities, MIT OpenCourseWare, https://www.youtube.com/watch?v=vEsUsaK1HBk

View File

@ -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.

View File

@ -0,0 +1,65 @@
Doomsday Fuel
=============
Making fuel for the LAMBCHOP's reactor core is a tricky process because of the exotic matter involved. It starts as raw ore, then during processing, begins randomly changing between forms, eventually reaching a stable form. There may be multiple stable forms that a sample could ultimately reach, not all of which are useful as fuel.
Commander Lambda has tasked you to help the scientists increase fuel creation efficiency by predicting the end state of a given ore sample. You have carefully studied the different structures that the ore can take and which transitions it undergoes. It appears that, while random, the probability of each structure transforming is fixed. That is, each time the ore is in 1 state, it has the same probabilities of entering the next state (which might be the same state). You have recorded the observed transitions in a matrix. The others in the lab have hypothesized more exotic forms that the ore can become, but you haven't seen all of them.
Write a function solution(m) that takes an array of array of nonnegative ints representing how many times that state has gone to the next state and return an array of ints for each terminal state giving the exact probabilities of each terminal state, represented as the numerator for each state, then the denominator for all of them at the end and in simplest form. The matrix is at most 10 by 10. It is guaranteed that no matter which state the ore is in, there is a path from that state to a terminal state. That is, the processing will always eventually end in a stable state. The ore starts in state 0. The denominator will fit within a signed 32-bit integer during the calculation, as long as the fraction is simplified regularly.
For example, consider the matrix m:
[
[0,1,0,0,0,1], # s0, the initial state, goes to s1 and s5 with equal probability
[4,0,0,3,2,0], # s1 can become s0, s3, or s4, but with different probabilities
[0,0,0,0,0,0], # s2 is terminal, and unreachable (never observed in practice)
[0,0,0,0,0,0], # s3 is terminal
[0,0,0,0,0,0], # s4 is terminal
[0,0,0,0,0,0], # s5 is terminal
]
So, we can consider different paths to terminal states, such as:
s0 -> s1 -> s3
s0 -> s1 -> s0 -> s1 -> s0 -> s1 -> s4
s0 -> s1 -> s0 -> s5
Tracing the probabilities of each, we find that
s2 has probability 0
s3 has probability 3/14
s4 has probability 1/7
s5 has probability 9/14
So, putting that together, and making a common denominator, gives an answer in the form of
[s2.numerator, s3.numerator, s4.numerator, s5.numerator, denominator] which is
[0, 3, 2, 9, 14].
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({{0, 2, 1, 0, 0}, {0, 0, 0, 3, 4}, {0, 0, 0, 0, 0}, {0, 0, 0, 0,0}, {0, 0, 0, 0, 0}})
Output:
[7, 6, 8, 21]
Input:
Solution.solution({{0, 1, 0, 0, 0, 1}, {4, 0, 0, 3, 2, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}})
Output:
[0, 3, 2, 9, 14]
-- Python cases --
Input:
solution.solution([[0, 2, 1, 0, 0], [0, 0, 0, 3, 4], [0, 0, 0, 0, 0], [0, 0, 0, 0,0], [0, 0, 0, 0, 0]])
Output:
[7, 6, 8, 21]
Input:
solution.solution([[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])
Output:
[0, 3, 2, 9, 14]
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.

View File

@ -0,0 +1,97 @@
import numpy as np
def solution(m):
stable_ores, unstable_ores = categorize_ores(m)
if 0 in stable_ores:
return [1]+[0]*(len(stable_ores)-1)+[1]
p = np.array(m)
q = np.zeros(p.shape, dtype=int)
for i in range(len(m)):
q[i][i] = sum(p[i])
r = q - p
t = gaussian_elimination(r, stable_ores, unstable_ores)
numerators = -t[0][stable_ores]
denominator = sum(numerators)
result = np.hstack((numerators, denominator))
g = abs_gcd(*list(result))
return list(result / g)
def categorize_ores(m):
stable_ores = []
unstable_ores = []
for i in range(len(m)):
if sum(m[i]) == 0:
stable_ores += [i]
else:
unstable_ores += [i]
return stable_ores, unstable_ores
def gaussian_elimination(r, s, u):
arr = r
for i in range(len(u)):
for j in range(i+1, len(u)):
row_up, row_down = arr[u[i]], arr[u[j]]
if row_down[u[i]] != 0:
arr[u[j]] = row_down*arr[u[i]][u[i]] - row_up*arr[u[j]][u[i]]
arr[u[j]] = arr[u[j]] / abs_gcd(*list(arr[u[j]]))
for i in reversed(range(len(u))):
for j in reversed(range(i)):
row_down, row_up = arr[u[i]], arr[u[j]]
if row_up[u[i]] != 0:
arr[u[j]] = row_up*row_down[u[i]] - row_down*row_up[u[i]]
arr[u[j]] = arr[u[j]] / abs_gcd(*list(arr[u[j]]))
return arr
def abs_gcd(*args):
if len(args) == 1:
return abs(args[0])
if len(args) == 2:
m, n = abs(args[0]), abs(args[1])
while n != 0:
t = m % n
m, n = n, t
return abs(m)
return abs_gcd(args[0], abs_gcd(*args[1:]))
tests = [
[
[[0, 2, 1, 0, 0], [0, 0, 0, 3, 4], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
[7, 6, 8, 21]
],
[
[[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
[0, 3, 2, 9, 14]
],
[
[[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0]],
[0, 3, 11, 14]
],
[
[[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
[0, 1, 6, 7]
],
[
[[0]],
[1, 1]
],
[
[[0, 1], [0, 0]],
[1, 1]
],
[
[[0, 5, 0, 3], [0, 0, 4, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[1, 1, 2]
],
]
#print(solution(tests[1][0]), tests[1][1])
for test in tests:
result = solution(test[0])
print(result == test[1], result, test[1])