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
c701791c
Verified
Commit
c701791c
authored
2 months ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
feat: computing fixed code
parent
6aa28209
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
+25
-8
25 additions, 8 deletions
dyn_huffman.py
with
25 additions
and
8 deletions
dyn_huffman.py
+
25
−
8
View file @
c701791c
...
@@ -4,6 +4,10 @@ import numpy as np
...
@@ -4,6 +4,10 @@ import numpy as np
import
pprint
import
pprint
import
math
import
math
import
argparse
import
argparse
import
string
LIST_ALPHABET
=
list
(
string
.
ascii_lowercase
)
+
list
(
string
.
digits
)
DICT_ALPHABET
=
{
item
:
index
+
1
for
index
,
item
in
enumerate
(
LIST_ALPHABET
)}
@dataclass
@dataclass
class
Node
:
class
Node
:
...
@@ -46,28 +50,41 @@ def insert_char(tree: Node, char: str):
...
@@ -46,28 +50,41 @@ def insert_char(tree: Node, char: str):
swap
(
tree
)
swap
(
tree
)
def
compute_vitter_params
(
m
:
int
)
->
(
int
,
int
)
:
def
compute_vitter_params
(
m
:
int
)
->
tuple
[
int
,
int
]
:
e
=
math
.
floor
(
np
.
log2
(
m
))
e
=
math
.
floor
(
np
.
log2
(
m
))
r
=
m
-
(
2
**
e
)
r
=
m
-
(
2
**
e
)
return
(
e
,
r
)
return
(
e
,
r
)
def
main
(
string
:
str
)
->
None
:
def
compute_k
(
char
:
str
)
->
int
:
print
(
compute_vitter_params
(
26
))
return
DICT_ALPHABET
[
char
]
def
compute_code
(
char
:
str
):
e
,
r
=
compute_vitter_params
(
len
(
LIST_ALPHABET
))
k_char
=
compute_k
(
char
)
if
k_char
<=
2
*
r
:
bin_val
=
k_char
-
1
n_bits
=
e
+
1
return
bin
(
bin_val
)[
2
:].
zfill
(
n_bits
)
else
:
bin_val
=
k_char
-
r
-
1
return
bin
(
bin_val
)[
2
:].
zfill
(
e
)
def
main
(
string
:
str
)
->
None
:
tree
=
Node
()
tree
=
Node
()
enc
=
[]
for
c
in
string
:
for
c
in
string
:
pprint
.
pp
(
tree
)
pprint
.
pp
(
tree
)
print
(
f
'
\n
====== Inserting
\'
{
c
}
\'
======
\n
'
)
print
(
f
'
\n
====== Inserting
\'
{
c
}
\'
======
\n
'
)
insert_char
(
tree
,
c
)
insert_char
(
tree
,
c
)
enc
.
append
(
compute_code
(
c
))
pprint
.
pp
(
tree
)
pprint
.
pp
(
tree
)
pprint
.
pp
(
enc
)
# pprint.pp(tree)
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
...
...
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