【问题标题】:How to simplify my codes?如何简化我的代码?
【发布时间】:2013-10-08 03:17:43
【问题描述】:

我为一个类编写了一个程序,它使用递归来模拟某些类型的简单分支结构,例如树。在我向教授展示之前,我认为我的代码很棒。他告诉我的代码太复杂了,并说我需要简化它。除了将它们分开之外,我不确定我还能做什么。有小费吗? (我是一个初学者,所以放轻松。)这个程序创建了多个具有不同厚度、分支数量和不同坐标的树。

import random 
import turtle
##I'm using a python module called turtle to visualize results
p1 = turtle.Pen()
##Creates a pen
p1.tracer(True)
## Shows pen drawing
p1.up()
p1.left(90)

d=random.randint(0,2)
## Varying thickness of branch
length=150
##Length of branches
contract=random.uniform(.5,1)
## Varying degree of contraction
branch=random.randint(5,8)
## Varying amount of branches
first=random.randint(30,70)
## Varying first degree of branch
next=random.randint(1,30)
## Varying degree between each branches
number1=random.randint(10,20)
number2=random.randint(-100,100)
number3=random.randint(-100,100)
# Range of numbers used for coordinates 
def drawFern1(pen, depth, length, contractBy, branches, firstBranchAngle, nextBranchAngle):
    if depth > 0:
       #Pen's Position and heading
       heading = pen.heading()
       position = pen.position()
       pen.width(depth)
       pen.forward(length)
       pen.left(firstBranchAngle)
       for i in range(branches):
        drawFern1(pen, depth-1, contractBy*length, contractBy,branches,firstBranchAngle,nextBranchAngle)
        pen.right(nextBranchAngle)
      pen.setheading(heading)
      pen.setposition(position)
# Ensures that multiple trees are created each at different coordinates. 
for i in range(number1):
   p1.sety(number2)
   p1.setx(number3)
   p1.down()
   drawFern1(p1,d,length,contract,branch,first,next)
   number2 = random.randint(-100,100)
   number3 = random.randint(-100,100)
   p1.up()

【问题讨论】:

  • 您应该做的第一件事是添加cmets来解释代码的作用。
  • 这段代码一点也不“复杂”。我什至不知道如何使用该语言正确编码,尽管我仍然可以立即完美地阅读您的代码。我的看法是你的教授在问你一个“诡计”的问题。
  • 嗯,我教授的英语不是很好。我想他的意思是说这太令人费解了?
  • 老实说,这段代码没有太多不必要的地方。你应该让你的教授澄清他们的意思。

标签: python recursion turtle-graphics simplify


【解决方案1】:

这段代码对我来说看起来很可靠,尤其是对于 Python 初学者。我见过更糟糕的情况。

如果我正在编写它,我想我只会在 for 主循环内计算 number2number3 - 您在此处使用的启动定义通常对于 while 循环很方便,但不是在这种情况下是必要的。我还会尝试使用更多解释性变量名称,并且根据问题陈述,我可能要求随机生成的 depth 值至少为 1 - 如果 depth 生成为 0,则不会绘制任何内容。

我的版本如下所示:

import random 
import turtle

def drawFern(pen, depth, length, contraction, branches, firstBranchAngle, nextBranchAngle):
    if depth > 0:
        # Pen's Position and heading
        heading = pen.heading()
        position = pen.position()
        pen.width(depth)
        pen.forward(length)
        pen.left(firstBranchAngle)
        for i in xrange(branches):
            drawFern(pen, depth-1, contraction*length, contraction, branches, firstBranchAngle, nextBranchAngle)
            pen.right(nextBranchAngle)
        pen.setheading(heading)
        pen.setposition(position)

# I'm using a python module called turtle to visualize results
# Creates a pen
pen = turtle.Pen()
#  Shows pen drawing
pen.tracer(True)
pen.up()
pen.left(90)

# Configure initial state
# Varying depth of recursive fern
depth = random.randint(1,2)
# Length of branches
length = 150
# Varying degree of contraction
contraction = random.uniform(.5,1)
# Varying number of branches
branches = random.randint(5,8)
# Varying first degree of branch
first_angle = random.randint(30,70)
#  Varying degree between each branches
next_angle = random.randint(1,30)

number_of_trees =random.randint(10,20)

for i in xrange(number_of_trees):
    new_x = random.randint(-100, 100)
    new_y = random.randint(-100, 100)
    pen.setx(new_x)
    pen.sety(new_y)   
    pen.down()
    drawFern(pen, depth, length, contraction, branches, first_angle, next_angle)
    pen.up()

除了将 x 和 y 坐标随机化移动到主循环中,将递归函数定义移动到文件的前面,并使用一些更明确的变量名称,我还使用了 xrange 调用而不是 range 调用- 如果您使用的是 Python 2.x,则进行微不足道的优化。如果您使用的是 Python 3,range 是正确的。但这些都是微小的变化。

您也可以在 range(branches) 循环之前添加一个 if 子句,即使 depth 等于 1 也不要尝试 - 这是另一个小的优化,尽管不会产生很大的不同。

【讨论】:

    【解决方案2】:

    在向教授展示之前,我认为我的代码很棒。他告诉我 代码太复杂,说要简化一下。

    考虑到它绘制的树的质量,这是相当复杂的代码:

    仅绘制垂直线和空白屏幕都在编写的程序的随机参数内!让我们重新编写程序,将一些随机性从静态配置代码转移到递归例程本身。我们还将微调随机范围并清理代码,主要是通过消除仅设置和使用一次的变量:

    from random import randint, uniform
    from turtle import Screen, Pen  # Using python turtle module to visualize results
    
    # Configure initial state
    
    DEPTH = randint(3, 4)  # Varying thickness and splitting of branches
    LENGTH = randint(125, 150)  # Length of branches
    CONTRACT_BY = uniform(0.4, 0.8)  # Varying degree of contraction
    
    def drawFern(pen, depth, length, contractBy):
        if depth < 1:
            return
    
        # Save pen's position and heading
        heading = pen.heading()
        position = pen.position()
    
        pen.width(depth * 1.5)  # pen thickness depends on branching
        pen.forward(length)
        pen.left(randint(30, 70)) # Varying first degree of branch)
    
        for _ in range(randint(5, 8)):  # Varying amount of branches
            drawFern(pen, depth - 1, contractBy * length, contractBy)
            pen.right(randint(5, 30))  # Varying degree between each branches
    
        # Restore pen's Position and heading
        pen.setheading(heading)
        pen.setposition(position)
    
    screen = Screen()
    
    pen = Pen(visible=False)
    pen.left(90)
    
    screen.tracer(False)
    # Ensure that multiple trees are created each at different coordinates.
    for i in range(randint(10, 20)):
        pen.penup()
        pen.setposition(randint(-200, 200), randint(-300, 0))
        pen.pendown()
        drawFern(pen, DEPTH, LENGTH, CONTRACT_BY)
    screen.tracer(True)
    
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多