简体   繁体   English

在列中连续打印数字

[英]Print numbers serially in columns

I am struggling in one of the Pattern matching problems in Python我正在努力解决 Python 中的模式匹配问题之一

When input = 3, below is the expected output (input value is the number of columns it should print)当输入 = 3 时,下面是预期的 output(输入值是它应该打印的列数)

Expected output:预期 output:

1
2 6
3 7 9
4 8
5

I am somehow moving in a wrong direction, hence would need some help in it.我不知何故朝着错误的方向前进,因此需要一些帮助。

This is the code I have tried so far:这是我到目前为止尝试过的代码:

def display(): 
        n = 5
        i = 1
        # Outer loop for how many lines we want to print 
        while(i<=n):  
            k = i 
            j = 1
  
            # Inner loop for printing natural number 
            while(j <= i):  
                print (k,end=" ") 
                  
                # Logic to print natural value column-wise 
                k = k + n - j 
                j = j + 1
                  
            print("\r") 
            i = i + 1
  
#Driver code 
display() 

But it is giving me output as this:但它给了我 output 是这样的:

1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15 

Anybody who can help me with this?谁能帮我解决这个问题?

n=10
for i in range(1,2*n):
    k=i
    for j in range(2*n-i if i>n else i):
        print(k,end=' ')
        k = k + 2*n - 2*j - 2
    print()

Result结果

1 
2 20 
3 21 37 
4 22 38 52 
5 23 39 53 65 
6 24 40 54 66 76 
7 25 41 55 67 77 85 
8 26 42 56 68 78 86 92 
9 27 43 57 69 79 87 93 97 
10 28 44 58 70 80 88 94 98 100 
11 29 45 59 71 81 89 95 99 
12 30 46 60 72 82 90 96 
13 31 47 61 73 83 91 
14 32 48 62 74 84 
15 33 49 63 75 
16 34 50 64 
17 35 51 
18 36 
19 
> 

Here's a way, I started from scratch and not for code, much more easy for me这是一种方法,我从头开始,而不是代码,对我来说更容易

def build(nb_cols):
    values = list(range(1, nb_cols ** 2 + 1))
    res = []
    for idx in range(nb_cols):
        row_values, values = values[-(idx * 2 + 1):], values[:-(idx * 2 + 1)]
        res.append([' '] * (nb_cols - idx - 1) + row_values + [' '] * (nb_cols - idx - 1))

    for r in zip(*reversed(res)):
        print(" ".join(map(str, r)))

Here's a recursive solution:这是一个递归解决方案:

def col_counter(start, end):
    yield start
    if start < end:
        yield from col_counter(start+1, end)
        yield start

def row_generator(start, col, N, i=1):
    if i < col:
        start = start + 2*(N - i)     
        yield start
        yield from row_generator(start, col, N, i+1)

def display(N):
    for i, col_num in enumerate(col_counter(1, N), 1):
        print(i, *row_generator(i, col_num, N))

Output: Output:

>>> display(3)
1
2 6
3 7 9
4 8
5

>>> display(4)
1
2 8
3 9 13
4 10 14 16
5 11 15
6 12
7

>>> display(10)
1
2 20
3 21 37
4 22 38 52
5 23 39 53 65
6 24 40 54 66 76
7 25 41 55 67 77 85
8 26 42 56 68 78 86 92
9 27 43 57 69 79 87 93 97
10 28 44 58 70 80 88 94 98 100
11 29 45 59 71 81 89 95 99
12 30 46 60 72 82 90 96
13 31 47 61 73 83 91
14 32 48 62 74 84
15 33 49 63 75
16 34 50 64
17 35 51
18 36
19

Here is the solution using simple loops这是使用简单循环的解决方案

def display(n):
    nrow = 2*n -1  #Number of rows
    i = 1
    noofcols = 1   #Number of columns in each row
    t = 1
    while (i <= nrow):
        print(i,end=' ') 
        
        if i <= n: 
            noofcols = i
        else:
            noofcols = 2*n - i
        m =i
        if t < noofcols:
            for x in range(1,noofcols):
                m = nrow + m -(2*x-1)
                print(m, end=' ')       
            
        i = i+1
        print()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM