Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
M1-EEA
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
EEA
M1-EEA
Commits
930cfb8a
Commit
930cfb8a
authored
Feb 04, 2019
by
Pierre-antoine Comby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ajout code huffman et arithmétique
parent
618f6bcc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
166 additions
and
0 deletions
+166
-0
455-Codage_Sources/algo_code/code_arithmetique.py
455-Codage_Sources/algo_code/code_arithmetique.py
+29
-0
455-Codage_Sources/algo_code/gen_markov1.py
455-Codage_Sources/algo_code/gen_markov1.py
+24
-0
455-Codage_Sources/algo_code/huffman.py
455-Codage_Sources/algo_code/huffman.py
+37
-0
455-Codage_Sources/algo_code/huffman2.py
455-Codage_Sources/algo_code/huffman2.py
+76
-0
No files found.
455-Codage_Sources/algo_code/code_arithmetique.py
0 → 100755
View file @
930cfb8a
#!/usr/bin/env python3
import
numpy
as
np
p_s
=
[
0.1
,
0.9
]
P
=
np
.
random
.
rand
(
N
)
X
=
[
0
for
p
in
P
if
p
>
p_s
[
0
]
else
1
]
def
binary
(
n
,
b
=
2
,
m
):
"""
Convertie un nombre décimal en sa version binaire tronqué à m bits.
"""
return
np
.
ceil
(
n
*
b
**
m
)
def
arithm
(
X
):
l
=
[
0
]
h
=
[
1
]
for
x
in
X
:
if
x
==
0
:
h
.
append
(
l
[
-
1
]
+
p_s
[
0
]
*
(
h
[
-
1
]
-
l
[
-
1
]))
l
.
append
(
l
[
-
1
])
else
:
l
.
append
(
l
[
-
1
]
+
p_s
[
0
]
*
(
h
[
-
1
]
-
l
[
-
1
]))
h
.
append
(
h
[
-
1
])
lmb
=
(
l
[
-
1
]
+
h
[
-
1
])
/
2
mu
=
int
(
-
np
.
log2
(
h
[
-
1
]
-
l
[
-
1
]))
+
1
455-Codage_Sources/algo_code/gen_markov1.py
0 → 100755
View file @
930cfb8a
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 12:31:21 2019
@author: pac
"""
import
numpy
as
np
P
=
[[
0.8
,
0.1
,
0.1
],[
0.1
,
0.7
,
0.2
],[
0.1
,
0.2
,
0.7
]]
def
get_state
(
p
,
cump
):
print
(
p
,
cump
)
for
i
in
range
(
len
(
cump
)):
if
p
<
cump
[
i
]:
return
i
def
gen_markov1
(
N
,
P
,
init_state
=
0
):
cumP
=
np
.
cumsum
(
P
,
1
)
states
=
[
init_state
]
p
=
np
.
random
.
rand
(
N
)
for
k
in
range
(
N
):
states
.
append
(
get_state
(
p
[
k
],
cumP
[
states
[
-
1
]]))
return
states
print
(
gen_markov1
(
100
,
P
,
init_state
=
1
))
455-Codage_Sources/algo_code/huffman.py
0 → 100755
View file @
930cfb8a
#!/usr/bin/python3
import
numpy
as
np
def
get_2min
(
l
):
min1
=
0
min2
=
1
for
k
in
range
(
1
,
len
(
l
)):
if
l
[
k
]
<=
l
[
min1
]:
min2
=
min1
min1
=
k
elif
l
[
k
]
<=
l
[
min2
]:
min2
=
k
return
min1
,
min2
def
huffman_rec
(
p
):
if
len
(
p
)
==
2
:
C
=
[
'1'
,
'0'
]
print
(
p
,
C
)
return
C
else
:
min1
,
min2
=
get_2min
(
p
)
min1
,
min2
=
min
(
min1
,
min2
),
max
(
min1
,
min2
)
print
(
p
,
min1
,
min2
)
p_save
=
p
.
pop
(
min2
)
p
[
min1
]
=
p
[
min1
]
+
p_save
C
=
huffman_rec
(
p
)
C
.
insert
(
min2
,
C
[
min1
]
+
'1'
)
C
[
min1
]
+=
'0'
p
.
insert
(
min2
,
p_save
)
p
[
min1
]
-=
p_save
print
(
p
,
C
)
return
C
p
=
[
25
,
20
,
15
,
12
,
10
,
8
,
5
,
5
]
print
(
huffman_rec
(
p
))
455-Codage_Sources/algo_code/huffman2.py
0 → 100755
View file @
930cfb8a
#!/usr/bin/env python3
import
subprocess
class
Noeud
(
object
):
def
__init__
(
self
,
p
=
0
,
left
=
None
,
right
=
None
,
code
=
''
,
name
=
''
):
self
.
left
=
left
self
.
right
=
right
if
left
!=
None
and
right
!=
None
:
self
.
p
=
left
.
p
+
right
.
p
self
.
name
=
left
.
name
+
right
.
name
else
:
self
.
p
=
p
self
.
name
=
name
self
.
code
=
code
def
__lt__
(
self
,
other
):
return
self
.
p
<
other
.
p
def
__repr__
(
self
):
return
self
.
name
table
=
[
(
'A'
,
25
),
(
'B'
,
20
),
(
'C'
,
15
),
(
'D'
,
12
),
(
'E'
,
10
),
(
'F'
,
8
),
(
'G'
,
5
),
(
'H'
,
5
)]
table_noeud
=
[
Noeud
(
name
=
x
[
0
],
p
=
x
[
1
])
for
x
in
table
]
#print(table_noeud)
def
create_tree
(
table_noeud
):
queue
=
table_noeud
.
copy
()
while
len
(
queue
)
>
2
:
queue
.
sort
()
print
(
queue
)
l
=
queue
.
pop
(
0
)
r
=
queue
.
pop
(
0
)
queue
.
append
(
Noeud
(
left
=
l
,
right
=
r
))
root
=
Noeud
(
left
=
queue
[
0
],
right
=
queue
[
1
])
return
root
root_node
=
create_tree
(
table_noeud
)
print
(
root_node
)
def
gen_code
(
node
,
prefix
=
''
):
if
node
.
left
!=
None
:
node
.
code
=
prefix
gen_code
(
node
.
left
,
prefix
+
'0'
)
gen_code
(
node
.
right
,
prefix
+
'1'
)
else
:
node
.
code
=
prefix
print
(
node
.
name
,
node
.
code
)
gen_code
(
root_node
)
def
draw_tree
(
node
):
if
len
(
node
.
name
)
==
1
:
# feuille
desc
=
'N{} [label="{}:{}", fontcolor=blue, fontsize=16, width=2, shape=box];
\n
'
.
format
(
node
.
code
,
node
.
name
,
node
.
code
)
else
:
desc
=
'N{} [label="{}"];
\n
'
.
format
(
node
.
code
,
node
.
code
)
desc
+=
draw_tree
(
node
.
left
)
desc
+=
draw_tree
(
node
.
right
)
desc
+=
'N{}:n -> N{}:e;
\n
'
.
format
(
node
.
code
,
node
.
left
.
code
)
desc
+=
'N{}:s -> N{}:e;
\n
'
.
format
(
node
.
code
,
node
.
right
.
code
)
return
desc
with
open
(
'graph.dot'
,
'w'
)
as
f
:
f
.
write
(
'digraph G {
\n
'
)
f
.
write
(
' splines=ortho
\n
'
)
f
.
write
(
'rankdir=RL;
\n
'
)
f
.
write
(
draw_tree
(
root_node
))
f
.
write
(
'{rank =same; N'
+
'; N'
.
join
([
n
.
code
for
n
in
table_noeud
])
+
';}
\n
'
)
f
.
write
(
'}'
)
subprocess
.
call
(
'dot -Tpng graph.dot -o graph.png'
,
shell
=
True
)
print
(
'done'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment