隐藏的测试用例未通过Google Foobar挑战世界末日加油赛

编程入门 行业动态 更新时间:2024-10-13 20:17:58
本文介绍了隐藏的测试用例未通过Google Foobar挑战世界末日加油赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力应对Google Foobar挑战,现在已经达到了世界末日加油赛的3级挑战.说明如下:

I'm working my way through the Google Foobar challenge and am now at the level 3 challenge Doomsday Fuel. The instructions are as follows:

为LAMBCHOP反应堆堆芯制造燃料是一个棘手的过程,因为其中涉及到奇特的物质.它从原始矿石开始,然后在加工过程中开始在形态之间随机变化,最终达到稳定的形态.样品可能最终会达到多种稳定形式,但并非所有形式都可用作燃料.

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命令您通过预测给定矿石样品的最终状态来帮助科学家提高燃料产生效率.您已经仔细研究了矿石可以采用的不同结构以及其经历的过渡过程.看起来,虽然是随机的,但每个结构转换的概率都是固定的.也就是说,矿石每次处于1状态时,进入下一个状态(可能是相同状态)的概率相同.您已将观察到的过渡记录在矩阵中.实验室中的其他人假设矿石可以变成更奇特的形式,但是您没有看到所有这些.

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.

编写一个函数solution(m),该函数采用一个非负整数数组来表示该状态进入下一个状态的次数,并为每个终端状态返回一个整数数组,并给出每个终端状态的确切概率,表示为每个状态的分子,然后以最简单的形式表示所有状态的分母.矩阵最大为10 x10.可以确保无论矿石处于哪种状态,都存在从该状态到最终状态的路径.即,处理将总是最终以稳定状态结束.矿石从状态0开始.分母将在计算过程中放入一个有符号的32位整数内,只要该分数定期进行简化即可.

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

语言

要提供Java解决方案,请编辑Solution.java要提供Python解决方案,请编辑solution.py

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. I have written the following code to solve it:

import java.util.ArrayList; public class Solution { public static int[] solution(int[][] m) { double[][] mDouble = toDouble(m); //TODO: change the double back into an int // GOAL ONE: find Q matrix : // 1:seperate the input into two 2d arrays ArrayList<double[]> ters = new ArrayList<double[]>(); ArrayList<double[]> nonTers = new ArrayList<double[]>(); for(int i = 0; i < mDouble.length; i++){ boolean isTerminal = true; int sum = 0; for(int j = 0; j < mDouble[0].length; j++){ sum += mDouble[i][j]; if(mDouble[i][j] != 0){ isTerminal = false; } } if(isTerminal){ ters.add(mDouble[i]); }else{ for(int j = 0; j < mDouble[0].length; j++){ mDouble[i][j] = mDouble[i][j]/sum; } nonTers.add(mDouble[i]); } } double[][] terminalStates = new double[ters.size()][m.length]; double[][] nonTerminalStates = new double[nonTers.size()][m.length]; for(int i = 0; i < ters.size(); i++){ terminalStates[i] = ters.get(i); } for(int i = 0; i < nonTers.size(); i++){ nonTerminalStates[i] = nonTers.get(i); } // 2: Plug into a function that will create the 2d array double[][] QMatrix = getQMatrix(nonTerminalStates); // GOAL TWO: find I matrix double[][] IMatrix = makeIMatrix(QMatrix.length); //GOAL 3: Find F matrix //1: subtract the q matrix from the I matrix double[][] AMatrix = SubtractMatrices(IMatrix, QMatrix); //2: find the inverse TODO WRITE FUNCTION double[][] FMatrix = invert(AMatrix); //GOAL 4: multiply by R Matrix //1: find r Matrx double[][] RMatrix = getRMatrix(nonTerminalStates, terminalStates.length); //2: use multiply function to get FR Matrix double[][] FRMatrix = multiplyMatrices(FMatrix, RMatrix); //GOAL 5: find answer array //1: get the one dimensional answer double[] unsimplifiedAns = FRMatrix[0]; //2: get fractions for the answers int[] ans = fractionAns(unsimplifiedAns); return ans; } public static int[] fractionAns(double[] uAns){ int[] ans = new int[uAns.length + 1]; int[] denominators = new int[uAns.length]; int[] numerators = new int[uAns.length]; for(int i = 0; i < uAns.length; i++){ denominators[i] = (int)(convertDecimalToFraction(uAns[i])[1]); numerators[i] = (int)(convertDecimalToFraction(uAns[i])[0]); } int lcm = (int) lcm_of_array_elements(denominators); for(int i = 0; i < uAns.length; i++){ ans[i] = numerators[i]*(lcm/convertDecimalToFraction(uAns[i])[1]); } ans[ans.length-1] = lcm; return ans; } static private int[] convertDecimalToFraction(double x){ double tolerance = 1.0E-10; double h1=1; double h2=0; double k1=0; double k2=1; double b = x; do { double a = Math.floor(b); double aux = h1; h1 = a*h1+h2; h2 = aux; aux = k1; k1 = a*k1+k2; k2 = aux; b = 1/(b-a); } while (Math.abs(x-h1/k1) > x*tolerance); return new int[]{(int)h1, (int)k1}; } public static long lcm_of_array_elements(int[] element_array) { long lcm_of_array_elements = 1; int divisor = 2; while (true) { int counter = 0; boolean divisible = false; for (int i = 0; i < element_array.length; i++) { // lcm_of_array_elements (n1, n2, ... 0) = 0. // For negative number we convert into // positive and calculate lcm_of_array_elements. if (element_array[i] == 0) { return 0; } else if (element_array[i] < 0) { element_array[i] = element_array[i] * (-1); } if (element_array[i] == 1) { counter++; } // Divide element_array by devisor if complete // division i.e. without remainder then replace // number with quotient; used for find next factor if (element_array[i] % divisor == 0) { divisible = true; element_array[i] = element_array[i] / divisor; } } // If divisor able to completely divide any number // from array multiply with lcm_of_array_elements // and store into lcm_of_array_elements and continue // to same divisor for next factor finding. // else increment divisor if (divisible) { lcm_of_array_elements = lcm_of_array_elements * divisor; } else { divisor++; } // Check if all element_array is 1 indicate // we found all factors and terminate while loop. if (counter == element_array.length) { return lcm_of_array_elements; } } } public static double[][] toDouble(int[][] ma){ double[][] retArr = new double[ma.length][ma.length]; for(int i = 0; i < retArr.length; i++){ for(int j = 0; j < retArr[0].length; j++){ retArr[i][j] = (ma[i][j]); } } return retArr; } public static double[][] getRMatrix(double[][] nonTerminals, int terminalLength){ double[][] retArr = new double[nonTerminals.length][terminalLength]; for(int i = 0; i < retArr.length; i++){ for(int j = nonTerminals.length; j < nonTerminals[0].length; j++){ retArr[i][j-nonTerminals.length] = (nonTerminals[i][j]); } } return retArr; } public static double[][] multiplyMatrices(double[][] firstMatrix, double[][] secondMatrix){ int r1 = firstMatrix.length; int c1 = firstMatrix[0].length; int c2 = secondMatrix[0].length; double[][] product = new double[r1][c2]; for(int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { for (int k = 0; k < c1; k++) { product[i][j] += firstMatrix[i][k] * secondMatrix[k][j]; } } } return product; } public static double[][] inverseMatrix(double[][] Amatrix){ return null; } public static double[][] SubtractMatrices(double[][] I, double[][] Q){ double[][] retArr = new double[I.length][I.length]; for(int i = 0; i < retArr.length; i++){ for(int j = 0; j < retArr.length; j++){ retArr[i][j] = I[i][j]-Q[i][j]; } } return retArr; } public static double[][] getQMatrix(double[][] qArr){ int size = qArr.length; double[][] retArr = new double[size][size]; for(int i = 0; i < size; i++){ for(int j = 0; j < size; j++){ retArr[i][j] = qArr[i][j]; } } return retArr; } public static double[][] makeIMatrix(int size){ double[][] retArr = new double[size][size]; for(int i = 0; i < size; i++){ for(int j = 0; j < size; j++){ if(i == j){ retArr[i][j] = 1; }else{ retArr[i][j] = 0; } } } return retArr; } public static double[][] invert(double a[][]) { int n = a.length; double x[][] = new double[n][n]; double b[][] = new double[n][n]; int index[] = new int[n]; for (int i=0; i<n; ++i) b[i][i] = 1; // Transform the matrix into an upper triangle gaussian(a, index); // Update the matrix b[i][j] with the ratios stored for (int i=0; i<n-1; ++i) for (int j=i+1; j<n; ++j) for (int k=0; k<n; ++k) b[index[j]][k] -= a[index[j]][i]*b[index[i]][k]; // Perform backward substitutions for (int i=0; i<n; ++i) { x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1]; for (int j=n-2; j>=0; --j) { x[j][i] = b[index[j]][i]; for (int k=j+1; k<n; ++k) { x[j][i] -= a[index[j]][k]*x[k][i]; } x[j][i] /= a[index[j]][j]; } } return x; } // Method to carry out the partial-pivoting Gaussian // elimination. Here index[] stores pivoting order. public static void gaussian(double a[][], int index[]) { int n = index.length; double c[] = new double[n]; // Initialize the index for (int i=0; i<n; ++i) index[i] = i; // Find the rescaling factors, one from each row for (int i=0; i<n; ++i) { double c1 = 0; for (int j=0; j<n; ++j) { double c0 = Math.abs(a[i][j]); if (c0 > c1) c1 = c0; } c[i] = c1; } // Search the pivoting element from each column int k = 0; for (int j=0; j<n-1; ++j) { double pi1 = 0; for (int i=j; i<n; ++i) { double pi0 = Math.abs(a[index[i]][j]); pi0 /= c[index[i]]; if (pi0 > pi1) { pi1 = pi0; k = i; } } // Interchange rows according to the pivoting order int itmp = index[j]; index[j] = index[k]; index[k] = itmp; for (int i=j+1; i<n; ++i) { double pj = a[index[i]][j]/a[index[j]][j]; // Record pivoting ratios below the diagonal a[index[i]][j] = pj; // Modify other elements accordingly for (int l=j+1; l<n; ++l) a[index[i]][l] -= pj*a[index[j]][l]; } } } }

它通过了所有测试用例,但有两个我看不见的隐藏用例.

It passes all the test cases but two hidden ones I cannot see.

我已经尝试了所有可能在代码中找到故障的测试用例,但我没有.

I've tried all the test cases I possibly could to find the fault in my code but I cannot.

这里有没有我的代码失败的测试用例?

Are there any test cases here where my code fails?

推荐答案

问题出在行上

double[] unsimplifiedAns = FRMatrix[0];

仅当状态0为非终止时,以上情况才成立.

The above is true only if state 0 is non-terminating.

否则,输出数组将为全0,除了第一个和最后一个元素为"1".

Otherwise the output array will be all '0's except the first and last element as '1'.

更多推荐

隐藏的测试用例未通过Google Foobar挑战世界末日加油赛

本文发布于:2023-11-29 11:26:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:末日   测试   世界   Google   用例未

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!