Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AP-WALLOT-Angy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Angy Wallot
AP-WALLOT-Angy
Commits
7e83250c
Commit
7e83250c
authored
1 year ago
by
Angy Wallot
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
72e5c20e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tp.8/apqueue.py
+109
-0
109 additions, 0 deletions
tp.8/apqueue.py
with
109 additions
and
0 deletions
tp.8/apqueue.py
0 → 100644
+
109
−
0
View file @
7e83250c
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod:`apqueue` module
:author: `FIL - Faculté des Sciences et Technologies -
Univ. Lille <http://portail.fil.univ-lille1.fr>`_
:date: 2015, september
:last revision: 2024, March
A module for queue data structure.
:Provides:
* class ApQueue
and methods
* `enqueue`
* `dequeue`
* `is_empty`
"""
from
typing
import
TypeVar
T
=
TypeVar
(
'
T
'
)
class
ApQueueEmptyError
(
Exception
):
"""
Exception for empty stacks
"""
def
__init__
(
self
,
msg
):
self
.
message
=
msg
class
ApQueue
():
"""
$$$ ap_queue = ApQueue()
$$$ ap_queue.is_empty()
True
$$$ ap_queue.enqueue(1)
$$$ ap_queue.is_empty()
False
$$$ ap_queue.enqueue(2)
$$$ str(ap_queue)
'
→2|1→
'
$$$ ap_queue.dequeue()
1
$$$ ap_queue.dequeue()
2
$$$ ap_queue.is_empty()
True
$$e ap_queue.dequeue()
ApQueueEmptyError
"""
ARROW
=
chr
(
0x2192
)
def
__init__
(
self
):
"""
build a new empty queue
precondition: none
"""
self
.
__content
=
[]
def
enqueue
(
self
,
elt
:
T
):
"""
insert an element at the begining of the queue
precondition: none
"""
self
.
__content
.
insert
(
0
,
elt
)
def
dequeue
(
self
)
->
T
:
"""
return the element on top of self
Side effect: self contains an element less
precondition: self must be non empty
"""
if
len
(
self
.
__content
)
>
0
:
res
=
self
.
__content
.
pop
()
else
:
raise
ApQueueEmptyError
(
'
empty queue, nothing to dequeue
'
)
return
res
def
is_empty
(
self
)
->
bool
:
"""
return:
* ``True`` if s is empty
* ``False`` otherwise
precondition: none
"""
return
self
.
__content
==
[]
def
__str__
(
self
)
->
str
:
"""
return the string representation of this queue.
"""
return
ApQueue
.
ARROW
+
\
"
|
"
.
join
(
str
(
el
)
for
el
in
self
.
__content
[::
-
1
])
+
\
ApQueue
.
ARROW
def
__len__
(
self
)
->
int
:
"""
return the length of this queue
"""
return
len
(
self
.
__content
)
if
__name__
==
'
__main__
'
:
import
apl1test
apl1test
.
testmod
(
'
apqueue.py
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment