3
\$\begingroup\$

How could I make this code more effective? I'm just experimenting and learning the different ways of how Python runs.

list=[]
original=mkPoint3D(x,y)
z1=mkPoint3D(x-d,y)
z2=mkPoint3D(x+d,y)
z3=mkPoint3D(x,y-d)
z4=mkPoint3D(x,y+d)
list.append(original)
list.append(z1)
list.append(z2)
list.append(z3)
list.append(z4)

if(maxPoint(list).z==original.z):
    print("The local maximum is at " + "( " + str(original.x) + " , "  + str(original.y) + " )" + " = " + str(original.z))
elif maxPoint(list).z==z1.z:
    hillClimb(z1.x,z1.y,d)
elif maxPoint(list).z==z2.z:
    hillClimb(z2.x,z2.y,d)
elif maxPoint(list).z==z3.z:
    hillClimb(z3.x,z3.y,d)
elif maxPoint(list).z==z4.z:
    hillClimb(z4.x,z4.y,d)
\$\endgroup\$
4
\$\begingroup\$

Don't use "list" as a variable name, since it is a python function. Don't initialize it a list and append to it, if you can just create it and populate it with one line. Don't call a function more than once, if you don't need to. Use a loop with a break statement, instead of all the elif statements

original=mkPoint3D(x,y)
z1=mkPoint3D(x-d,y)
z2=mkPoint3D(x+d,y)
z3=mkPoint3D(x,y-d)
z4=mkPoint3D(x,y+d)

point_list = [original, z1, z2, z3, z4]

maxz = maxPoint(point_list).z

if maxz == original.z:
    print (" The local ...")

else:
    for next_point in point_list[1:]:
        if maxz == next_point.z:
            matching_point = next_point
            break

    hillClimb(matching_point.x, matching_point.y, d)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.