gpt4 book ai didi

python - 在python中排列字符图

转载 作者:行者123 更新时间:2023-12-02 16:11:19 26 4
gpt4 key购买 nike

我正在尝试编写一段代码,根据用户输入的值制作前后斜线图

def setNums(num_filter):
if num_filter[0] != 0:
num_filter.insert(0,0)
else:
pass
arranged_values = [-value if counter%2 == 0 else value for counter,value in enumerate(num_filter)]
return arranged_values

def printLines(num_filter, total_lines):
su = 0
sd = 0
li = -1

for counter,num in enumerate(num_filter):
for z in range(num):
if (counter%2 != 0):
li += 1
total_lines[li] += (" "*su+"/")
try:
su = len(total_lines[li])-len(total_lines[li+1])
except IndexError:
pass
sd = 0

else:
total_lines[li] = total_lines[li]+(" "*sd+"\\")
sd = len(total_lines[li])-len(total_lines[li-1])
li -= 1
su = 0
return '\n'.join(total_lines[::-1])

def main():
numbers = input("Enter numbers (seperated by space): ")
num_filter = [int(num) for num in numbers.split()]
nums_arranged = setNums(num_filter)
total_lines = ['']*max([sum(nums_arranged[0:num:1]) for num in range(0, len(nums_arranged)+1)][1:]
output = printLines(num_filter, total_lines)
print ("Printing graph... \n", output)

当图表上升时效果很好,但当图表下降时,它会在图表顶部粘贴下斜线。

这段代码主要是让用户输入多个数字,比如2 3 4 2 4 1 3 6,然后用负号分隔奇数索引值和偶数索引值。然后用累积和,它预测所需的行(这就是我认为的问题所在),然后根据 numbers 的值,它按排列打印斜线。

我期望的输出是这样的:

The output I am expecting is like this

我得到的输出是这样的:

The output I am getting

最佳答案

您只考虑了图形可以有多高,而忘记衡量图形可以有多低。当您尝试查找具有负索引的行时,起点以下的行将移到顶部,有时行太低,这会使负索引超出图形的长度并引发 IndexError。

与你的不同,我为图表的行和列创建了一个列表列表,然后在每个循环中更改它们的值。

# find the highest and the lowest point of the graph
def height(nums):
highest = 0
lowest = 0
current = 0
step = 1
for num in nums:
current += num * step
if current > highest:
highest = current
if current < lowest:
lowest = current
step = step * -1
# highest - lowest is the height of the graph
# highest - 1 is the index of the lowest row of the positive part
return (highest - lowest, highest - 1)

def draw_graph(nums):
h, base_row = height(nums)
# create all rows and columns, sum(nums) is the width of the graph
rows = [[" "] * sum(nums) for i in range(h)]
row = base_row
col = 0
# -1 for go up, 1 for go down
step = -1
for num in nums:
slash = "/" if step == -1 else "\\"
for n in range(num):
rows[row][col] = slash
# move up or down based on step
row += step
# must move forward no matter step
col += 1
# turn around when finished a part
step *= -1
# adjustment as up and down slashes are on the same row when turn around
row += step
return "\n".join("".join(row) for row in rows)

def main():
nums = [int(i) for i in input("Enter numbers (seperated by space): ").split()]
graph = draw_graph(nums)
print("Printing graph... ")
print(graph)

if __name__ == '__main__':
main()

测试运行:

Enter numbers (seperated by space): 2 3 4 2 4 1 3 6
Printing graph...
/\
/ \
/\/ \
/ \
/\ / \
/\ / \/ \
/ \ /
\/

Enter numbers (seperated by space): 6 6 3 2 1 4 7 2
Printing graph...
/\
/ \ /\
/ \ / \
/ \ /\ /
/ \ / \/\ /
/ \/ \ /
\ /
\/

关于python - 在python中排列字符图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67948386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com