【问题标题】:Splitting user input into lists of increasing size [python]将用户输入拆分为越来越大的列表 [python]
【发布时间】:2018-05-24 16:52:59
【问题描述】:

我正在尝试生成一个代码来验证用户输入是否符合帕斯卡三角形的标准。我知道如何输入行数并让它形成一个帕斯卡三角形,但我无法弄清楚如何让用户输入像1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 这样的东西,并且让我的程序说它是否是帕斯卡三角形或不。

values = input("Enter the numbers: ").split()

pascals_triangle = list(map(list, values))

我知道第一行可以拆分数字,第二行会将数字分配到列表的各个列表中。每次我尝试让列表在每一行中增加 1 时,我都会收到 str/int 错误。一旦我通过了这个小路障,我应该能够找出其余的代码。

data = input("Enter values: ").split()


def pascal_triangle(data):
    size = int(data[0])
    n = 2 * size + 1
    grid = [[0 for x in range(n)] for y in range(size)]

    left = 1
    for i in range(size, 0, -1):
        grids = data[i].split(' ')
        count = 0
        for g in grids:
            grid[i - 1][left + 2 * count] = int(g)
            count += 1
        left += 1
        if count != i:
            return False

    left = 1
    for i in range(size - 1, -1, -1):
        if i == 0:
            return grid[i][left] == 1
        numbers = i + 1
        count = 0
        while count < numbers:
            current = grid[i][left + count * 2]
            upper_left = grid[i - 1][left - 1 + count * 2]
            upper_right = grid[i - 1][left + 1 + count * 2]
            if current != (upper_left + upper_right):
                return False
            count += 1

        left += 1
    return False



status = pascal_triangle(data)
if status:
    print('It is a pascal triangle')
else:
    print('It is not a pascal triangle')

那么,在这段代码中,为什么我仍然没有得到准确的答案?

【问题讨论】:

  • 向我们展示您出错的尝试,以便我们可以解释错误或您如何出错。一个简单但通用的答案是使用长度增加的切片。
  • “每次我尝试获取列表时......” 你能展示一下你的尝试吗?这对我们来说也会更容易。
  • 如果以下答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道您的问题已得到您满意的解决,并为帮助您的人提供帮助。有关完整说明,请参阅here

标签: python python-3.x list


【解决方案1】:

如果您尝试以某种奇特的方式执行此操作,例如调整 itertools 文档中的 grouper 配方以采用可迭代的组大小而不是固定的组大小……退后一步,编写首先是“哑”版本。-只需编写一个循环。

首先,拆分整个字符串,就像在逐行版本中拆分每一行一样。

有一件事:将list 映射到你的价值观上不会有任何好处;这只会变成,例如,'23' 变成 ['2', '3'],你可以做的并不多。您需要一个数字列表,然后将其分成一行(每一行也是一个数字列表 - 通过逐行映射 intline.split() 得到的同一行版本)。

所以,这里有一些伪代码:

values = input("Enter the numbers: ").split()
nums = [int(value) for value in values]
size = 1
start = 0
while start < len(nums):
    rownums = nums[start:start+size]
    make sure len(rownums) == size
    check rownums the same way you checked each line
    update size and start
if you got here without seeing any errors, it's valid

【讨论】:

    【解决方案2】:

    一种方法是生成帕斯卡三角形的每一行,并使用islice 从用户数据中获取当前行长度的列表,并查看数据是否与该行匹配。

    from itertools import islice
    
    def pascal():
        """ Pascal's triangle generator """
        a = [1]
        while True:
            yield a
            #Generate next row from current row
            a = [x + y for x, y in zip([0] + a, a + [0])]
    
    def test_pascal(values):
        it = map(int, values.split())
        ok = True
        for row in pascal():
            data = list(islice(it, len(row)))
            if not data:
                break
            if data != row:
                ok = False
                print('bad data', data, row)
                break
        return ok
    
    # Test
    
    values = '1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1'
    print(test_pascal(values))
    
    values = '1 1 1 1 2 1 1 3 3 1 1 4 6 5 1'
    print(test_pascal(values))
    

    输出

    True
    bad data [1, 4, 6, 5, 1] [1, 4, 6, 4, 1]
    False
    

    【讨论】:

    • @random_student itertools 里面有一些非常方便的东西(事实上,它是built-in modules 之一),但是学习如何有效地使用它们确实需要一些时间。 islice 非常简单:islice(it, n) 表示从名为 it 的迭代中获取下一个 n 项。它返回一个迭代器,所以我们需要把它变成列表。
    猜你喜欢
    • 1970-01-01
    • 2014-05-02
    • 2012-10-25
    • 2017-10-15
    • 2019-07-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多