Question

Task 3: Understanding Programs Given the initial statements s1 = [2,1,4,3] s2 = [’c’,’a’,’b’] show the...

Task 3: Understanding Programs
Given the initial statements
s1 = [2,1,4,3]
s2 = [’c’,’a’,’b’]
show the result of evaluating each of the following sequence expressions:
s1 + s2 =>
3 * s1 + 2 * s2 =>
s1[1] =>
s1[1:3] =>

s1 + s2[-1] =>


Given the same initial statements as in the previous problem, show the values of s1 and s2 after executing each of the following statements. Treat each part independently (i.e., assume that and start with their original values each time).
s1.remove(2) =>
s1.sort() =>
s1.append([s2.index(’b’)]) =>
s2.pop(s1.pop(2)) =>
s2.insert(s1[0], ’d’) =>

Using python please Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answers for your questions below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

s1 = [2,1,4,3]
s2 = [’c’,’a’,’b’]

s1 + s2 => will return [2, 1, 4, 3, 'c', 'a', 'b'] since + operator returns the union of two lists


3 * s1 + 2 * s2 => will return [2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 'c', 'a', 'b', 'c', 'a', 'b'] since * operator takes copy of each element in the list n times as specified. So 3*s1 will return 3*[2, 1, 4, 3] = [2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3] and 2 * s2 returns ['c', 'a', 'b', 'c', 'a', 'b'], and + operator returns the union of these two lists.


s1[1] => returns 1 since 1 is the element at index 1 in s1


s1[1:3] => returns [1, 4] (elements between index 1 and index 3 (exclusive))

s1 + s2[-1] => This will cause an exception since + operator is undefined for operands list and string. Here s1 is a list, and s2[-1] is a string (last element in s2), so these two operands can’t be added.

Given s1 = [2,1,4,3] and s2 = [’c’,’a’,’b’]

After calling s1.remove(2)

Value of s1 = [1, 4, 3] (2 has been removed)

Value of s2 = ['c', 'a', 'b']

Given s1 = [2,1,4,3] and s2 = [’c’,’a’,’b’]

After calling s1.sort()

Value of s1 = [1, 2, 3, 4] (sorted)

Value of s2 = ['c', 'a', 'b']

Given s1 = [2,1,4,3] and s2 = [’c’,’a’,’b’]

After calling s1.append([s2.index('b')])

Value of s1 = [2, 1, 4, 3, [2]]

Value of s2 = ['c', 'a', 'b']

Explanation: index method returns the index of an element in a list, here index of letter b in s2 is 2, this value is used to create a single element list (note the square braces surrounding s2.index(‘b’)) and this list is appended to S1

Given s1 = [2,1,4,3] and s2 = [’c’,’a’,’b’]

After calling s2.pop(s1.pop(2))

Value of s1 = [2, 1, 3]

Value of s2 = ['c', 'a', 'b']

The above statement will raise an exception.

Explanation. pop method removes an element from an index and return it. So s1.pop(2) will remove the element at index 2 in s1, which is 4 and pass it to the outer pop method (s2.pop(4)) but since 4 is not a valid index in s2, exception is occurred.

Given s1 = [2,1,4,3] and s2 = [’c’,’a’,’b’]

After calling s2.insert(s1[0], 'd')

Value of s1 = [2, 1, 4, 3]

Value of s2 = ['c', 'a', 'd', 'b']

Explanation: insert method adds an element (second parameter) to a specified index (first parameter). So s2.insert(s1[0], ‘d’) will insert ‘d’ into s2 at index s1[0] which is 2, remaining elements will be shifted right one place.

Add a comment
Know the answer?
Add Answer to:
Task 3: Understanding Programs Given the initial statements s1 = [2,1,4,3] s2 = [’c’,’a’,’b’] show the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Let S1 = { 1, 2, 3 }, S2 = { a, b }, S3 =...

    Let S1 = { 1, 2, 3 }, S2 = { a, b }, S3 = { 4, 5, 6 }. Show a B-tree of minimum degree t = 3 that contains the 18 tuple keys in S1 × S2 × S3, ordered by the linear order defined in (a). Assume that a <2 b in S2. please show the 18 tuple at first which is a cartesian product of s1,s2 and s3 and insert them into a B tree...

  • Given the following sequence of instructions: lw $s2, 0($s1) //1 lw $s1, 40($s3) //2 sub $s3,...

    Given the following sequence of instructions: lw $s2, 0($s1) //1 lw $s1, 40($s3) //2 sub $s3, $s1, $s2 //3 add $s3, $s2, $s2 //4 or $s4, $s3, $zero //5 sw $s3, 50($s1) //6 a. List the read after write (current instruction is reading certain registers which haven’t been written back yet) data dependencies. As an example , 3 on 1 ($s2) shows instruction 3 has data dependency on instruction 1 since it is reading register $s2. b. Assume the 5...

  • Consider the simplex tableau given below. X1 X2 S1 S2 Р 1 4 2 3 -...

    Consider the simplex tableau given below. X1 X2 S1 S2 Р 1 4 2 3 - 3 0 1 0 0 3 24 0 -6 0 1 0 (A) The pivot element is located in column and row (B) The entering variable is (C) The exiting variable is O. (D) Enter the values after one pivot operation in the tableau below. X1 X2 S1 S2 Р 1 2 0 3 0 -4 1 1 0 12 09 96 0 1...

  • 1) Let A and B be two programs that perform the same task. Let tA (n)...

    1) Let A and B be two programs that perform the same task. Let tA (n) and tB (n), respectively, denote their run times. For each of the following pairs, find the range of n values for which program A is faster than program B. Show the values for each and how you obtained them (justify). a) tA (n) =1000n , tB (n) =10n^2 b) tA (n) = 2n^2 , tB (n) = n ^3 c) tA (n) = 2^n...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • please show complete answers for a,b,c,d Find the solution to the given system that satisfies the given initial conditio...

    please show complete answers for a,b,c,d Find the solution to the given system that satisfies the given initial condition. -5 1 |x(t) 10 3 х'() 3 -3 1 (b) х(т) %—D (d) x(T/6) (с) x(- 2т)%3 (а) x(0) %3 -1 1 3 (а) x() 3D| | (Use parentheses to clearly denote the argument of each function.) Find the solution to the given system that satisfies the given initial condition. -5 1 |x(t) 10 3 х'() 3 -3 1 (b) х(т)...

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • Please help me with the coding for LL(1)!! The given grammar was: P → PL |...

    Please help me with the coding for LL(1)!! The given grammar was: P → PL | L L → N; | M; | C N → print E M → print "W" W → TW | ε C → if E {P} | if E {P} else {P} E → (EOE) | V (note: this has a variable O) O → + | - | * V → 0 | 1 | 2 | 3 (note: this has a terminal...

  • The molar volume in cm^3/mol of a binary liquid mixture at T and P is given by: V~ = 120 x1 + 70 x2 + (15 x1 + 8 x2) x1 x2 a.) Find expressions for the partial molar volumes of species 1 and 2 at T and P. b.) Show that when these expressions are combine

    The molar volume in cm^3/mol of a binary liquid mixture at T and P is given by:V~ = 120 x1 + 70 x2 + (15 x1 + 8 x2) x1 x2a.) Find expressions for the partial molar volumes of species 1 and 2 at T and P.b.) Show that when these expressions are combined in accord with Eqn 11.11 the given equation for V~ is recovered.c.) Show that these expressions satisfy Eqn 11.14, the Gibbs-Duhem equation.d.) Show that at constant...

  • Fit to page ID Page view A Re - + of the cropped tile, width (int)...

    Fit to page ID Page view A Re - + of the cropped tile, width (int) is the width of the cropped tile Retur ed image that is a 3D list of int Example crop ([ LLU ,1, ,2,2), (3,3,3]], [(4,4,4], [5,5,5),(6,6,6]], [[7,7,7], [8,8,8], [9,9,9]] ), (0,0), 1, 2) [[[1,1,1], [2,2,2]]] color2gray(image) Description: It takes a color image (3D list) and returns a new grayscale image (2D list) where each pixel will take the average value of the three color...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT