【问题标题】:python function sorting alternating diskspython函数对交替磁盘进行排序
【发布时间】:2013-04-14 04:36:05
【问题描述】:

抱歉刚才遗漏的信息!!

我需要一些帮助来完成我的任务,这是基本的 Python 编程,但在实现该功能时我遇到了困难。希望可以有人帮帮我!!提前谢谢^^

任务: 首先,我需要构建一个数组,A 2n 交替“1”和“0”。即101010 然后,我需要按全 0 然后全 1 的顺序对它们进行排序,即 000111

为此,我需要构建一个函数 Arrange_Disks(A,n) 来接收数组 A 和 n,并在完成 2n 个磁盘的必要移动后返回数组。主函数应显示排列前后的数组以及进行排列所需的移动次数。使用 2 到 10 范围内的 n 值。

下面是我目前完成的功能:

A=[]

def Arrange_Disks(A,n):

    moves=0
    for i in xrange(n):
        minn=i
        for j in xrange(i+1,n):
            if A[j]<A[minn]:
                minn=j
        moves+=minn-i       
        A[i],A[minn]=A[minn],A[i]
    return A
    return moves


def main():

    n=input("Please enter a number between 2 to 10: ")

    for disk in range (1,(2*n+1)):
        if (disk%2) != 0:
            A.append("1")
        else:
            A.append("0")
    print "This is the original array: ", A

    Arrange_Disks(A,n)

    print "This is the sorted array: ", A
    print "This is the number of moves required: ", moves

main()

不知何故,当我运行这段代码时,它只显示到这里:

>Please enter a number between 2 to 10: 3
>This is the original array:  ['1', '0', '1', '0', '1', '0']
>This is the sorted array:  ['0', '1', '1', '0', '1', '0']
>This is the number of moves required:  0

并且输出不反映移动的数量......

所以我想知道是否有人可以启发我了解我的代码。我知道这是一个很长的问题,非常感谢您阅读并花时间回答我的问题!

ps 我对这个问题进行了冒泡排序 我正在使用 python 2.7

再次感谢^^

【问题讨论】:

  • 只是一个小点,一个方法中可以有多个return语句;但是按照您编写的方式,第一个将退出该方法,而第二个将永远不会运行。

标签: python list python-2.7 sorting


【解决方案1】:

如果我正确理解了您的问题,那么我想您可以在这里使用Selection Sort

def selection_sort(A,n):
    moves=0
    for i in xrange(n):
        minn=i
        for j in xrange(i+1,n):
            if A[j]<A[minn]:
                minn=j
        moves+=minn-i       
        A[i],A[minn]=A[minn],A[i]
    print moves,A

#examples:
selection_sort(['1', '0', '1', '0', '1', '0'],6)
selection_sort(['1', '0', '1', '1', '1', '1'],6)

输出:

6 ['0', '0', '0', '1', '1', '1']
1 ['0', '1', '1', '1', '1', '1']

要生成这些交替的1,0 列表,您可以使用itertools.cycle

>>> from itertools import cycle
>>> c=cycle(('1','0'))
>>> n=3
>>> [next(c) for _ in xrange(n*2)]
['1', '0', '1', '0', '1', '0']

您的代码的工作版本:

def Arrange_Disks(A):
    length=len(A)
    moves=0
    for i in xrange(length):
        minn=i
        for j in xrange(i+1,length):
            if A[j]<A[minn]:
                minn=j
        moves+=minn-i       
        A[i],A[minn]=A[minn],A[i]
    return A,moves                #returns a tuple containing both A and moves


def main():

    n=input("Please enter a number between 2 to 10: ")
    A=[]
    for disk in range (1,(2*n+1)):
        if (disk%2) != 0:
            A.append("1")
        else:
            A.append("0")
    print "This is the original array: ", A

    A,moves=Arrange_Disks(A)

    print "This is the sorted array: ", A
    print "This is the number of moves required: ", moves

main()

输出:

Please enter a number between 2 to 10: 3
This is the original array:  ['1', '0', '1', '0', '1', '0']
This is the sorted array:  ['0', '0', '0', '1', '1', '1']
This is the number of moves required:  6

Please enter a number between 2 to 10: 5
This is the original array:  ['1', '0', '1', '0', '1', '0', '1', '0', '1', '0']
This is the sorted array:  ['0', '0', '0', '0', '0', '1', '1', '1', '1', '1']
This is the number of moves required:  15

【讨论】:

  • 我很抱歉我的信息丢失了!!但是,选择排序会产生一个非常奇怪的输出,例如 ['0', '1', '1', '0', '1', '0']...但是非常感谢您的帮助!
  • @Lavinia 问题是你传递了错误的n 值,n 传递给`Arrange_Disks` 必须等于列表的长度(即 n*2)。我已在我的解决方案中添加了您的代码的工作版本。
  • 非常感谢您的帮助!!!我真的很感激!!但是我仍然没有真正理解传递 n 值的逻辑......就像我认为 n 只是用户请求的值?为什么它与数组的长度有关?再次感谢您对我的问题如此耐心!!
  • @Lavinia 在 python 中没有必要传递该值,因为列表已经有一个 __len__ 方法,但在像 C 这样的语言中,你必须明确传递 n*2 作为长度。我已经修复了我的代码。
  • 我明白了!!非常感谢您的耐心和明确的解释^^ ohhh erm 我可以添加一个问题吗?为什么你使用moves+=minn-i 而不是moves=moves+1?我并没有真正关注这部分......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2021-10-15
  • 2022-01-20
相关资源
最近更新 更多