Question

Write a function called dequeue_element(queue, i) that takes a queue called queue and an index of...

Write a function called

dequeue_element(queue, i)

that takes a queue called queue and an index of an element in the queue, i. This function should get the element at index i of the queue and dequeue it but leave all of the other elements in the original order. The function should then return the dequeued value

For example, if queue = [A, B, C, D, E, F, G] and the function is called as:

dequeue_element(queue, 2)

Once the function is complete, queue = [A, B, D, E, F, G] and the function returns C. If i is out of range, raise a Exception. To get full credit, this function should not use any additional list-based data structures (stacks, lists, dequeues, strings etc).

in python

Homework Answers

Answer #1

CODE :

Explanation is avaliable in the comments

def dequeue_element(queue,index):
  
queue_len=len(queue) # length of the queue
  
if index < 0:
raise Exception("Given Index is out of range") # raising exception if index is out of range.
  
if index > queue_len:
raise Exception("Given Index is out of range") # raising exception if index is out of range.
  
else:
  
# avoiding slicing solution as it is creating another additonal list.
# only way is to traverse O(N).
  
for i in range(0,queue_len): # traversing till the index found.
if index == i:
break;
  
queue_element_removed = queue[i] #element which is to be removed
  
# relocating elements
  
for j in range(i+1,queue_len):
queue[j-1]=queue[j]
  
queue.pop() # removes last element
  
return queue

queue = ['A', 'B' , 'C', 'D' , 'E', 'F', 'G']

index=2

# Function calling

print(dequeue_element(queue,index))

print(dequeue_element(queue,1))
print(dequeue_element(queue,5))
print(dequeue_element(queue,0))

Output :

Know the answer?
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
Similar Questions
Please write the python code for a function called called count_all_words that takes a phrase as...
Please write the python code for a function called called count_all_words that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. (See example below.) Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
asap python: Write a function called fileCaseSwap that takes 2 filenames (strings) as input parameters. The...
asap python: Write a function called fileCaseSwap that takes 2 filenames (strings) as input parameters. The function should: Open the first file for reading Open the second file for writitng Read the first file Swap the case (swapcase) of every character in the file Write the replaced text to the second file. test call: fileCaseSwap("emma-short.txt", "emma-short-swap.txt") fileCaseSwap("emma.txt", "emma-swap.txt") """ """ The first 3 lines of the emma-short-swap.txt file should be emma bY jANE aUSTEN
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT