Tryupload

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

i love uploading

mentioned briefly several times already, Python lists make nice stacks (LIFO
queues) but poor (FIFO)
queues. Appending to them takes constant time (at least when averaged over many
such appends), but
popping from (or inserting at) the front takes linear time. What we want for
algorithms such as BFS is a
double-ended queue, or deque. Such queues are often implemented as linked lists
(where
appending/prepending and popping at either end are constant-time operations), or
so-called circular
buffers—arrays where we keep track of the position of both first element (the head)
and last element (the
tail). If either the head or the tail moves beyond its end of the array, we just
let it “flow around” to the
other side, and we use the mod (%) operator to calculate the actual indices (hence
the term circular). If we
fill the array completely, we can just reallocate the contents to a bigger one,
like with dynamic arrays (see
the black box on list in Chapter 2).
Luckily, Python has a deque class in the collections module in the standard
library. In addition to
methods such as append, extend, and pop, which are
mentioned briefly several times already, Python lists make nice stacks (LIFO
queues) but poor (FIFO)
queues. Appending to them takes constant time (at least when averaged over many
such appends), but
popping from (or inserting at) the front takes linear time. What we want for
algorithms such as BFS is a
double-ended queue, or deque. Such queues are often implemented as linked lists
(where
appending/prepending and popping at either end are constant-time operations), or
so-called circular
buffers—arrays where we keep track of the position of both first element (the head)
and last element (the
tail). If either the head or the tail moves beyond its end of the array, we just
let it “flow around” to the
other side, and we use the mod (%) operator to calculate the actual indices (hence
the term circular). If we
fill the array completely, we can just reallocate the contents to a bigger one,
like with dynamic arrays (see
the black box on list in Chapter 2).
Luckily, Python has a deque class in the collections module in the standard
library. In addition to
methods such as append, extend, and pop, which are
mentioned briefly several times already, Python lists make nice stacks (LIFO
queues) but poor (FIFO)
queues. Appending to them takes constant time (at least when averaged over many
such appends), but
popping from (or inserting at) the front takes linear time. What we want for
algorithms such as BFS is a
double-ended queue, or deque. Such queues are often implemented as linked lists
(where
appending/prepending and popping at either end are constant-time operations), or
so-called circular
buffers—arrays where we keep track of the position of both first element (the head)
and last element (the
tail). If either the head or the tail moves beyond its end of the array, we just
let it “flow around” to the
other side, and we use the mod (%) operator to calculate the actual indices (hence
the term circular). If we
fill the array completely, we can just reallocate the contents to a bigger one,
like with dynamic arrays (see
the black box on list in Chapter 2).
Luckily, Python has a deque class in the collections module in the standard
library. In addition to
methods such as append, extend, and pop, which are
mentioned briefly several times already, Python lists make nice stacks (LIFO
queues) but poor (FIFO)
queues. Appending to them takes constant time (at least when averaged over many
such appends), but
popping from (or inserting at) the front takes linear time. What we want for
algorithms such as BFS is a
double-ended queue, or deque. Such queues are often implemented as linked lists
(where
appending/prepending and popping at either end are constant-time operations), or
so-called circular
buffers—arrays where we keep track of the position of both first element (the head)
and last element (the
tail). If either the head or the tail moves beyond its end of the array, we just
let it “flow around” to the
other side, and we use the mod (%) operator to calculate the actual indices (hence
the term circular). If we
fill the array completely, we can just reallocate the contents to a bigger one,
like with dynamic arrays (see
the black box on list in Chapter 2).
Luckily, Python has a deque class in the collections module in the standard
library. In addition to
methods such as append, extend, and pop, which are

mentioned briefly several times already, Python lists make nice stacks (LIFO
queues) but poor (FIFO)
queues. Appending to them takes constant time (at least when averaged over many
such appends), but
popping from (or inserting at) the front takes linear time. What we want for
algorithms such as BFS is a
double-ended queue, or deque. Such queues are often implemented as linked lists
(where
appending/prepending and popping at either end are constant-time operations), or
so-called circular
buffers—arrays where we keep track of the position of both first element (the head)
and last element (the
tail). If either the head or the tail moves beyond its end of the array, we just
let it “flow around” to the
other side, and we use the mod (%) operator to calculate the actual indices (hence
the term circular). If we
fill the array completely, we can just reallocate the contents to a bigger one,
like with dynamic arrays (see
the black box on list in Chapter 2).
Luckily, Python has a deque class in the collections module in the standard
library. In addition to
methods such as append, extend, and pop, which are

You might also like