Question

In: Computer Science

If you copy the functions below into your Python environment and execute q2([1,2,3]), you should see...

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)

Solutions

Expert Solution

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.


Related Solutions

Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
Governance,_risk_management,_and_compliance (business legal environment) 1) What role to you see for compliance functions in the next...
Governance,_risk_management,_and_compliance (business legal environment) 1) What role to you see for compliance functions in the next 5 to 10 years in the U.S. legal environment of business?
What should be put in the blank position below in order to make the loop execute...
What should be put in the blank position below in order to make the loop execute exactly 5 times? (C program) int i ; for ( i = 10 ; ______ ; i-- ){   printf ( "%d\n", i ) ; }
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi *...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi * parameters: none * returns: N/A * operation: just say "hi" when called. * expected output: >>> say_hi() hi * function name: personal_hi * parameters: name (string) * returns: N/A * operation: Similar to say_hi, but you should include the name argument in the greeting. * expected output: >>> personal_hi("Samantha") Hi, Samantha * function name: introduce * parameters: name1 (string) name2 (string) * returns: N/A...
Python 3 Functions that give answers Define and complete the functions described below. * function name:...
Python 3 Functions that give answers Define and complete the functions described below. * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. * expected output: JUST RETURNS THE NAME...TO VIEW IT YOU CAN PRINT IT AS BELOW >>> print(get_name()) John * function name: get_full_name * parameters: fname (string) lname (string) first_last (boolean) * returns: string * operation: Return (again, NOT print) the full name based on the first...
you may not use the Python ord() or chr() functions you may not use the Python...
you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions You will write a total of four functions, each of which will take two inputs and return a string: c_encrypt() c_decrypt() vig_encrypt() vig_decrypt() The first argument will be a string containing the plaintext (or clear text) message to be encrypted for the two encrypt functions, and a string containing a...
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
Part I– Check your file system Step 1: Execute the ls /dev/sd* command to see the...
Part I– Check your file system Step 1: Execute the ls /dev/sd* command to see the current hard disk devices. Step 2. Execute the fdisk -l command to list the current hard disk partitions Step 3. Execute the parted -l command to list the current hard disk partition table. Part II– Create a new virtual disk Step 1. In the Oracle VM VirtualBox Manager program, create a new virtual hard disk with the size of 200 MB, and name it...
Project #2 ## Language Should be Python. Using for, while, if, elif only. No advanced functions....
Project #2 ## Language Should be Python. Using for, while, if, elif only. No advanced functions. Assignment Specifications The program will compute and display information for a company which rents vehicles to its customers. For a specified customer, the program will compute and display the amount of money charged for that customer’s vehicle rental. The program should start by asking the user if he wants to continue. The answer is ‘Y’ for yes or ‘N’ for no. The basic structure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT