Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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中计算交换_Python_Python 3.x_List_Compare And Swap - Fatal编程技术网

列表中的最小交换元素,使其与另一个列表相同,并在python中计算交换

列表中的最小交换元素,使其与另一个列表相同,并在python中计算交换,python,python-3.x,list,compare-and-swap,Python,Python 3.x,List,Compare And Swap,我必须列出作为我的输入, a=[0,1,0,1]和 b=[1,0,1,0] 注意:两个列表的元素将仅为0和1。如果无法通过交换使两者相同,则我将打印-1。如果开始时相同,则打印0,如果不相同, 我想让a==b,在这种情况下,我需要2个最小交换。 第一次掉期将是a的第0个索引和a的第1个索引,以及 第二次互换将是,a的第二个索引和a的第三个索引。 之后,a将与b相同 这是我的密码: def xx(a,b): move = 0 if a == b: print(0)

我必须列出作为我的输入, a=[0,1,0,1]和 b=[1,0,1,0]

注意:两个列表的元素将仅为0和1。如果无法通过交换使两者相同,则我将打印-1。如果开始时相同,则打印0,如果不相同, 我想让a==b,在这种情况下,我需要2个最小交换。 第一次掉期将是a的第0个索引和a的第1个索引,以及 第二次互换将是,a的第二个索引和a的第三个索引。 之后,a将与b相同

这是我的密码:

def xx(a,b):
    move = 0
    if a == b:
        print(0)
    else:
        if len(a) == 1 and len(a) == len(b):
            print(-1)
        else:
            for i in range(len(a)):
                if a[i] != b[i]:
                    j = a.index(b[i])
                    a[i] = a[j]
                    move += 1
            count_swap = move // 2
            print(count_swap)
a = list(map(int,input().split()))
b = list(map(int,input().split()))
xx(a,b)
有没有什么有效的方法来获得交换计数

输入:

0 1 0 1
1 0 1 0
0 1 0
1 0 0
0
1
输出:

2
1
-1
True 2 [0, 1, 0, 1] [1, 0, 1, 0]
True 1 [0, 1, 0] [1, 0, 0]
True -1 [0] [1]
True 0 [0, 1, 0, 1] [0, 1, 0, 1]
输入:

0 1 0 1
1 0 1 0
0 1 0
1 0 0
0
1
输出:

2
1
-1
True 2 [0, 1, 0, 1] [1, 0, 1, 0]
True 1 [0, 1, 0] [1, 0, 0]
True -1 [0] [1]
True 0 [0, 1, 0, 1] [0, 1, 0, 1]
输入:

0 1 0 1
1 0 1 0
0 1 0
1 0 0
0
1
输出:

2
1
-1
True 2 [0, 1, 0, 1] [1, 0, 1, 0]
True 1 [0, 1, 0] [1, 0, 0]
True -1 [0] [1]
True 0 [0, 1, 0, 1] [0, 1, 0, 1]

首先,为了使互换使列表相等,它们必须以相同数量的1和0开始。因此,我们可以使用
计数器
检查不可能性

其次,单一互换必然会修复两个差异。所以我们可以只计算差异,然后除以2。我们实际上不需要进行任何互换

示范:

from collections import Counter

def swap_count(xs, ys):
    if xs == ys:
        return 0
    else:
        cx = Counter(xs)
        cy = Counter(ys)
        if cx == cy:
            n_diffs = sum(x != y for x, y in zip(xs, ys))
            return n_diffs // 2
        else:
            return -1

def main():
    tests = [
        (2, [0, 1, 0, 1], [1, 0, 1, 0]),
        (1, [0, 1, 0], [1, 0, 0]),
        (-1, [0], [1]),
        (0, [0, 1, 0, 1], [0, 1, 0, 1]),
    ]
    for exp, xs, ys in tests:
        n = swap_count(xs, ys)
        print(n == exp, n, xs, ys)

main()
输出:

2
1
-1
True 2 [0, 1, 0, 1] [1, 0, 1, 0]
True 1 [0, 1, 0] [1, 0, 0]
True -1 [0] [1]
True 0 [0, 1, 0, 1] [0, 1, 0, 1]
这应该是一个O(N)解决方案,它迭代项目并执行列表b上的交换。如果我们离开列表b(索引器)的末尾,则无法找到解决方案并返回-1

def count_swaps(a, b):
    swaps = 0
    for idx in range(len(a)):
        if a[idx] != b[idx]:
            try:
                b[idx], b[idx + 1] = b[idx + 1], b[idx]
                swaps += 1
            except IndexError:
                return -1

    return swaps


assert count_swaps([0, 1, 0, 1], [1, 0, 1, 0]) == 2
assert count_swaps([0, 1, 0], [1, 0, 0]) == 1
assert count_swaps([0], [1]) == -1