Skip to content
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

Refactored by Sourcery #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion epi_judge_python/absent_value_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def find_missing_element_wrapper(stream):
try:
res = find_missing_element(iter(stream))
if res in stream:
raise TestFailure('{} appears in stream'.format(res))
raise TestFailure(f'{res} appears in stream')
except ValueError:
raise TestFailure('Unexpected no missing element exception')

Expand Down
2 changes: 0 additions & 2 deletions epi_judge_python/adding_credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def client_credits_info_tester(ops):
op = x[0]
s_arg = x[1]
i_arg = x[2]
if op == 'ClientsCreditsInfo':
pass
if op == 'max':
result = cr.max()
if result != s_arg:
Expand Down
33 changes: 15 additions & 18 deletions epi_judge_python/alternating_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ def check_answer(A):
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] <= A[{}]'.format(i - 1, i),
'{} > {}'.format(A[i - 1], A[i]))
if i + 1 < len(A):
if A[i] < A[i + 1]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i, i + 1),
'{} < {}'.format(A[i], A[i + 1]))
if i + 1 < len(A) and A[i] < A[i + 1]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i, i + 1),
'{} < {}'.format(A[i], A[i + 1]))
else:
if i > 0:
if A[i - 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i - 1, i),
'{} < {}'.format(A[i - 1], A[i]))
if i + 1 < len(A):
if A[i + 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] <= A[{}]'.format(i, i + 1),
'{} > {}'.format(A[i], A[i + 1]))
if i > 0 and A[i - 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i - 1, i),
'{} < {}'.format(A[i - 1], A[i]))
if i + 1 < len(A) and A[i + 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] <= A[{}]'.format(i, i + 1),
'{} > {}'.format(A[i], A[i + 1]))

executor.run(functools.partial(rearrange, A))
check_answer(A)
Expand Down
8 changes: 3 additions & 5 deletions epi_judge_python/circular_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ def queue_tester(ops):
elif op == 'dequeue':
result = q.dequeue()
if result != arg:
raise TestFailure('Dequeue: expected ' + str(arg) + ', got ' +
str(result))
raise TestFailure((f'Dequeue: expected {str(arg)}, got ' + str(result)))
elif op == 'size':
result = q.size()
if result != arg:
raise TestFailure('Size: expected ' + str(arg) + ', got ' +
str(result))
raise TestFailure((f'Size: expected {str(arg)}, got ' + str(result)))
else:
raise RuntimeError('Unsupported queue operation: ' + op)
raise RuntimeError(f'Unsupported queue operation: {op}')


if __name__ == '__main__':
Expand Down
13 changes: 6 additions & 7 deletions epi_judge_python/copy_posting_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def copy_postings_list(L: PostingListNode) -> Optional[PostingListNode]:


def assert_lists_equal(orig, copy):
node_mapping = dict()
node_mapping = {}
o_it = orig
c_it = copy
while o_it:
Expand All @@ -38,18 +38,17 @@ def assert_lists_equal(orig, copy):
if c_it.jump is not None:
raise TestFailure(
'Jump link points to a different nodes in the copied list')
else:
if not node_mapping[o_it.jump] is c_it.jump:
raise TestFailure(
'Jump link points to a different nodes in the copied list')
elif node_mapping[o_it.jump] is not c_it.jump:
raise TestFailure(
'Jump link points to a different nodes in the copied list')
o_it = o_it.next
c_it = c_it.next


@enable_executor_hook
def copy_postings_list_wrapper(executor, l):
def create_posting_list(serialized):
key_mapping = dict()
key_mapping = {}
head = None
for (order, _) in reversed(serialized):
head = PostingListNode(order, head)
Expand All @@ -58,7 +57,7 @@ def create_posting_list(serialized):
list_it = head
for (_, jump_index) in serialized:
if jump_index != -1:
list_it.jump = key_mapping.get(jump_index, None)
list_it.jump = key_mapping.get(jump_index)
if not list_it.jump:
raise RuntimeError('Jump index out of range')

Expand Down
2 changes: 1 addition & 1 deletion epi_judge_python/do_lists_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def overlapping_lists_wrapper(executor, l0, l1, common, cycle0, cycle1):

result = executor.run(functools.partial(overlapping_lists, l0, l1))

if not (id(result) in common_nodes or (not common_nodes and not result)):
if id(result) not in common_nodes and (common_nodes or result):
raise TestFailure('Invalid result')


Expand Down
2 changes: 1 addition & 1 deletion epi_judge_python/dutch_national_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def dutch_flag_partition_wrapper(executor, A, pivot_idx):
i += 1

if i != len(A):
raise TestFailure('Not partitioned after {}th element'.format(i))
raise TestFailure(f'Not partitioned after {i}th element')
elif any(count):
raise TestFailure('Some elements are missing from original array')

Expand Down
2 changes: 1 addition & 1 deletion epi_judge_python/enumerate_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def serialize_structure(tree):
q = [tree]
while q:
a = q.pop(0)
result.append(0 if not a else 1)
result.append(1 if a else 0)
if a:
q.append(a.left)
q.append(a.right)
Expand Down
3 changes: 1 addition & 2 deletions epi_judge_python/graph_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def check_graph(node, graph):
if node is None:
raise TestFailure('Graph was not copied')

vertex_set = set()
q = collections.deque()
q.append(node)
vertex_set.add(node)
vertex_set = {node}
while q:
vertex = q.popleft()
if vertex.label >= len(graph):
Expand Down
7 changes: 5 additions & 2 deletions epi_judge_python/gray_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def gray_code_wrapper(executor, num_bits):

expected_size = (1 << num_bits)
if len(result) != expected_size:
raise TestFailure('Length mismatch: expected ' + str(expected_size) +
', got ' + str(len(result)))
raise TestFailure(
(f'Length mismatch: expected {str(expected_size)}' + ', got ')
+ str(len(result))
)

for i in range(1, len(result)):
if not differ_by_1_bit(result[i - 1], result[i]):
if result[i - 1] == result[i]:
Expand Down
6 changes: 4 additions & 2 deletions epi_judge_python/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def compute_tower_hanoi_wrapper(executor, num_rings):

for from_peg, to_peg in result:
if pegs[to_peg] and pegs[from_peg][-1] >= pegs[to_peg][-1]:
raise TestFailure('Illegal move from {} to {}'.format(
pegs[from_peg][-1], pegs[to_peg][-1]))
raise TestFailure(
f'Illegal move from {pegs[from_peg][-1]} to {pegs[to_peg][-1]}'
)

pegs[to_peg].append(pegs[from_peg].pop())
expected_pegs1 = [[], [], list(reversed(range(1, num_rings + 1)))]
expected_pegs2 = [[], list(reversed(range(1, num_rings + 1))), []]
Expand Down
8 changes: 3 additions & 5 deletions epi_judge_python/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ def lru_cache_tester(commands):
if cmd[0] == 'lookup':
result = cache.lookup(cmd[1])
if result != cmd[2]:
raise TestFailure('Lookup: expected ' + str(cmd[2]) +
', got ' + str(result))
raise TestFailure((f'Lookup: expected {str(cmd[2])}' + ', got ') + str(result))
elif cmd[0] == 'insert':
cache.insert(cmd[1], cmd[2])
elif cmd[0] == 'erase':
result = 1 if cache.erase(cmd[1]) else 0
if result != cmd[2]:
raise TestFailure('Erase: expected ' + str(cmd[2]) + ', got ' +
str(result))
raise TestFailure((f'Erase: expected {str(cmd[2])}, got ' + str(result)))
else:
raise RuntimeError('Unexpected command ' + cmd[0])
raise RuntimeError(f'Unexpected command {cmd[0]}')


if __name__ == '__main__':
Expand Down
29 changes: 14 additions & 15 deletions epi_judge_python/pivot_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def list_pivoting(l: ListNode, x: int) -> Optional[ListNode]:


def linked_to_list(l):
v = list()
v = []
while l is not None:
v.append(l.data)
l = l.next
Expand All @@ -29,20 +29,19 @@ def list_pivoting_wrapper(executor, l, x):
pivoted = linked_to_list(l)
mode = -1
for i in pivoted:
if mode == -1:
if i == x:
mode = 0
elif i > x:
mode = 1
elif mode == 0:
if i < x:
raise TestFailure('List is not pivoted')
elif i > x:
mode = 1
else:
if i <= x:
raise TestFailure('List is not pivoted')

if mode == -1 and i == x:
mode = 0
elif (
mode == -1
and i > x
or mode != -1
and mode == 0
and i >= x
and i > x
):
mode = 1
elif mode != -1 and (mode != 0 or i < x) and (mode == 0 or i <= x):
raise TestFailure('List is not pivoted')
if sorted(original) != sorted(pivoted):
raise TestFailure('Result list contains different values')

Expand Down
5 changes: 2 additions & 3 deletions epi_judge_python/queue_from_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def queue_tester(ops):
elif op == 'dequeue':
result = q.dequeue()
if result != arg:
raise TestFailure('Dequeue: expected ' + str(arg) +
', got ' + str(result))
raise TestFailure((f'Dequeue: expected {str(arg)}' + ', got ') + str(result))
else:
raise RuntimeError('Unsupported queue operation: ' + op)
raise RuntimeError(f'Unsupported queue operation: {op}')
except IndexError:
raise TestFailure('Unexpected IndexError exception')

Expand Down
8 changes: 3 additions & 5 deletions epi_judge_python/queue_with_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ def queue_tester(ops):
elif op == 'dequeue':
result = q.dequeue()
if result != arg:
raise TestFailure('Dequeue: expected ' + str(arg) +
', got ' + str(result))
raise TestFailure((f'Dequeue: expected {str(arg)}' + ', got ') + str(result))
elif op == 'max':
result = q.max()
if result != arg:
raise TestFailure('Max: expected ' + str(arg) + ', got ' +
str(result))
raise TestFailure((f'Max: expected {str(arg)}, got ' + str(result)))
else:
raise RuntimeError('Unsupported queue operation: ' + op)
raise RuntimeError(f'Unsupported queue operation: {op}')
except IndexError:
raise TestFailure('Unexpected IndexError exception')

Expand Down
2 changes: 1 addition & 1 deletion epi_judge_python/refueling_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find_ample_city_wrapper(executor, gallons, distances):
city = (result + i) % num_cities
tank += gallons[city] * MPG - distances[city]
if tank < 0:
raise TestFailure('Out of gas on city {}'.format(i))
raise TestFailure(f'Out of gas on city {i}')


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions epi_judge_python/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def eliminate_duplicate_wrapper(executor, names):


def comp(expected, result):
return all([
return all(
e == r.first_name for (e, r) in zip(sorted(expected), sorted(result))
])
)


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions epi_judge_python/search_entry_equal_to_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def search_entry_equal_to_its_index(A: List[int]) -> int:
def search_entry_equal_to_its_index_wrapper(executor, A):
result = executor.run(functools.partial(search_entry_equal_to_its_index,
A))
if result != -1:
if A[result] != result:
raise TestFailure('Entry does not equal to its index')
else:
if result == -1:
if any(i == a for i, a in enumerate(A)):
raise TestFailure('There are entries which equal to its index')

elif A[result] != result:
raise TestFailure('Entry does not equal to its index')


if __name__ == '__main__':
exit(
Expand Down
22 changes: 15 additions & 7 deletions epi_judge_python/search_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ def search_maze(maze: List[List[int]], s: Coordinate,


def path_element_is_feasible(maze, prev, cur):
if not ((0 <= cur.x < len(maze)) and
(0 <= cur.y < len(maze[cur.x])) and maze[cur.x][cur.y] == WHITE):
return False
return cur == (prev.x + 1, prev.y) or \
cur == (prev.x - 1, prev.y) or \
cur == (prev.x, prev.y + 1) or \
cur == (prev.x, prev.y - 1)
return (
cur
in [
(prev.x + 1, prev.y),
(prev.x - 1, prev.y),
(prev.x, prev.y + 1),
(prev.x, prev.y - 1),
]
if (
(0 <= cur.x < len(maze))
and (0 <= cur.y < len(maze[cur.x]))
and maze[cur.x][cur.y] == WHITE
)
else False
)


@enable_executor_hook
Expand Down
2 changes: 0 additions & 2 deletions epi_judge_python/smallest_subarray_covering_all_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def find_smallest_sequentially_covering_subset_wrapper(executor, paragraph,
while kw_idx < len(keywords):
if para_idx >= len(paragraph):
raise TestFailure('Not all keywords are in the generated subarray')
if para_idx >= len(paragraph):
raise TestFailure('Subarray end index exceeds array size')
if paragraph[para_idx] == keywords[kw_idx]:
kw_idx += 1
para_idx += 1
Expand Down
11 changes: 4 additions & 7 deletions epi_judge_python/stack_with_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ def stack_tester(ops):
elif op == 'pop':
result = s.pop()
if result != arg:
raise TestFailure('Pop: expected ' + str(arg) + ', got ' +
str(result))
raise TestFailure((f'Pop: expected {str(arg)}, got ' + str(result)))
elif op == 'max':
result = s.max()
if result != arg:
raise TestFailure('Max: expected ' + str(arg) + ', got ' +
str(result))
raise TestFailure((f'Max: expected {str(arg)}, got ' + str(result)))
elif op == 'empty':
result = int(s.empty())
if result != arg:
raise TestFailure('Empty: expected ' + str(arg) +
', got ' + str(result))
raise TestFailure((f'Empty: expected {str(arg)}' + ', got ') + str(result))
else:
raise RuntimeError('Unsupported stack operation: ' + op)
raise RuntimeError(f'Unsupported stack operation: {op}')
except IndexError:
raise TestFailure('Unexpected IndexError exception')

Expand Down
2 changes: 1 addition & 1 deletion epi_judge_python/sudoku_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def solve_sudoku_wrapper(executor, partial_assignment):
if len(br) != len(sr):
raise TestFailure('Initial cell assignment has been changed')
for (bcell, scell) in zip(br, sr):
if bcell != 0 and bcell != scell:
if bcell not in [0, scell]:
raise TestFailure('Initial cell assignment has been changed')

block_size = int(math.sqrt(len(solved)))
Expand Down
Loading