Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python可以';t比较最大公约数的值_Python_Algorithm_Greatest Common Divisor - Fatal编程技术网

Python可以';t比较最大公约数的值

Python可以';t比较最大公约数的值,python,algorithm,greatest-common-divisor,Python,Algorithm,Greatest Common Divisor,我正在努力解决这个黑客问题: 这是我的尝试: from fractions import gcd def connectedCities(numCities, threshold, originCities, destinationCities): # Write your code here output = [] for i in range(0, len(originCities)): if gcd(originCities[i], d

我正在努力解决这个黑客问题:

这是我的尝试:

from fractions import gcd

def connectedCities(numCities, threshold, originCities, destinationCities):
    # Write your code here
    output = []
    
    for i in range(0, len(originCities)):
        if gcd(originCities[i], destinationCities[i]) > threshold:
            output.append(1)
        else:
            output.append(0)
    
    return output
但对于以下输入:

10
1
4
10
4
3
6
4
3
6
2
9
我的输出是:

Your Output (stdout)
0
1
0
1


Expected Output
1
1
1
1

我也不太明白如何读取输入。但是我想我知道为什么你的代码没有给出预期的输出。您正在忽略传递属性

两个城市直接连接到iff城市编号的gcd>阈值。然而,它们也可以通过其他城市间接连接。您的代码忽略了这种可能性

Example:
numOfCities = 8
threshold = 1
2 is directly connected to 4, 6, 8 (gcd = 2)
6 is directly connected to 3 (gcd = 3)
therefore 2,4,8 can also reach 3 
一种方法: 循环浏览所有城市编号,找到直接连接的城市(这将为您提供一个邻接目录)
然后使用邻接字典(通过广度或深度搜索)查看起点城市是否可以到达目的地城市。

我完全不知道这是好是坏,但它很好地解决了问题

from math import gcd

def exists(a, b, t):
    return gcd(a, b) > t

# find goes trough everithing it can
def find(paths, seen, start, end, true, path):
    for i in paths[start]:
        if i in seen or i == true:
            continue
        # creating shortcuts, it may backfire or help performance, it depends
        for j in path:
            paths[i].add(j)
        path.append(i)
        if i == end:
            return True
        seen.add(i)
        if find(paths, seen, i, end, true, path):
            return True
    return False


def solve(nc, t, sc, ec):
    ew = sc + ec

    # create lookup table
    paths = [None] * len(ew)
    for i in range(len(ew)):
        paths[i] = set()

    # fill it
    for i in range(len(ew)):
        for j in range(i+1, len(ew)):
            if exists(ew[i], ew[j], t):
                paths[i].add(j)
                paths[j].add(i)

    # traverse paths to find a values
    res = []
    seen = set()
    path = []
    for i in range(len(sc)):
        if exists(sc[i], ec[i], t) or find(paths, seen, i, i + len(sc), i, path):
            res.append(1)
        else:
            res.append(0)
        seen.clear()
        path.clear()
    return res
            
        

print(solve(10, 1, [4, 10, 4, 3, 6], [4, 3, 6, 2, 9]))

你应该如何阅读这些输入?@JakubDóka作为什么的列表?这些是城市吗?那么trashold有多大,我们有多少客户机?在附件中,有一个示例显示输入shapeokey。这将是一个困难的示例,但可行。根据我的代码,您介意给我看一个演示吗?