Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AP-KINADINOVA-Dariya
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
Dariya Kinadinova
AP-KINADINOVA-Dariya
Commits
38ecf3be
Commit
38ecf3be
authored
1 year ago
by
Dariya Kinadinova
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
7e2f61a9
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tp9/aplst.py
+141
-0
141 additions, 0 deletions
tp9/aplst.py
with
141 additions
and
0 deletions
tp9/aplst.py
0 → 100644
+
141
−
0
View file @
38ecf3be
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod:`list` module
:author: FIL - Faculté des Sciences et Technologies -
Univ. Lille <http://portail.fil.univ-lille1.fr>_
:date: 2022, march
:last revision: 2024, march
Provides
- a class :class:`ApLst` for non mutable lists
- an exception :class:`ApLstError`
ApLsts are either empty, either essentially objects with two composants :
#. a *head* composant which represent the head value of the list,
#. and a *tail* composant which is represent the tail of the list
"""
class
ApLstError
(
Exception
):
"""
Exception used by methods
* ``__init__``
* ``head``
* ``tail``
of class :class:`ApLst`.
"""
def
__init__
(
self
,
msg
):
self
.
message
=
msg
class
ApLst
():
"""
$$$ list = ApLst()
$$$ list.is_empty()
True
$$e list.head()
ApLstError
$$$ list2 = ApLst(1, list)
$$$ list2.is_empty()
False
$$$ list2.head()
1
$$$ list2.tail().is_empty()
True
$$$ l = ApLst(2, list2)
$$$ repr(l)
'
ApLst(2, ApLst(1, ApLst()))
'
$$$ str(l)
'
[2, 1]
'
$$$ repr(list2)
'
ApLst(1, ApLst())
'
"""
def
__init__
(
self
,
*
args
):
"""
build a new empty list if args is empty,
or a list whose head is first element of args,
and tail list is second element of args.
précondition: len(args) in {0, 2}
and if len(args) == 2, args[1] must be a ApLst
raise: `ApLstError` if précondition is not satisfied
"""
if
len
(
args
)
==
0
:
self
.
content
=
()
elif
len
(
args
)
==
2
:
if
isinstance
(
args
[
1
],
ApLst
):
self
.
content
=
(
args
[
0
],
args
[
1
])
else
:
raise
ApLstError
(
'
bad type for second argument
'
)
else
:
raise
ApLstError
(
'
bad number of arguments
'
)
def
is_empty
(
self
)
->
bool
:
"""
return
- True if self is empty
- False otherwise
précondition: none
"""
return
len
(
self
.
content
)
==
0
def
head
(
self
):
"""
return: head element of self
raise: `ApLstError` if self is empty
"""
if
self
.
is_empty
():
raise
ApLstError
(
'
head: empty list
'
)
else
:
return
self
.
content
[
0
]
def
tail
(
self
)
->
"
ApLst
"
:
"""
return: tail list of self
raise: `ApLstError` if self is empty
"""
if
self
.
is_empty
():
raise
ApLstError
(
'
head: empty list
'
)
else
:
return
self
.
content
[
1
]
def
__str__
(
self
)
->
str
:
"""
return: a string representation of list self
précondition: none
"""
def
str_content
(
self
,
item_number
=
0
):
if
self
.
is_empty
():
return
''
elif
item_number
==
50
:
return
'
, ...
'
else
:
comma
=
''
if
item_number
==
0
else
'
,
'
return
(
comma
+
str
(
self
.
head
())
+
str_content
(
self
.
tail
(),
item_number
+
1
))
return
f
'
[
{
str_content
(
self
)
}
]
'
def
__repr__
(
self
)
->
str
:
if
self
.
is_empty
():
content
=
""
else
:
content
=
f
"
{
repr
(
self
.
head
())
}
,
{
repr
(
self
.
tail
())
}
"
return
f
"
ApLst(
{
content
}
)
"
if
(
__name__
==
'
__main__
'
):
import
apl1test
apl1test
.
testmod
(
"
aplst.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