Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algoritmer og datastrukturer
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
Container 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
Jacob Theisen
Algoritmer og datastrukturer
Commits
08b6e655
Commit
08b6e655
authored
3 years ago
by
Jacob Theisen
Browse files
Options
Downloads
Patches
Plain Diff
kjør
parent
54eeab10
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ov8/huffman.py
+115
-18
115 additions, 18 deletions
ov8/huffman.py
ov8/trash/test
+0
-0
0 additions, 0 deletions
ov8/trash/test
ov8/trash/test.py
+20
-2
20 additions, 2 deletions
ov8/trash/test.py
with
135 additions
and
20 deletions
ov8/huffman.py
+
115
−
18
View file @
08b6e655
with
open
(
'
./compressed_text
'
,
'
rb
'
)
as
file
:
tekst
=
file
.
read
()
class
Table_object
(
object
):
def
__init__
(
self
,
char
,
freq
):
self
.
char
=
char
self
.
freq
=
freq
all_lz_bytes
=
[]
strings_array
=
[]
def
dcomp
():
i
=
0
dcomp_string
=
''
togle_not_bytes
=
False
while
i
<
(
len
(
tekst
)):
if
togle_not_bytes
:
dcomp_string
+=
tekst
[
i
:
i
+
num
].
decode
(
'
utf-8
'
)
strings_array
.
append
(
tekst
[
i
:
i
+
num
].
decode
(
'
utf-8
'
))
togle_not_bytes
=
False
if
i
+
num
+
4
<
len
(
tekst
):
all_lz_bytes
.
append
(
tekst
[
i
+
num
:
i
+
num
+
2
])
all_lz_bytes
.
append
(
tekst
[
i
+
num
+
2
:
i
+
num
+
4
])
i
+=
num
+
4
else
:
value
=
tekst
[
i
:
i
+
2
]
value
=
int
.
from_bytes
(
value
,
"
little
"
)
togle_not_bytes
=
True
num
=
value
all_lz_bytes
.
append
(
tekst
[
i
:
i
+
2
])
i
+=
2
return
dcomp_string
all_strings
=
dcomp
()
print
(
all_strings
)
print
(
strings_array
)
print
(
all_lz_bytes
)
print
(
tekst
)
table
=
[]
for
i
in
all_strings
:
a
=
[
a
for
a
in
table
if
a
.
char
==
i
]
if
a
:
a
[
0
].
freq
+=
1
else
:
table
.
append
(
Table_object
(
i
,
1
))
table
=
sorted
(
table
,
key
=
lambda
x
:
x
.
freq
)
# A Huffman Tree Node
class
node
:
def
__init__
(
self
,
freq
,
symbol
,
left
=
None
,
right
=
None
):
...
...
@@ -17,6 +73,11 @@ class node:
self
.
huff
=
''
rep_tree_arr
=
[]
class
rep_tree
():
def
__init__
(
self
,
symb
,
val
):
self
.
symb
=
symb
self
.
val
=
val
def
printNodes
(
node
,
val
=
''
):
...
...
@@ -33,27 +94,19 @@ def printNodes(node, val=''):
# if node is edge node then
# display its huffman code
if
(
not
node
.
left
and
not
node
.
right
):
print
(
f
"
{
node
.
symbol
}
->
{
newVal
}
"
)
rep_tree_arr
.
append
(
rep_tree
(
node
.
symbol
,
newVal
)
)
# characters for huffman tree
chars
=
[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
,
'
e
'
,
'
f
'
]
# frequency of characters
freq
=
[
5
,
9
,
12
,
13
,
16
,
45
]
# list containing unused nodes
nodes
=
[]
# converting characters and frequencies
# into huffman tree nodes
for
x
in
range
(
len
(
chars
)):
nodes
.
append
(
node
(
freq
[
x
],
chars
[
x
]))
for
j
in
table
:
nodes
.
append
(
node
(
j
.
freq
,
j
.
char
))
while
len
(
nodes
)
>
1
:
# sort all the nodes in ascending order
# based on theri frequency
nodes
=
sorted
(
nodes
,
key
=
lambda
x
:
x
.
freq
)
# pick 2 smallest nodes
left
=
nodes
[
0
]
...
...
@@ -75,3 +128,47 @@ while len(nodes) > 1:
# Huffman Tree is ready!
printNodes
(
nodes
[
0
])
bit_string
=
''
for
i
in
all_strings
:
a
=
[
a
for
a
in
rep_tree_arr
if
a
.
symb
==
i
]
bit_string
+=
a
[
0
].
val
if
len
(
bit_string
)
%
8
!=
0
:
rest
=
8
-
len
(
bit_string
)
%
8
bit_string
+=
'
0
'
*
rest
print
(
bit_string
)
bytes
=
[]
for
i
in
range
(
0
,
len
(
bit_string
),
8
):
num
=
int
(
bit_string
[
i
:
i
+
8
],
2
)
print
(
num
)
bytes
.
append
(
num
.
to_bytes
(
1
,
'
little
'
))
print
(
bytes
)
num_of_bytes
=
len
(
bytes
)
with
open
(
'
./trash/huffmann_test_file_comp
'
,
'
w
'
)
as
file
:
for
i
in
rep_tree_arr
:
file
.
write
(
f
'
{
i
.
symb
}
{
i
.
val
}
'
)
file
.
write
(
f
'
{
rest
}
'
)
file
.
write
(
f
'
{
num_of_bytes
}
'
)
with
open
(
'
./trash/huffmann_test_file_comp
'
,
'
ab
'
)
as
file
:
for
i
in
bytes
:
file
.
write
(
i
)
with
open
(
'
./trash/huffmann_test_file_comp
'
,
'
ab
'
)
as
file
:
for
i
in
all_lz_bytes
:
file
.
write
(
i
)
This diff is collapsed.
Click to expand it.
ov8/trash/test
+
0
−
0
View file @
08b6e655
No preview for this file type
This diff is collapsed.
Click to expand it.
ov8/trash/test.py
+
20
−
2
View file @
08b6e655
...
...
@@ -107,7 +107,25 @@ for i in a:
else:
print(
"
nor
"
)
'''
'''
with open(
'
../opg8-2021.pdf
'
) as file:
print
(
file
.
read
())
print(file.read())
'''
import
array
bits_array
=
array
.
array
(
'
B
'
)
bits_array
.
append
(
int
(
'
110001
'
,
2
))
bits_array
.
append
(
int
(
'
110010
'
,
2
))
bits_array
.
append
(
int
(
'
110011
'
,
2
))
bits_array
.
append
(
int
(
'
11111111
'
,
2
))
print
(
bits_array
)
with
open
(
'
./test
'
,
'
w
'
)
as
file
:
file
.
write
(
''
)
with
open
(
'
./test
'
,
'
ab
'
)
as
file
:
file
.
write
(
bits_array
)
with
open
(
'
./test
'
,
"
rb
"
)
as
file
:
print
(
file
.
read
())
\ No newline at end of file
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