Difference between "enqueue" and "dequeue" - queue

Can somebody please explain the main differences? I don't have a clear knowledge about these functions in programming for any language.

Some of the basic data structures in programming languages such as C and C++ are stacks and queues.
The stack data structure follows the "First In Last Out" policy (FILO) where the first element inserted or "pushed" into a stack is the last element that is removed or "popped" from the stack.
Similarly, a queue data structure follows a "First In First Out" policy (as in the case of a normal queue when we stand in line at the counter), where the first element is pushed into the queue or "Enqueued" and the same element when it has to be removed from the queue is "Dequeued".
This is quite similar to push and pop in a stack, but the terms enqueue and dequeue avoid confusion as to whether the data structure in use is a stack or a queue.
Class coders has a simple program to demonstrate the enqueue and dequeue process. You could check it out for reference.
http://classcoders.blogspot.in/2012/01/enque-and-deque-in-c.html

Enqueue and Dequeue tend to be operations on a queue, a data structure that does exactly what it sounds like it does.
You enqueue items at one end and dequeue at the other, just like a line of people queuing up for tickets to the latest Taylor Swift concert (I was originally going to say Billy Joel but that would date me severely).
There are variations of queues such as double-ended ones where you can enqueue and dequeue at either end but the vast majority would be the simpler form:
+---+---+---+
enqueue -> | 3 | 2 | 1 | -> dequeue
+---+---+---+
That diagram shows a queue where you've enqueued the numbers 1, 2 and 3 in that order, without yet dequeuing any.
By way of example, here's some Python code that shows a simplistic queue in action, with enqueue and dequeue functions. Were it more serious code, it would be implemented as a class but it should be enough to illustrate the workings:
import random
def enqueue(lst, itm):
lst.append(itm) # Just add item to end of list.
return lst # And return list (for consistency with dequeue).
def dequeue(lst):
itm = lst[0] # Grab the first item in list.
lst = lst[1:] # Change list to remove first item.
return (itm, lst) # Then return item and new list.
# Test harness. Start with empty queue.
myList = []
# Enqueue or dequeue a bit, with latter having probability of 10%.
for _ in range(15):
if random.randint(0, 9) == 0 and len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
else:
itm = 10 * random.randint(1, 9)
myList = enqueue(myList, itm)
print(f"Enqueued {itm} to give {myList}")
# Now dequeue remainder of list.
print("========")
while len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
A sample run of that shows it in operation:
Enqueued 70 to give [70]
Enqueued 20 to give [70, 20]
Enqueued 40 to give [70, 20, 40]
Enqueued 50 to give [70, 20, 40, 50]
Dequeued 70 to give [20, 40, 50]
Enqueued 20 to give [20, 40, 50, 20]
Enqueued 30 to give [20, 40, 50, 20, 30]
Enqueued 20 to give [20, 40, 50, 20, 30, 20]
Enqueued 70 to give [20, 40, 50, 20, 30, 20, 70]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20, 20]
Dequeued 20 to give [40, 50, 20, 30, 20, 70, 20, 20]
Enqueued 80 to give [40, 50, 20, 30, 20, 70, 20, 20, 80]
Dequeued 40 to give [50, 20, 30, 20, 70, 20, 20, 80]
Enqueued 90 to give [50, 20, 30, 20, 70, 20, 20, 80, 90]
========
Dequeued 50 to give [20, 30, 20, 70, 20, 20, 80, 90]
Dequeued 20 to give [30, 20, 70, 20, 20, 80, 90]
Dequeued 30 to give [20, 70, 20, 20, 80, 90]
Dequeued 20 to give [70, 20, 20, 80, 90]
Dequeued 70 to give [20, 20, 80, 90]
Dequeued 20 to give [20, 80, 90]
Dequeued 20 to give [80, 90]
Dequeued 80 to give [90]
Dequeued 90 to give []

These are terms usually used when describing a "FIFO" queue, that is "first in, first out". This works like a line. You decide to go to the movies. There is a long line to buy tickets, you decide to get into the queue to buy tickets, that is "Enqueue". at some point you are at the front of the line, and you get to buy a ticket, at which point you leave the line, that is "Dequeue".

A queue is a certain 2-sided data structure. You can add new elements on one side, and remove elements from the other side (as opposed to a stack that has only one side). Enqueue means to add an element, dequeue to remove an element. Please have a look here.

Enqueue means to add an element, dequeue to remove an element.
var stackInput= []; // First stack
var stackOutput= []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while(stackInput.length > 0) {
var elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}

In my opinion one of the worst chosen word's to describe the process, as it is not related to anything in real-life or similar. In general the word "queue" is very bad as if pronounced, it sounds like the English character "q". See the inefficiency here?
enqueue: to place something into a queue; to add an element to the tail of a queue;
dequeue to take something out of a queue; to remove the first available element from the head of a queue
source: https://www.thefreedictionary.com

Related

Loading Mapbox Style object from local json file in dash app

Trying to set a Mapbox style from a local JSON file in plotly dash app.
mapbox_token = <mytoken>
local_style=
json.load(open(application_root_path+r'/pages/campaignmap/mapbox_style.json'))
fig = go.Figure(
go.Scattermapbox(
mode="lines", fill="toself",
lon=[-10, -10, 8, 8, -10, None, 30, 30, 50,
50, 30, None, 100, 100, 80, 80, 100],
lat=[30, 6, 6, 30, 30, None, 20, 30, 30, 20, 20, None, 40, 50, 50, 40, 40],))
fig.update_layout(
dict1={"mapbox": {"accesstoken": mapbox_token}})
fig.update_layout(
showlegend=False,
margin={'l': 0, 'r': 0, 'b': 0, 't': 0},
mapbox_style=local_style
)
Based on the documentation on the plotly website Mapbox Map Layers in Python (see image below) this should work. The JSON file contains the export of the style from MapBox and it works fine when I use the service URL (second last dot point in image).
I've tried loading the JSON as string, dictionary and file path in the mapbox_style parameter with no luck.
Any ideas on how to make it load?
I've tried the same things you've described and had no luck. I can provide a style URL and it works perfectly, but if I download that style and provide just the style.json representation of that same style, I can't seem to get Plotly to recognize it...

How can I add custom time line in react native svg charts

I am using react native svg charts to display some data on line chart .the data is associated with some time which need to be labelled on x axis can anybody tell me how can I do it.
When we look at LineChart, it draws the lines like this.
It accepts 50 as points and 10 points and draws a line between
It accepts 10 as points and 40 points and draws a line between
class LineChartExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
return (
<LineChart
style={{ height: 200 }}
data={data}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
>
<Grid />
</LineChart>
)
}
}

Crypt-arithmetic puzzle help: SANTA-CLAUS=XMAS

I'm trying to solve the crypt-arithmetic puzzle: SANTA-CLAUS=XMAS in ECLIPSE Prolog. I am using the puzzle "SEND+MORE=MONEY" as a guide to do so. My problem that I'm not sure how to implement the "borrowing" part of subtraction. In addition, there's a carry over which can be added to the next terms as you continue adding. But in subtraction, we have to borrow and subtract a tens from the number you borrow from. Below is the code for the addition puzzle which I am using as a guide:
solve([S,E,N,D,M,O,R,Y]) :-
car(M), M > 0, car(C100),
dig(S), S > 0,
M is (S+M+C100) // 10, O is (S+M+C100) mod 10,
dig(E), car(C10),
N is (E+O+C10) mod 10, C100 is (E+O+C10) // 10,
dig(R), car(C1),
E is (N+R+C1) mod 10, C10 is (N+R+C1) // 10,
dig(D),
Y is (D+E) mod 10, C1 is (D+E) // 10,
all_diff([S,E,N,D,M,O,R,Y]).
car(0). car(1).
dig(0). dig(1). dig(2). dig(3). dig(4).
dig(5). dig(6). dig(7). dig(8). dig(9).
all_diff([]).
all_diff([N|L]) :- not member(N,L), all_diff(L).
member(N,[N|L]).
member(N,[M|L]) :- member(N,L).
I just need help understanding how to implement borrowing in subtraction.

Array prefix(while:) looping different number of times for same logic. (unable to comprehend)

Here is the shot of the playground where I am trying to print all the even numbers until you reach an odd one in 3 ways and I don't understand why there are different number of times it looped
From my understanding it should loop only 7 times because there are 6 even numbers in the starting and the 7th time there is an odd number which must terminate the loop but we have gotten 8 times in one case ! Why ?
Also quite literally the second and third way are same but still there is a difference in the number of times the loop was executed.
This, I don't understand.
===== EDIT =======
I was asked for text form of the code so here it is
import UIKit
// First way
var arr = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix { num in
return num.isMultiple(of: 2)
}
print(arr)
// Second way
var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})
print(arr2)
//Third way (almost second way but directly prinitng this time)
print([34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
.prefix(while: {$0.isMultiple(of: 2)}))
The message of "(x times)" isn't really saying how many times a line is executed in a loop.
Think of it like this, sometimes the playground want to output multiple messages on the same line, because two things are being evaluated on that line. This happen the most often in loops, where in each iteration a new thing is evaluated, and Xcode wants to display that on the same line, but can't, so can only display "(2 times)".
You can also see this happen in other places, like closures that are on the same line:
let x = Optional.some(1).map { $0 + 1 }.map { String($0) } // (3 times)
Here, the 3 things that are evaluated are:
x
$0 + 1
String($0)
Another example:
func f() { print("x");print("y") } // (2 times)
f()
Normally, each print statement will have some text displayed on the right, won't they? But since they are on the same line, Xcode can only show "(2 times)".
The same thing happens in this case:
var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})
There are 8 things to evaluate. The closure result is evaluated 7 times, plus once for the value of arr2.
If you put the variable declaration and the closure on separate lines, then Xcode can show the value of the variable on a separate line, which is why the number is one less in the two other cases.
To illustrate this graphically:
You are forcing Xcode to combine the two boxed messages into one single line. Xcode can't usefully fit all that text onto one single line, so the best it can do is "(8 times)".
I believe these are just artifacts of how playgrounds work. With a little re-formatting I get "7 times" in all three variations:
let data = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
let arr = data.prefix {
$0.isMultiple(of: 2)
}
print(arr)
let arr2 = data.prefix(while: {
$0.isMultiple(of: 2)
})
print(arr2)
print(data.prefix(while: {
$0.isMultiple(of: 2)
}))

PostGis Erro invalid geometry

I'm using POSTGIS="2.4" and Postgresql 9.6 and facing following error
While trying to insert polygon data
INSERT INTO aalis.mv_l1_parcelownership_aalis (geometry) VALUES
(st_Polygonfromtext ('polygon(482449.20552234241,
999758.79058533313,.....)',20137));
You're close :-)
The geometry you provided in your insert statement is invalid. Make sure that your POLYGONS are really correct and try one of these statements (using ST_GeomFromText or ST_PolygonFromText):
INSERT INTO aalis.mv_l1_parcelownership_aalis
VALUES (ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',20137));
or
INSERT INTO aalis.mv_l1_parcelownership_aalis
VALUES (ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',20137));
To check if your geometries are correct you can use ST_IsValid:
SELECT ST_IsValid(ST_GeomFromText('POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))'));
HINWEIS: Self-intersection at or near point 0 0
st_isvalid
------------
f
(1 Zeile)
SELECT ST_IsValid(ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'));
st_isvalid
------------
t
(1 Zeile)
Keep in mind also that the WKT standard sort of expects double parenthesis (( for polygons with 0 interior rings, and yours has only one: 'polygon(482449.20552234241, 999758.79058533313,.....). Also, the x and y axes are separated by space, not by comma. Commas separate coordinate pairs instead.
Example:
SELECT ST_IsValid('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))');
st_isvalid
------------
t
(1 Zeile)
SELECT ST_IsValid('POLYGON(30 10, 40 40, 20 40, 10 20, 30 10)');
FEHLER: parse error - invalid geometry
ZEILE 1: SELECT ST_IsValid('POLYGON(30 10, 40 40, 20 40, 10 20, 30 10...
^
TIP: "POLYGON(30 " <-- parse error at position 11 within geometry
Your polygon text is way off, and includes the characters ....., which are not valid:
polygon(482449.20552234241, 999758.79058533313,.....)
Not sure what your coordinates are, but the polygon text is generally in the form:
polygon((1.000 1.000, 2.000 1.500, 3.000 2.000, 1.000 1.000))
Note that the x-y pairs are in the form x y, and there are commas between the pairs.