Question

In: Computer Science

what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back('...

what is the expected output of the following segment of code?

dq = Deque()
dq.push_back('M')
dq.push_back(' ')
l = ['A', 'L', 'E', 'S', 'T', 'E']
for i in l:
 if ord(i) % 3 == 0:
→ → dq.push_back(i)
elif ord(i) % 5 == 0:
→ → dq.push_front(i)
→ → dq.push_back(i)
elif ord(i) % 4 == 0:
→ → dq.push_back(i)
else:
→ → dq.peek_front()
→ → dq.push_front('X')
→ → dq.push_back('R')
dq.push_front(dq.peek_back())
dq.pop_back()
print(dq)

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
Output

deque(['E', 'X', 'A', 'M', ' ', 'A', 'L', 'E', 'R', 'T'])

The above is not compatible with python, so I made same program that would be compatible in python

from collections import deque  

dq = deque()
dq.append('M')
dq.append(' ')
l = ['A', 'L', 'E', 'S', 'T', 'E']
for i in l:
    if ord(i) % 3 == 0:
        dq.append(i)
    elif ord(i) % 5 == 0:
        dq.appendleft(i)
        dq.append(i)
    elif ord(i) % 4 == 0:
        dq.append(i)
    else:
        dq[0]
        dq.appendleft('X')
        dq.append('R')
dq.appendleft(dq[len(dq)-1])
dq.pop()
print(dq)


Related Solutions

21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
What would the output be for the following program code segment? #define ROWS  3      #define COLS 3...
What would the output be for the following program code segment? #define ROWS  3      #define COLS 3 void addCoulmns(int arr[][COLS],int Sum[COLS]); int main(void) {        int array[ROWS][COLS] = {{ 10, 35, 23 },                                   { 5, 4, 57 },                                   { 4, 7, 21 }};        int ColSum[ROWS];        int rows;        int columns;        addCoulmns(array,ColSum);        for (columns = 0; columns < COLS - 1; columns++)               printf("%d\n",ColSum[columns]);        return(0); } void addCoulmns(int arr[][COLS],int Sum[COLS]) {        int row;        int col;        for (col = 0; col < COLS -...
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
The following code segment is stored in memory starting at memory location 0x00445670. What are the...
The following code segment is stored in memory starting at memory location 0x00445670. What are the two possible values for the contents of the PC after the branch instruction has executed?       bgez $a0, skip                      # mem location: 0x00445670 subu $s2, $s1, $t0 # branch NOT taken (false) ori    $v0, $t1, 0x0003 #       skip: addi $t0, $t1, 2 # branch taken (true) if taken: if not taken: Hint: Remember how many bytes each instructions takes.
Show what is written by the following segment of code, given item1, item2, and item3 are...
Show what is written by the following segment of code, given item1, item2, and item3 are int variable: item1 = 4; item3= 0; item2 = item1 + 1; stack.Push(item2); stack.Push(item2 +1); stack.Push(item1); item2 = stack.Top(); stack.Pop(); item1 = item2 + 1; stack.Push(item1); Stack.Push(item3); while (!stack.IsEmpty()) {      item3 = stack.Top():      cout <<item3<<endl;      stack.Pop(); } cout<<item1 <<endl <<item2<<endl<<item3<<endl;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT