【问题标题】:doctest is failing while output seems correctdoctest 失败,而输出似乎正确
【发布时间】:2019-05-10 07:52:07
【问题描述】:

我正在尝试运行 python 代码,输出似乎是正确的,但没有通过 doctest 测试用例,我需要帮助。

我的输出很完美,输出看起来和 doctest 中提供的测试用例一样。

from random import randrange, seed
from collections import defaultdict


def display(square, size):
    print('\n'.join(' '.join(f'{x:{size}}' for x in row) for row in square))


def f(for_seed, n, upper_bound):
    """
    >>> f(0, 2, 2)
    Here is the square:
    1 1
    0 1
    It is not a good square because it contains duplicates, namely: 1
    >>> f(0, 3, 5)
    Here is the square:
    3 3 0
    2 4 3
    3 2 3
    It is not a good square because it contains duplicates, namely: 2 3
    >>> f(0, 6, 50)
    Here is the square:
    24 48 26  2 16 32
    31 25 19 30 22 37
    13 32  8 18  8 48
     6 39 16 34 45 38
     9 19  6 46  4 43
    21 30 35  6 22 27
    It is not a good square because it contains duplicates, namely: 6 8 16 19 22 30 32 48
    >>> f(0, 2, 50)
    Here is the square:
    24 48
    26  2
    It is a good square.
    Ordering the elements from left to right column, from top to bottom, yields:
     2 26
    24 48
    >>> f(0, 3, 100)
    Here is the square:
     49  97  53
      5  33  65
     62  51  38
    It is a good square.
    Ordering the elements from left to right column, from top to bottom, yields:
      5  49  62
     33  51  65
     38  53  97
    >>> f(0, 6, 5000)
    Here is the square:
    3155 3445  331 2121 4188 3980
    3317 2484 3904 2933 4779 1789
    4134 1140 2308 1144  776 2052
    4362 4930 1203 2540  809  604
    2704 3867 4585  824 2898 3556
    2590 1675 4526 3907 3626 4270
    It is a good square.
    Ordering the elements from left to right column, from top to bottom, yields:
     331 1144 2308 2933 3867 4270
     604 1203 2484 3155 3904 4362
     776 1675 2540 3317 3907 4526
     809 1789 2590 3445 3980 4585
     824 2052 2704 3556 4134 4779
    1140 2121 2898 3626 4188 4930

    """
    seed(for_seed)
    square = [[randrange(upper_bound) for _ in range (n)] for _ in range(n)]
    duplicates = set()
    ordered_square = []
    print('Here is the square: ')
    display(square, len(str(upper_bound)))
    elements = defaultdict(int)
    for row in square:
        for column in row:
            elements[column] += 1
    for i in elements:
        if elements[i]>1:
            duplicates.add(i)

    if duplicates:
        print('It is not a good square because it contains duplicates, namely: ', end = '')
        print(' '.join(str(e) for e in sorted(duplicates)))
    else:
        print('It is a good square.')
        print('Ordering the elements from left to right column, from top to bottom, yields: ')
        ordered_square = h(square,n)
        display(ordered_square, len(str(upper_bound)))


def h(square,n):
    ordered_list = []
    ordered_square = [[0]*n for _ in range(n)]
    for row in square:
        for column in row:
            ordered_list.append(column)
    ordered_list = sorted(ordered_list)
    k = 0
    for i in range(n):
        for j in range(n):
            ordered_square[j][i] = ordered_list[k]
            k += 1

    return ordered_square


if __name__ == '__main__':
    import doctest
    doctest.testmod()

文档测试输出:

Failed example:
    f(0, 3, 100)
Expected:
    Here is the square:
     49  97  53
      5  33  65
     62  51  38
    It is a good square.
    Ordering the elements from left to right column, from top to bottom, yields:
      5  49  62
     33  51  65
     38  53  97
Got:
    Here is the square:
     49  97  53
      5  33  65
     62  51  38
    It is a good square.
    Ordering the elements from left to right column, from top to bottom, yields:
      5  49  62
     33  51  65
     38  53  97

输出看起来几乎相同。我的逻辑似乎也是正确的,但我不明白为什么 doctest 没有通过。请帮忙。

【问题讨论】:

    标签: python python-3.x doctest


    【解决方案1】:

    您的代码会打印尾随空格,而您的 doctests 并不期望它们。删除以下两个打印行中的尾随空格:

    print('Here is the square: ')
    

    print('Ordering the elements from left to right column, from top to bottom, yields: ')
    

    【讨论】:

      【解决方案2】:

      问题是因为多余的空格和多余的行。删除它,一切正常。

      PFB 代码。

      '''
      You might find the zip() function useful, though you can also do without it.
      '''
      
      from random import randrange, seed
      from collections import defaultdict
      
      
      def display(square, size):
          print('\n'.join(' '.join(f'{x:{size}}' for x in row) for row in square))
      
      def f(for_seed, n, upper_bound):
          '''
          >>> f(0, 2, 2)
          Here is the square:
          1 1
          0 1
          It is not a good square because it contains duplicates, namely: 1
          >>> f(0, 3, 5)
          Here is the square:
          3 3 0
          2 4 3
          3 2 3
          It is not a good square because it contains duplicates, namely: 2 3
          >>> f(0, 6, 50)
          Here is the square:
          24 48 26  2 16 32
          31 25 19 30 22 37
          13 32  8 18  8 48
           6 39 16 34 45 38
           9 19  6 46  4 43
          21 30 35  6 22 27
          It is not a good square because it contains duplicates, namely: 6 8 16 19 22 30 32 48
          >>> f(0, 2, 50)
          Here is the square:
          24 48
          26  2
          It is a good square.
          Ordering the elements from left to right column, from top to bottom, yields:
           2 26
          24 48
          >>> f(0, 3, 100)
          Here is the square:
           49  97  53
            5  33  65
           62  51  38
          It is a good square.
          Ordering the elements from left to right column, from top to bottom, yields:
            5  49  62
           33  51  65
           38  53  97
          >>> f(0, 6, 5000)
          Here is the square:
          3155 3445  331 2121 4188 3980
          3317 2484 3904 2933 4779 1789
          4134 1140 2308 1144  776 2052
          4362 4930 1203 2540  809  604
          2704 3867 4585  824 2898 3556
          2590 1675 4526 3907 3626 4270
          It is a good square.
          Ordering the elements from left to right column, from top to bottom, yields:
           331 1144 2308 2933 3867 4270
           604 1203 2484 3155 3904 4362
           776 1675 2540 3317 3907 4526
           809 1789 2590 3445 3980 4585
           824 2052 2704 3556 4134 4779
          1140 2121 2898 3626 4188 4930
      
          '''
          seed(for_seed)
          square = [[randrange(upper_bound) for _ in range (n)] for _ in range(n)]
          duplicates = set()
          ordered_square = []
          print('Here is the square:')
          display(square, len(str(upper_bound)))
          elements = defaultdict(int)
          for row in square:
              for column in row:
                  elements[column] += 1
          for i in elements:
              if elements[i]>1:
                  duplicates.add(i)
      
          if duplicates:
              print('It is not a good square because it contains duplicates, namely: ', end = '')
              print(' '.join(str(e) for e in sorted(duplicates)))
          else:
              print('It is a good square.')
              print('Ordering the elements from left to right column, from top to bottom, yields:')
              ordered_square = h(square,n)
              display(ordered_square, len(str(upper_bound)))
      
      def h(square,n):
          ordered_list = []
          ordered_square = [[0]*n for _ in range(n)]
          for row in square:
              for column in row:
                  ordered_list.append(column)
          ordered_list = sorted(ordered_list)
          k = 0
          for i in range(n):
              for j in range(n):
                  ordered_square[j][i] = ordered_list[k]
                  k += 1
      
          return ordered_square
      
      
      if __name__ == '__main__':
          import doctest
          doctest.testmod()
      

      做完之后,所有的测试用例都通过了。

      【讨论】:

      • 只有尾随空格有问题,附加行没有问题。看我之前的回答。我的回答返回 0 次失败 :-)
      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多