import math #********************************************************************** # TASK #1: Write code to create a list called Q containing the # square-roots of all numbers from 0 through 100, by 5s. #********************************************************************** #---------------------------------------------------------------------- # Version #1: Explicit counter loop to step through the desired # values, take the square root of each, then add the value to the # end of the list Q. #---------------------------------------------------------------------- Q = [] # initialize Q to an empty list Counter = 0 while Counter < 101: X = math.sqrt(Counter) Q = Q + [X] # Add old value of Q to new value X turned into a list Counter = Counter + 5 print (Q) #---------------------------------------------------------------------- # Version #2: Replace the counter loop with a for-loop that goes # through the same set of values. Notice that there is NO explicit # initialization of Counter to 0; that's all handled by the for-loop. #---------------------------------------------------------------------- Q = [] # initialize Q to an empty list for Counter in range(0,101,5): # Counter steps through list [0,5,10,15,20,...,100] X = math.sqrt(Counter) Q = Q + [X] # Add old value of Q to new value X turned into a list print (Q) #---------------------------------------------------------------------- # Version #3: Variable X is defined once and used only once, so it can # be eliminated. #---------------------------------------------------------------------- Q = [] for Counter in range(0,101,5): Q = Q + [math.sqrt(Counter)] print (Q) #---------------------------------------------------------------------- # Version #4: Now that the payload of the for-loop has been reduced # to a single line, that line can be brought up to the same line # as the for-loop itself. #---------------------------------------------------------------------- Q = [] for Counter in range(0,101,5): Q = Q + [math.sqrt(Counter)] print (Q) #********************************************************************** # TASK #2: Write a function called GetSQRT with one parameter N # that returns a list containing the square-roots of all numbers # from 0 through N-1. #********************************************************************** def GetSQRT (N): Q = [] for Counter in range(N): Q = Q + [math.sqrt(Counter)] return Q #********************************************************************** # TASK #3: Rewrite the Distance function to use lists instead of # explicit values. #********************************************************************** #---------------------------------------------------------------------- # Here is the original function, using explicit values for the # two points. For example, to compute the distance between points # <3,4> and <4,7> we would call Distance(3,4,4,7) #---------------------------------------------------------------------- def Distance1 (X1,Y1,X2,Y2): DeltaX = X2 - X1 DeltaY = Y2 - Y1 return math.sqrt(DeltaX*DeltaX + DeltaY*DeltaY) print (Distance1(3,4,4,7)) #---------------------------------------------------------------------- # Here is the updated function. P1 and P2 are now both lists of # two values, where the X value is in position 0 and the Y value is # in position 1. To compute the distance between <3,4> and <4,7> # we would call Distance([3,4], [4,7]), or we could assign the # points to variables as shown below. #---------------------------------------------------------------------- def Distance2 (P1,P2): # P1 and P2 are both [X,Y] lists DeltaX = P2[0] - P1[0] DeltaY = P2[1] - P1[1] return math.sqrt(DeltaX*DeltaX + DeltaY*DeltaY) P1 = [3,4] P2 = [4,7] print (Distance2(P1,P2))