Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
ssic_idea
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
ssic_idea
Commits
22711e0f
Commit
22711e0f
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Small refactoring of the xor method
The code of the method is cleaner and easier to read.
parent
3c4648d0
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
main.py
+26
-23
26 additions, 23 deletions
main.py
with
26 additions
and
23 deletions
main.py
+
26
−
23
View file @
22711e0f
...
...
@@ -56,39 +56,42 @@ def mul_mod(a, b):
return
format
(
res
,
'
04b
'
)
def
xor
(
a
,
b
):
#a XOR b = str(binaire)
# CODE SPAGHETTI | À OPTIMISER SI POSSIBLE
# accepte des nombres (décimaux & binaires-str) qui n'ont pas la même taille (100 et 7, ou encore '101' et '10101010101'par exemple)
# Return a string of the result of a XOR b
def
xor
(
a
,
b
):
res
=
''
if
(
type
(
a
)
!=
str
):
#transforme les int en str (5 = 101 ; pas 0b101)
a
=
format
(
a
,
"
b
"
)
if
(
type
(
b
)
!=
str
):
#transforme les int en str (7 = 111 ; pas 0b101)
b
=
format
(
b
,
"
b
"
)
if
(
len
(
a
)
>
len
(
b
)):
#si len(a) > len(b), met b --> a & a --> b pour étape suivante où len(a) pourra être comme len(b)
tmp
=
a
a
=
b
b
=
tmp
if
(
len
(
a
)
<
len
(
b
)):
#partie ou on met les 2 strings à la même taille.
l_a
=
len
(
a
)
#int
l_b
=
len
(
b
)
#int
list
=
[]
list
=
((
l_b
-
l_a
)
*
'
0
'
,
a
)
#à gauche, le nombre de '0' nécessaire pour avoir la même taille que 'b'
# & à droite 'a' qui est le plus petit str
a
=
''
.
join
(
list
)
#fusion des '0' et du str(a)
# Format to binary string
if
type
(
a
)
!=
str
:
a
=
format
(
a
,
"
04b
"
)
if
type
(
b
)
!=
str
:
b
=
format
(
b
,
"
04b
"
)
# Adjust the size of a and b
if
len
(
a
)
!=
len
(
b
):
# Add leading zero to the smallest value
if
len
(
a
)
>
len
(
b
):
tmp
=
a
a
=
b
b
=
tmp
if
len
(
a
)
<
len
(
b
):
# Create a list with the first element containing the leading zeroes and the second the initial value
value_as_list
=
((
len
(
b
)
-
len
(
a
))
*
'
0
'
,
a
)
# Then concatenate the two elements into a string
a
=
''
.
join
(
value_as_list
)
# Parse the strings and apply the XOR operation
i
=
0
size
=
len
(
a
)
#choisis 'a', mais on aurait pu mettre 'b'; same
size
=
len
(
a
)
while
i
<
size
:
#fonction XOR
while
i
<
size
:
if
a
[
i
]
==
b
[
i
]:
res
+=
'
0
'
else
:
res
+=
'
1
'
i
+=
1
return
res
...
...
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