Which of the following best translates a potentially negative index idx into a p
ID: 3741078 • Letter: W
Question
Which of the following best translates a potentially negative index idx into a positive offset into a list data structure (containing self.count elements), per Python’s regular array index semantics?
(a) idx = (abs(idx) + self.count) % self.count) (b) idx = idx + (-self.count) (c) if idx < 0: idx = idx + self.count (d) if idx < 0: idx = self.count - idx
Given that self.head refers to the sentinel head link of a circular, doubly-linked list implementation, which choice completes the following function so that it removes just the last occurrence of x from the list?
def remove_last(self, x):
____________________________________
(a) n = self.head.prior
while n.val != x:
n.next = n.next
n.prior = n.prior
n = n.prior
(b) n = self.head.next
while n is not self.head:
if n.val == x:
n.prior.next = n.next
n.next.prior = n.prior
return n = n.next
(c) n = self.head.prior
while n is not self.head:
if n.val == x:
n.prior.next = n.next
n.next.prior = n.prior
return n = n.prior
(d) n = self.head.next
while n.val != x:
n = n.next
while n is not self.head:
if n.val == x:
break
n = n.next
n.prior.next = n.next
n.next.prior = n.prior
Explanation / Answer
Q) Which of the following best translates a potentially negative index idx into a positive offset into a list data structure (containing self.count elements), per Python’s regular array index semantics?
(a) idx = (abs(idx) + self.count) % self.count) (b) idx = idx + (-self.count) (c) if idx < 0: idx = idx + self.count (d) if idx < 0: idx = self.count - idx
Answer: (a) idx = (abs(idx) + self.count) % self.count)
Explanation:
Rest all options does not take care of cases when the absolute value of idx is more than self.count. In such a case, options b), c) and d) will give incorrect index value.
Q) Given that self.head refers to the sentinel head link of a circular, doubly-linked list implementation, which choice completes the following function so that it removes just the last occurrence of x from the list?
def remove_last(self, x):
Answer:
(d) n = self.head.next
while n.val != x:
n = n.next
while n is not self.head:
if n.val == x:
break
n = n.next
n.prior.next = n.next
n.next.prior = n.prior
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.