簡體   English   中英

如何簡化我的代碼?

[英]How to simplify my codes?

我為一個類編寫了一個程序,該程序使用遞歸來模仿某些簡單的分支結構(例如樹)。 在我向教授展示之前,我認為自己的代碼很棒。 他說我的代碼太復雜了,說我需要簡化它。 除了將它們分開,我不確定還能做什么。 有小費嗎? (我是一個初學者,所以請隨便吧。)該程序創建多個樹,它們的厚度,分支數和坐標都不同。

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()

對於我來說,這段代碼相當扎實,尤其是對於Python初學者而言。 我見過更糟。

如果我正在編寫它,我想我只會在main for循環內計算number2number3 -如您在此處的number3定義通常對於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調用-如果您願意,可以進行簡單的優化重新使用Python2.x。 如果您使用的是Python 3,則range是正確的。 但是這些都是微小的變化。

您還可以在range(branches)循環之前插入if子句,以至於即使depth等於1也不嘗試-這是另一個次要的優化,盡管不會帶來很大的不同。

在我向教授展示之前,我認為自己的代碼很棒。 他說我的代碼太復雜了,說我需要簡化它。

考慮到繪制的樹的質量,這是相當復雜的代碼:

在此處輸入圖片說明

在編寫程序的隨機參數范圍內,僅繪制垂直線和空白屏幕! 讓我們重新設計該程序,將一些隨機性從靜態配置代碼移到遞歸例程本身中。 我們還將微調隨機范圍並清理代碼,主要是通過消除僅設置和使用一次的變量:

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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM