(Solved) : Piu Lou Redum Laboratory Allivilius 0000000000 000000000000 000 Activity References 1 Wn P Q43946832 . . .

![ACTIVITY: References 2 2 print(Example: Copying Lists) x = [1, 2, 3] y = x[:] # copies the list print(before, x, y) y [0]](https://media.cheggcdn.com/media/2a6/2a6cd272-7573-4c01-b8af-bd284ca17056/phpsyTcx4.png)
![ACTIVITY: References 3 WN 4 print(Example: List expressions) x = [1, 2, 3] y = [4, 5, 6] z = x + y print (before, x, y, z](https://media.cheggcdn.com/media/998/998b0f93-791a-4546-9e58-fc1622f7de0f/phpuWB7JJ.png)
![ACTIVITY: References 4 print(Example: multi-dimensional lists) x = [[0, 1, 2], [5, 6, 7]] 4 y = [[10, 11, 12], [15, 16, 17]](https://media.cheggcdn.com/media/7cc/7ccab68d-b645-42c4-923e-b47789b375b3/phpVVS2B4.png)
![ACTIVITY: References 5 print(Example: list products) WN 4 6 x = [1, 2, 3] + 3 print(before, x) x[0] = 10 print(after ,x](https://media.cheggcdn.com/media/926/926d4e84-e1e5-42d7-90f5-ddfd99d424a0/phpORzNaj.png)
![ACTIVITY: References 6 print(Example: list products copy references) 3 OUAWN x = [[0, 1, 2]] * print(before, x) x[0][0] =](https://media.cheggcdn.com/media/b8e/b8ef1d13-4681-411f-a399-54c9f869587c/phpuvBlXM.png)


![ACTIVITY. Reierelles print (Example: functions and and mutable arguments) def swap_in_list(a_list, i, j): tmp = a_list [1] a_](https://media.cheggcdn.com/media/418/41866ff8-e6e2-4dbc-a5e0-0bed05237aab/phpSzAnO9.png)



![uniques.append(item) return duplicates # a global variable duplicates = []](https://media.cheggcdn.com/media/7c7/7c788ac2-0d98-4e2d-9d52-62165d84d115/phpauUTzv.png)
![#test_scope.pl # test case inputs = [1, 2, 3] expected = [1 reason - Longer list with no duplicates result - scope.find_dupl](https://media.cheggcdn.com/media/888/888819ee-e9de-4fac-8931-f480a68d3afd/phpVIFpwT.png)
PIU-LOU Redum Laboratory ALLIVILIUS 0.000000000 000000000000 000 ACTIVITY: References 1 WN print(‘Example: Copying References’) x = [1, 2, 3] y = x print(‘before’, x, y) y [0] = 100 print(‘after ‘, x, y) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Does the change to y on line 5 affect x? Explain why or why not. ACTIVITY: References 2 2 print(‘Example: Copying Lists’) x = [1, 2, 3] y = x[:] # copies the list print(‘before’, x, y) y [0] = 100 print(‘after ‘, x, y) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Does the change to y on line 5 affect x? Explain why or why not. ACTIVITY: References 3 WN 4 print(“Example: List expressions”) x = [1, 2, 3] y = [4, 5, 6] z = x + y print (“before’, x, y, z) x[0] = 100 y [0] = 999 print(‘after ‘, x, y, z) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Do the changes to x and y on lines 6-7 affect z? Explain why or why not. ACTIVITY: References 4 print(‘Example: multi-dimensional lists’) x = [[0, 1, 2], [5, 6, 7]] 4 y = [[10, 11, 12], [15, 16, 17]] 5z = x + y 6 print (‘before’, x, y, z) x[0][0] = 999 # change first element of first sublist 8 y [O] [0] = 100 # change first element of first sublist print(‘after ‘, x, y, z) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Do the changes to x and y on lines 7-8 affect z? Explain why or why not. ACTIVITY: References 5 print(‘Example: list products”) WN 4 6 x = [1, 2, 3] + 3 print(‘before’, x) x[0] = 10 print(‘after ‘,x) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: What does the change to x on line 5 do to x’s value? Explain. ACTIVITY: References 6 print(‘Example: list products copy references’) 3 OUAWN x = [[0, 1, 2]] * print(‘before’, x) x[0][0] = 10 print (after ‘, x) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: What does the change to x on line 5 do to x’s value? Explain. ACTIVITY: References 7 print(‘Example: functions and parameters’) WN TO def change-params (a): a = a – 1 return a 7a = 5 8 b = 10 print(‘before’, a, b) 10 b = change-params (a) 11 print(‘after ‘, a, b) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: How does line 10 affect the global variables a and b. Explain. 0000000000 000000000000 Ооо ACTIVITY: References 8 print(‘Example: functions and and immutable arguments’) 500 OWN def swap-ints (x, y): print(‘inside swap_ints(), before’, x, y) tmp = x x = y y = tmp print(‘inside swap_ints(), after ‘, x, y) 11 12 a = 3 b = 4 print(‘global scope, before’, a, b) swap_ints (a, b) print(‘global scope, after’, a, b) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Did variables a and b get swapped? Explain. CMPT145 Lab 02. References, and Scope ACTIVITY. Reierelles print (Example: functions and and mutable arguments) def swap_in_list(a_list, i, j): tmp = a_list [1] a_list [1] = a_list [2] a_list [2] = tmp TO009 some_list = [‘a’, ‘list’, ‘of’, ‘words’] print(‘before’, some_list) swap_in_list (some_list, 1, 2) print(‘after ‘, some_list) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Did the function call on line 10 affect the list some_list? Handin for References Activities • Collect your one sentence answers, and place them in a file named labo2-responses.txt • Example: | Activity References 1: There is only one list, with two references to it (variables x and y), and the assignment on line 5 changed this list. MOOO Activity References 2: Line 3 created a copy of the list, and the assignment statement on line 5 changes the copy, but not the original. (etc) • Hand-in the file lab02-responses.txt Scope ACTIVITY • Download the files scope.py and test_scope.py from Labo2 on Moodle. • Study the code in both files. • Run the test script. Observe the errors! Don’t fix them yet. • Add at least 4 new test cases to the test script. • Re-order your tests. You’ll get different reports! • Copy/paste the output of your test script, showing errors to the lab02-transcript.txt file. def find_duplicates (alist): Purpose: Return a list of duplicate values found in alist. Pre: alist: a list of values Post: None Return: A list of duplicates from alist uniques = [] for item in alist: if item in uniques: duplicates.append(item) else: uniques.append(item) uniques.append(item) return duplicates # a global variable duplicates = [] #test_scope.pl # test case inputs = [1, 2, 3] expected = [1 reason – Longer list with no duplicates’ result – scope.find_duplicates(inputs) if result != expected: print(‘Error in find_duplicates: expected’, expected, ‘but obtained’, result, ‘–‘, reason) print(“*** Test script finished ***’) Show transcribed image text PIU-LOU Redum Laboratory ALLIVILIUS 0.000000000 000000000000 000 ACTIVITY: References 1 WN print(‘Example: Copying References’) x = [1, 2, 3] y = x print(‘before’, x, y) y [0] = 100 print(‘after ‘, x, y) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Does the change to y on line 5 affect x? Explain why or why not.
ACTIVITY: References 2 2 print(‘Example: Copying Lists’) x = [1, 2, 3] y = x[:] # copies the list print(‘before’, x, y) y [0] = 100 print(‘after ‘, x, y) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Does the change to y on line 5 affect x? Explain why or why not.
ACTIVITY: References 3 WN 4 print(“Example: List expressions”) x = [1, 2, 3] y = [4, 5, 6] z = x + y print (“before’, x, y, z) x[0] = 100 y [0] = 999 print(‘after ‘, x, y, z) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Do the changes to x and y on lines 6-7 affect z? Explain why or why not.
ACTIVITY: References 4 print(‘Example: multi-dimensional lists’) x = [[0, 1, 2], [5, 6, 7]] 4 y = [[10, 11, 12], [15, 16, 17]] 5z = x + y 6 print (‘before’, x, y, z) x[0][0] = 999 # change first element of first sublist 8 y [O] [0] = 100 # change first element of first sublist print(‘after ‘, x, y, z) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Do the changes to x and y on lines 7-8 affect z? Explain why or why not.
ACTIVITY: References 5 print(‘Example: list products”) WN 4 6 x = [1, 2, 3] + 3 print(‘before’, x) x[0] = 10 print(‘after ‘,x) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: What does the change to x on line 5 do to x’s value? Explain.
ACTIVITY: References 6 print(‘Example: list products copy references’) 3 OUAWN x = [[0, 1, 2]] * print(‘before’, x) x[0][0] = 10 print (after ‘, x) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: What does the change to x on line 5 do to x’s value? Explain.
ACTIVITY: References 7 print(‘Example: functions and parameters’) WN TO def change-params (a): a = a – 1 return a 7a = 5 8 b = 10 print(‘before’, a, b) 10 b = change-params (a) 11 print(‘after ‘, a, b) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: How does line 10 affect the global variables a and b. Explain.
0000000000 000000000000 Ооо ACTIVITY: References 8 print(‘Example: functions and and immutable arguments’) 500 OWN def swap-ints (x, y): print(‘inside swap_ints(), before’, x, y) tmp = x x = y y = tmp print(‘inside swap_ints(), after ‘, x, y) 11 12 a = 3 b = 4 print(‘global scope, before’, a, b) swap_ints (a, b) print(‘global scope, after’, a, b) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Did variables a and b get swapped? Explain. CMPT145 Lab 02. References, and Scope
ACTIVITY. Reierelles print (Example: functions and and mutable arguments) def swap_in_list(a_list, i, j): tmp = a_list [1] a_list [1] = a_list [2] a_list [2] = tmp TO009 some_list = [‘a’, ‘list’, ‘of’, ‘words’] print(‘before’, some_list) swap_in_list (some_list, 1, 2) print(‘after ‘, some_list) 1. Predict the console output. 2. Draw the frame and heap diagram, showing all the variables and values in the snippet. 3. Question: Did the function call on line 10 affect the list some_list?
Handin for References Activities • Collect your one sentence answers, and place them in a file named labo2-responses.txt • Example: | Activity References 1: There is only one list, with two references to it (variables x and y), and the assignment on line 5 changed this list. MOOO Activity References 2: Line 3 created a copy of the list, and the assignment statement on line 5 changes the copy, but not the original. (etc) • Hand-in the file lab02-responses.txt
Scope ACTIVITY • Download the files scope.py and test_scope.py from Labo2 on Moodle. • Study the code in both files. • Run the test script. Observe the errors! Don’t fix them yet. • Add at least 4 new test cases to the test script. • Re-order your tests. You’ll get different reports! • Copy/paste the output of your test script, showing errors to the lab02-transcript.txt file.
def find_duplicates (alist): Purpose: Return a list of duplicate values found in alist. Pre: alist: a list of values Post: None Return: A list of duplicates from alist uniques = [] for item in alist: if item in uniques: duplicates.append(item) else: uniques.append(item)
uniques.append(item) return duplicates # a global variable duplicates = []
#test_scope.pl # test case inputs = [1, 2, 3] expected = [1 reason – Longer list with no duplicates’ result – scope.find_duplicates(inputs) if result != expected: print(‘Error in find_duplicates: expected’, expected, ‘but obtained’, result, ‘–‘, reason) print(“*** Test script finished ***’)
Expert Answer
Answer to PIU-LOU Redum Laboratory ALLIVILIUS 0.000000000 000000000000 000 ACTIVITY: References 1 WN print(‘Example: Copying Refer…
OR