Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dyn_huffman
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
ISC3
compression
dyn_huffman
Commits
fcad2681
Verified
Commit
fcad2681
authored
1 month ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
feat: counting nb of swaps
parent
af47c3a7
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
dyn_huffman.py
+12
-3
12 additions, 3 deletions
dyn_huffman.py
with
12 additions
and
3 deletions
dyn_huffman.py
+
12
−
3
View file @
fcad2681
...
...
@@ -10,6 +10,8 @@ LIST_ALPHABET = list(string.ascii_lowercase) + list(string.digits)
# LIST_ALPHABET = list(string.ascii_lowercase)
DICT_ALPHABET
=
{
item
:
index
+
1
for
index
,
item
in
enumerate
(
LIST_ALPHABET
)}
swap_count
=
0
@dataclass
class
Node
:
weight
:
int
=
0
...
...
@@ -20,9 +22,11 @@ class Node:
def
swap
(
tree
:
Node
):
global
swap_count
if
tree
.
left
and
tree
.
right
:
if
tree
.
left
.
weight
>
tree
.
right
.
weight
:
print
(
f
'
(
{
tree
.
left
.
weight
}
,
{
tree
.
right
.
weight
}
)
'
)
# print(f'({tree.left.weight}, {tree.right.weight})')
swap_count
+=
1
tmp
=
tree
.
right
tree
.
right
=
tree
.
left
tree
.
left
=
tmp
...
...
@@ -127,7 +131,7 @@ def compute_code(char: str):
return
bin
(
bin_val
)[
2
:].
zfill
(
e
)
def
encode
(
string
:
str
)
->
list
[
str
]
:
def
encode
(
string
:
str
)
->
str
:
tree
=
Node
()
encoded
:
list
[
str
]
=
[]
seen
:
set
[
str
]
=
set
()
...
...
@@ -141,14 +145,19 @@ def encode(string: str) -> list[str]:
encoded
.
append
(
prefix
.
nyt_code
)
encoded
.
append
(
compute_code
(
c
))
else
:
print
(
f
'
{
c
}
already seen
'
)
encoded
.
append
(
compute_prefix
(
tree
,
c
))
pprint
.
pp
(
tree
)
print
(
f
'
Nombre de swaps:
{
swap_count
}
'
)
return
'
'
.
join
(
encoded
)[
1
:]
def
main
(
string
:
str
)
->
None
:
msg
=
encode
(
string
)
print
(
msg
)
print
(
f
'
Code final:
{
msg
}
'
)
# def main(string: str) -> None:
# tree = Node()
...
...
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