[英]How to print following pattern getting issue with spaces?
我正在嘗試編寫代碼但沒有得到如何實現預期的 output 導致空間問題並且無法正確判斷如何在每次迭代后獲得精確的空間
我的代碼:
n=15
cnt=0
lst=[str(' ') for x in range(1,n+1)]
initial_length=len(''.join(lst))
print(initial_length)
for row in range(1,n+1):
lst[cnt-1]=str(row)
cnt=cnt-1
print(' '.join(lst))
上述代碼的 Output 與預期不符 output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
預期 output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
您必須考慮較大的數字占用更多空間,這意味着在創建包含空格的列表時,您需要將空間乘以該數字中的位數,您可以通過len(str(number))
獲得:
n = 15
# create a list that contains spaces of length of how many digits
# will be put there, then reverse it because ascending order and convert
# to list
lst = list(reversed([' ' * len(str(x)) for x in range(1, n + 1)]))
# go over each number
for x in range(1, n + 1):
# create a copy of the list
c = lst.copy()
# create a reversed object containing all the numbers
# up until x
numbers = reversed([str(x) for x in range(1, x + 1)])
# replace the last items in list's copy with the reversed order
# so basically put the numbers at the end
c[-x:] = numbers
# join by space and print
print(' '.join(c))
還:
我強烈建議遵循PEP 8 - Python 代碼樣式指南。 Function 和變量名應該在snake_case
中,class 名稱應該在CapitalCase
中。 如果將=
用作關鍵字參數( func(arg='value')
)的一部分,則不要在周圍有空格,但如果將=
用於分配值( variable = 'some value'
),則在 = 周圍有空格。 在運算符周圍留出空間( +-/
等: value = x + y
(此處除外value += x + y
))。 在 function 和 class 聲明周圍有兩個空行。 Object 方法定義周圍有一個空行。
另一個近似值,通過使用遞歸 function 預先知道空格的數量:
def findDigits(N):
if N <= 1:
return N
# Changing number to string
s = str(N)
# Add length of number to total_sum
return len(s) + findDigits(N - 1)
def print_inverse_pyramid(n):
# Calculate number of total digits until n
total_digits = findDigits(n)
# Print the pyramid
for row in range(1, n + 1):
total_digits -= len(str(row))
l_r = [str(i) for i in range(row, 0, -1)]
print(" " * (total_digits + (n - row)) + " ".join(l_r))
print_inverse_pyramid(15)
又快又臟:記住琴弦
n = 15
outlist = [' '.join([str(j) for j in range(i, 0, -1)]) for i in range(1, n +1 )]
outlist_with_len = [(l,len(l)) for l in outlist]
maxlen = max([v for l,v in outlist_with_len])
for l, v in outlist_with_len:
print(" " * (maxlen - v) + l)
可能這可以幫助您解決空間問題。 參考作者@Matiiss
代碼:
def print_number_pyramid(n):
# Getting list printed with only required spaces : [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
get_spaces=[ len(str(x)) * ' ' for x in reversed(range(1,n+1))]
# A counter variable which will be used as a update strategy
cnt=-1
for row in range(1,n+1):
# When first time the cnt value is set to -1 which will update the last value of get_spaces list.
# Then incrementing with -1 in every iteration. It try to update values of list in reverse order one by one
get_spaces[cnt]=str(row)
print(' '.join(get_spaces))
cnt = cnt -1
print_number_pyramid(20)
Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.