不确定为什么类似于bubble-sort的程序不起作用

时间:2015-05-21 01:56:28

标签: python python-2.7 checksum bubble-sort

我一直致力于编程挑战problem here,它基本上表明:

  

给定整数数组,您将遍历所有邻居对   元素,从头开始 - 和每对交换成员   第一个元素大于第二个元素。

然后返回掉期金额和最终答案的校验和。我的程序似乎根据它的需要进行排序和校验和。但是我的最后答案是除了他们给出的测试输入之外的一切。

所以:3 5242536 结果输出正确:2 96 7439 92999 240 70748 3 842 74 706 4 86 7 463 1871 7963 904 327 6268 20955 92662 278 57 8 5912 724 70916 13 388 1 697 99666 6924 2 100 186 37504 1 27631 59556 33041 87 9 45276 -1 和我的程序。

但是像:

39 1291223

结果为:39 3485793正确答案为# Python 2.7 def check_sum(data): data = [str(x) for x in str(data)[::]] numbers = len(data) result = 0 for number in range(numbers): result += int(data[number]) result *= 113 result %= 10000007 return(str(result)) def bubble_in_array(data): numbers = data[:-1] numbers = [int(x) for x in numbers] swap_count = 0 for x in range(len(numbers)-1): if numbers[x] > numbers[x+1]: temp = numbers[x+1] numbers[x+1] = numbers[x] numbers[x] = temp swap_count += 1 raw_number = int(''.join([str(x) for x in numbers])) print('%s %s') % (str(swap_count), check_sum(raw_number)) bubble_in_array(raw_input().split())

这就是我现在所拥有的:

       X509Certificate2 certificate = new X509Certificate2(key, "notasecret", X509KeyStorageFlags.Exportable);
                ServiceAccountCredential credential = new ServiceAccountCredential(
                    new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[] { GmailService.Scope.GmailModify },
                        User = userEmail
                    }.FromCertificate(certificate));

                if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
                {
                    GmailService service = new GmailService(
                        new Google.Apis.Services.BaseClientService.Initializer()
                        {
                            HttpClientInitializer = credential
                        }
                    );
        //more code

有谁知道我哪里出错了?

1 个答案:

答案 0 :(得分:1)

问题在于您计算Checksum的方式。当数组具有多个数字的数字时,它会失败。例如:

2 96 7439 92999 240 70748 3 842 74 706 4 86 7 463 1871 7963 904 327 6268 20955 92662 278 57 8 5912 724 70916 13 388 1 697 99666 6924 2 100 186 37504 1 27631 59556 33041 87 9 45276 -1

您正在计算Checksum 2967439240707483842747064867463187179639043276268209559266227857859127247091613388169792999692421001863750412763159556330418794527699666 位数,而您应计算Checksum [2, 96, 7439, 240, 70748, 3, 842, 74, 706, 4, 86, 7, 463, 1871, 7963, 904, 327, 6268, 20955, 92662, 278, 57, 8, 5912, 724, 70916, 13, 388, 1, 697, 92999, 6924, 2, 100, 186, 37504, 1, 27631, 59556, 33041, 87, 9, 45276, 99666]

修复:

# Python 2.7

def check_sum(data):
    result = 0

    for number in data:
        result += number
        result *= 113
        result %= 10000007

    return(result)

def bubble_in_array(data):
    numbers = [int(x) for x in data[:-1]]
    swap_count = 0

    for x in xrange(len(numbers)-1):
        if numbers[x] > numbers[x+1]:
            numbers[x+1], numbers[x] = numbers[x], numbers[x+1]
            swap_count += 1

    print('%d %d') % (swap_count, check_sum(numbers))

bubble_in_array(raw_input().split())

更多备注

  • 要在Python中交换两个变量,您不需要使用temp变量,只需使用a,b = b,a
  • 在python 2.X中,使用xrange代替range