Googlefoo.bar未能通过所有测试用例 但在python IDE中工作

所以我正在做 foo.bar 挑战,我在 python 中有代码输出所需的答案。我知道一个事实,至少在前两个测试用例我的输出匹配他们的输出,但它仍然失败所有这些。我认为这可能是因为它在 python 2.7.13 中运行,所以我找到了一个在线沙箱,运行该版本的 python,但我的代码仍然输出所需的输出结果:我已经尝试使用打印函数来输出

所以我正在做 foo.bar 挑战,我在 python 中有代码输出所需的答案。我知道一个事实,至少在前两个测试用例我的输出匹配他们的输出,但它仍然失败所有这些。我认为这可能是因为它在 python 2.7.13 中运行,所以我找到了一个在线沙箱,运行该版本的 python,但我的代码仍然输出所需的输出结果:我已经尝试使用打印函数来输出

末日燃料

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

拉姆达指挥官已责成您通过预测给定矿石样品的最终状态来帮助科学家提高燃料产生效率。您已经仔细研究了矿石可以采取的不同结构以及它经历的转变。看来,尽管是随机的,但每个结构转变的概率是固定的。也就是说,每次矿石处于 1 个状态时,它进入下一个状态的概率都相同(可能是相同的状态)。您已经在其他实验室中记录了观察到的不同大小的转变。

编写一个函数解(m),该函数解将采用非负整数数组的数组 32 作为表示该状态已进入下一个状态的次数,并为每个终端状态返回一个整数数组,给出每个终端状态的确切概率,表示为每个状态的分子,然后在最后以最简单的形式表示所有这些状态的分母。该矩阵最多为 10 乘 10。可以保证,无论处于哪个状态,最终都有一个整数。

例如,考虑到矩阵 m:[[0,1,0,0,0,1],# s0 的概率为 1,因此,我们可以将 numersgst-5 的分母 t t-1 放在一起,使 numersgs1 和 s5 具有相等的概率
[4,0,0,0,0,0],# s2 是终端,并且无法到达(实践中从未观察到

Languages

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

测试用例 = = = = = = = = = = 您的代码应该通过以下测试用例。请注意,它也可能针对此处未显示的隐藏测试用例运行。

--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}}) 输出:[7,6,8,21]

输入:Solution.solution ({{0,1,0,0,0,0,0,1},{4,0,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})

--Python cases--Input:solution.solution ([[0,2,1,0,0],[0,0,0,3,4],[0,0,0,0,0],[0,0,0,0]]) 输出:[7,6,8,21]

输入:solution.solution ([[0,1,0,0,0,0,0,1],[4,0,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])

我的代码如下:

import numpy as np
from fractions import Fraction
from math import gcd
def solution(M):
    height = (len(M))
    length = (len(M[0]))
    M = np.array(M)
    AB = []
    
    #Find B
    for i in range(0, height):
        #if B = 1
        if (sum(M[:,0])) == 0:
            sumB = 1 
        if(M[i,0]) != 0:
            B1 = Fraction((M[i,0]), (sum(M[i])))
            B2 = Fraction((M[0,i]), (sum(M[0])))
            B = B1 * B2
            #Find sum(B) to infinity
            sumB = (1/(1-B))
    #Find A
    boolean2 = 0
    count = 0
    index = []
    for i in range (0, height):
        if sum(M[i]) == 0:
            if boolean2 == 0:
                terminalstart = i
            boolean = 0
            boolean2 = 1
            for j in range(0, height):
                #if there is no A
                if j==height-1 and boolean == 0:
                    index.append(i-terminalstart)
                    count +=1
                if (M[j,i]) != 0:
                    boolean = 1
                    A1 = Fraction((M[j,i]), (sum(M[j])))
                    A = A1
                    if j!=0:
                        A2 = Fraction((M[0,j]), (sum(M[0])))
                        A = A1 * A2
                    
                    #Find AB
                    AB.append(A*sumB)
    #Find common denominators
    x = []
    y = []
    for i in range (0,len(AB)):
        x.append(AB[i].denominator)
    lcm = 1
    #change numerators to fit
    for i in x:
        lcm = lcm*i//gcd(lcm, i)
    for i in range (0, len(AB)):
        z = (lcm) / x[i]
        #
        z = float(z)
        #
        y.append(int((AB[i].numerator)*z))
        
    #insert 0s
    for i in range (0, count):
        y.insert(index[i], 0)
    #insert denominator
    y.append(lcm)
    return y
    

所以代码和问题基本上是不相关的,主要的一点是,我的输出(y)与示例中的输出完全相同,但是当它在 foo.bar 中运行时,它失败了。

def solution(M):
    y = [0, 3, 2, 9, 14]
    return y

所以我知道,因为我的代码得到完全相同的数组和数据类型 y 在 python IDE 中,它应该在 google foo.bar 中工作,但由于某种原因,它不是。

编辑:我发现一个代码在线工作:

import numpy as np
# Returns indexes of active & terminal states
def detect_states(matrix):
    active, terminal = [], []
    for rowN, row in enumerate(matrix):
        (active if sum(row) else terminal).append(rowN)
    return(active,terminal)
# Convert elements of array in simplest form
def simplest_form(B):
    B = B.round().astype(int).A1                   # np.matrix --> np.array
    gcd = np.gcd.reduce(B)
    B = np.append(B, B.sum())                      # append the common denom
    return (B / gcd).astype(int)
# Finds solution by calculating Absorbing probabilities
def solution(m):
    active, terminal = detect_states(m)
    if 0 in terminal:                              # special case when s0 is terminal
        return [1] + [0]*len(terminal[1:]) + [1]
    m = np.matrix(m, dtype=float)[active, :]       # list --> np.matrix (active states only)
    comm_denom = np.prod(m.sum(1))                 # product of sum of all active rows (used later)
    P = m / m.sum(1)                               # divide by sum of row to convert to probability matrix
    Q, R = P[:, active], P[:, terminal]            # separate Q & R
    I = np.identity(len(Q))
    N = (I - Q) ** (-1)                            # calc fundamental matrix
    B = N[0] * R * comm_denom / np.linalg.det(N)   # get absorbing probs & get them close to some integer
    return simplest_form(B)

当我通过添加行比较这个工作代码的最终答案时:

print(simplest_form(B))
print(type(simplest_form(B))

这是我得到的

[ 0  3  2  9 14]
<cl 'numpy.ndarray'>
array([ 0,  3,  2,  9, 14])

当我添加的行

y = np.asarray(y)
print(y)
print(type(y))

我的代码这是我得到的:

[ 0  3  2  9 14]
<cl 'numpy.ndarray'>
array([ 0,  3,  2,  9, 14])

当他们都运行相同的测试输入。这些是完全相同的,但由于某种原因,我的不工作在 foo.bar,但他的。

0

事实证明

math.gcd(x, y)

函数在 python 2 中是不允许的,我只是把它重写为:

def grcd(x, y):
    if x >= y:
        big = x
        small = y
    else:
        big = y
        small = x
    bool1 = 1
    for i in range(1, big+1):
        while bool1 == 1:
            if big % small == 0:
                greatest = small
                bool1 = 0
            small-= 1
    return greatest

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(928)
如何在Windows上安装Python软件包
上一篇
gpupdate与 powershell
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(59条)