+/foobar
google programming puzzles
git clone https://git.julia.blue/+/foobar
2e1d345
master
1Doomsday Fuel 2============= 3 4Making 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. 5 6Commander 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. 7 8Write a function answer(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. 9 10For example, consider the matrix m: 11[ 12 [0,1,0,0,0,1], # s0, the initial state, goes to s1 and s5 with equal probability 13 [4,0,0,3,2,0], # s1 can become s0, s3, or s4, but with different probabilities 14 [0,0,0,0,0,0], # s2 is terminal, and unreachable (never observed in practice) 15 [0,0,0,0,0,0], # s3 is terminal 16 [0,0,0,0,0,0], # s4 is terminal 17 [0,0,0,0,0,0], # s5 is terminal 18] 19So, we can consider different paths to terminal states, such as: 20s0 -> s1 -> s3 21s0 -> s1 -> s0 -> s1 -> s0 -> s1 -> s4 22s0 -> s1 -> s0 -> s5 23Tracing the probabilities of each, we find that 24s2 has probability 0 25s3 has probability 3/14 26s4 has probability 1/7 27s5 has probability 9/14 28So, putting that together, and making a common denominator, gives an answer in the form of 29[s2.numerator, s3.numerator, s4.numerator, s5.numerator, denominator] which is 30[0, 3, 2, 9, 14]. 31 32Languages 33========= 34 35To provide a Python solution, edit solution.py 36To provide a Java solution, edit solution.java 37 38Test cases 39========== 40 41Inputs: 42 (int) m = [[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]] 43Output: 44 (int list) [7, 6, 8, 21] 45 46Inputs: 47 (int) m = [[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]] 48Output: 49 (int list) [0, 3, 2, 9, 14] 50 511506 nix nix! 52