In: Computer Science
If you copy the functions below into your Python environment and execute q2([1,2,3]), you should see this printed output:
None [1, 2, 3, 6] [1, 2, 3, 6, 12] [1, 2, 3, 6] [1, 2, 3]
In terms of the initial value of the aList variable in q2, [1,2,3], explain each line of output. E.g. why is the first line None? Why is the second line [1,2,3,6]? Etc.
def q2a(inputL): s = 0 for i in range(len(inputL)): s = s + inputL[i] inputL.append(s) def q2b(inputL): s = 0 for item in inputL: s = s + item inputL = inputL + [s] return inputL def q2(aList): aList2 = aList[:] result = q2a(aList) print(result) print(aList) result2 = q2b(aList) print(result2) print(aList) print(aList2)
To explaining the each line of Output, we first examine the code line by line.
So as we see there are 3 functions and we are going to pass a LIST in the q2 function.
SO FIRST SEE THE q2a FUNCTION
def q2a(inputL): s = 0 ##initialize value of s to 0 for i in range(len(inputL)): ###iterate in the list s = s + inputL[i] ###adding all element in s inputL.append(s) ##appending the list with value of s
In this function we see that input list is passes as parameter which is [1,2,3]
initialize the value of s to 0, and then start iterating in the list and add the item in list to s
SoTHE VALUE OF S BECOME S=1+2+3=6
NOW THE aList become [1,2,3,6]
IN THIS CASE LAST ELEMENT IS UPDATE TO LIST GLOBALLY
AND THIS FUNCTION RETURN NOTHING.
Now lets see the second function.
def q2b(inputL): s = 0 ###initialize s with 0 for item in inputL: ##iter in input list s = s + item ##adding items to s inputL = inputL + [s] ##adding the input list with s return inputL ###returning the updated input list
In this function we also do the same thing but.
IN THIS CASE LST ELEMENT IS ADD INSIDE THE FUNCTION ONLY,IT DOES NOT REFLECT IN ORIGINAL LIST
we return the list in the last
Now lets see the q2 function
## here we are passing a list [1,2,3] in it.
then aList2 is making the copy of aList.
def q2(aList): ##HERE aList2 MAKE A COPY OF aList aList2 = aList[:] ## IN THE NEXT LINE q2a FNCTION IS CALLED AND aList IS PASSED TO IT result = q2a(aList) ### AS WE SEE THIS FUNCTION DOESN'T RETURN ANYTHING ONLY UPDATE THE LIST. ##SO IT DOESNOT PRINT ANYTHING TO NEXT LINE print(result) NONE ### SO THATS Y FIRST LINE IS PRINTED NONE. ## AS THE FUNCTION q2a UPDATE THE LAST VALUE TO LIST. ### SO THIS PRINT THE UPDATED LIST. print(aList) [1,2,3,6] ## NOW THE UPDATE LIST IS PASSED TO THE q2b FUNCTION WITH LIST [1,2,3,6] ## THE VALUE OF s IN THAT CASE IS s=1+2+3+4+6=12 ### 12 IS UPDATE IN LIST AND THE LIST IS RETURNED TO RESULT2 result2 = q2b(aList) print(result2) [1,2,3,6,12] ###RESULT2 PRINT THE UPDATED ARRAY. NOW NEXT LINE WILL PRINT THE aList print(aList) [1,2,3,6] ##THIS IS BECAUSE LAST ELEMENT IS NOT APPENDED IN THE ARRAY. ## NOW HERE WE ARE PRINTING THE aList2, WHICH WE MAKE THE COPY OF aList IN STARTUNG print(aList2) [1,2,3]
## THIS IS A COPY, SO ITS VALUE DOESNOT AFFECTED.
I Expalined all the output lines
None [1, 2, 3, 6] [1, 2, 3, 6, 12] [1, 2, 3, 6] [1, 2, 3]
FOR ANY MORE INFORMATION MESSAGE ME IN COMMENT.