Skip to content

Commit baba647

Browse files
cclaussgithub-actions
authored andcommitted
hamming_code.py: Reduce McCabe code complexity (TheAlgorithms#2140)
* hamming_code.py: Reduce McCabe code complexity As discussed in TheAlgorithms#2128 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 2dd8724 commit baba647

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

Diff for: hashes/hamming_code.py

+11-28
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ def emitterConverter(sizePar, data):
131131
if x == "1":
132132
contBO += 1
133133
contLoop += 1
134-
if contBO % 2 == 0:
135-
parity.append(0)
136-
else:
137-
parity.append(1)
134+
parity.append(contBO % 2)
138135

139136
qtdBP += 1
140137

@@ -168,12 +165,9 @@ def receptorConverter(sizePar, data):
168165
for x in range(1, len(data) + 1):
169166
# Performs a template of bit positions - who should be given,
170167
# and who should be parity
171-
if qtdBP < sizePar:
172-
if (np.log(x) / np.log(2)).is_integer():
173-
dataOutGab.append("P")
174-
qtdBP = qtdBP + 1
175-
else:
176-
dataOutGab.append("D")
168+
if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer():
169+
dataOutGab.append("P")
170+
qtdBP = qtdBP + 1
177171
else:
178172
dataOutGab.append("D")
179173

@@ -201,12 +195,9 @@ def receptorConverter(sizePar, data):
201195
for x in range(1, sizePar + len(dataOutput) + 1):
202196
# Performs a template position of bits - who should be given,
203197
# and who should be parity
204-
if qtdBP < sizePar:
205-
if (np.log(x) / np.log(2)).is_integer():
206-
dataOutGab.append("P")
207-
qtdBP = qtdBP + 1
208-
else:
209-
dataOutGab.append("D")
198+
if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer():
199+
dataOutGab.append("P")
200+
qtdBP = qtdBP + 1
210201
else:
211202
dataOutGab.append("D")
212203

@@ -230,14 +221,10 @@ def receptorConverter(sizePar, data):
230221
aux = (binPos[contLoop])[-1 * (bp)]
231222
except IndexError:
232223
aux = "0"
233-
if aux == "1":
234-
if x == "1":
235-
contBO += 1
224+
if aux == "1" and x == "1":
225+
contBO += 1
236226
contLoop += 1
237-
if contBO % 2 == 0:
238-
parity.append("0")
239-
else:
240-
parity.append("1")
227+
parity.append(str(contBO % 2))
241228

242229
qtdBP += 1
243230

@@ -250,11 +237,7 @@ def receptorConverter(sizePar, data):
250237
else:
251238
dataOut.append(dataOrd[x])
252239

253-
if parityReceived == parity:
254-
ack = True
255-
else:
256-
ack = False
257-
240+
ack = parityReceived == parity
258241
return dataOutput, ack
259242

260243

0 commit comments

Comments
 (0)