Skip to content

hamming_code.py: Reduce McCabe code complexity #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions backtracking/knight_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@


def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
'''
"""
Find all the valid positions a knight can move to from the current position.
>>> get_valid_pos((1, 3), 4)
[(2, 1), (0, 1), (3, 2)]
'''
"""

y, x = position
positions = [
Expand All @@ -20,7 +20,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
(y + 2, x + 1),
(y + 2, x - 1),
(y - 2, x + 1),
(y - 2, x - 1)
(y - 2, x - 1),
]
permissible_positions = []

Expand All @@ -33,23 +33,23 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:


def is_complete(board: List[List[int]]) -> bool:
'''
"""
Check if the board (matrix) has been completely filled with non-zero values.
>>> is_complete([[1]])
True
>>> is_complete([[1, 2], [3, 0]])
False
'''
"""

return not any(elem == 0 for row in board for elem in row)


def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int) -> bool:
'''
"""
Helper function to solve knight tour problem.
'''
"""

if is_complete(board):
return True
Expand All @@ -67,7 +67,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)


def open_knight_tour(n: int) -> List[List[int]]:
'''
"""
Find the solution for the knight tour problem for a board of size n. Raises
ValueError if the tour cannot be performed for the given size.
Expand All @@ -78,7 +78,7 @@ def open_knight_tour(n: int) -> List[List[int]]:
Traceback (most recent call last):
...
ValueError: Open Kight Tour cannot be performed on a board of size 2
'''
"""

board = [[0 for i in range(n)] for j in range(n)]

Expand Down
7 changes: 4 additions & 3 deletions dynamic_programming/max_non_adjacent_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def maximum_non_adjacent_sum(nums: List[int]) -> int:
'''
"""
Find the maximum non-adjacent sum of the integers in the nums input list
>>> print(maximum_non_adjacent_sum([1, 2, 3]))
Expand All @@ -15,14 +15,15 @@ def maximum_non_adjacent_sum(nums: List[int]) -> int:
0
>>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6])
500
'''
"""
if not nums:
return 0
max_including = nums[0]
max_excluding = 0
for num in nums[1:]:
max_including, max_excluding = (
max_excluding + num, max(max_including, max_excluding)
max_excluding + num,
max(max_including, max_excluding),
)
return max(max_excluding, max_including)

Expand Down
39 changes: 11 additions & 28 deletions hashes/hamming_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ def emitterConverter(sizePar, data):
if x == "1":
contBO += 1
contLoop += 1
if contBO % 2 == 0:
parity.append(0)
else:
parity.append(1)
parity.append(contBO % 2)

qtdBP += 1

Expand Down Expand Up @@ -168,12 +165,9 @@ def receptorConverter(sizePar, data):
for x in range(1, len(data) + 1):
# Performs a template of bit positions - who should be given,
# and who should be parity
if qtdBP < sizePar:
if (np.log(x) / np.log(2)).is_integer():
dataOutGab.append("P")
qtdBP = qtdBP + 1
else:
dataOutGab.append("D")
if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer():
dataOutGab.append("P")
qtdBP = qtdBP + 1
else:
dataOutGab.append("D")

Expand Down Expand Up @@ -201,12 +195,9 @@ def receptorConverter(sizePar, data):
for x in range(1, sizePar + len(dataOutput) + 1):
# Performs a template position of bits - who should be given,
# and who should be parity
if qtdBP < sizePar:
if (np.log(x) / np.log(2)).is_integer():
dataOutGab.append("P")
qtdBP = qtdBP + 1
else:
dataOutGab.append("D")
if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer():
dataOutGab.append("P")
qtdBP = qtdBP + 1
else:
dataOutGab.append("D")

Expand All @@ -230,14 +221,10 @@ def receptorConverter(sizePar, data):
aux = (binPos[contLoop])[-1 * (bp)]
except IndexError:
aux = "0"
if aux == "1":
if x == "1":
contBO += 1
if aux == "1" and x == "1":
contBO += 1
contLoop += 1
if contBO % 2 == 0:
parity.append("0")
else:
parity.append("1")
parity.append(str(contBO % 2))

qtdBP += 1

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

if parityReceived == parity:
ack = True
else:
ack = False

ack = parityReceived == parity
return dataOutput, ack


Expand Down