refactor: Replace list() and dict() calls with literals (#7198)

This commit is contained in:
Caeden 2022-10-15 02:07:03 +01:00 committed by GitHub
parent dcca5351c9
commit 6e69181d1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 14 additions and 14 deletions

View File

@ -160,7 +160,7 @@ def postorder(curr_node):
""" """
postOrder (left, right, self) postOrder (left, right, self)
""" """
node_list = list() node_list = []
if curr_node is not None: if curr_node is not None:
node_list = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node] node_list = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
return node_list return node_list

View File

@ -9,7 +9,7 @@ class Heap:
def __init__(self, key: Callable | None = None) -> None: def __init__(self, key: Callable | None = None) -> None:
# Stores actual heap items. # Stores actual heap items.
self.arr: list = list() self.arr: list = []
# Stores indexes of each item for supporting updates and deletion. # Stores indexes of each item for supporting updates and deletion.
self.pos_map: dict = {} self.pos_map: dict = {}
# Stores current size of heap. # Stores current size of heap.

View File

@ -8,7 +8,7 @@ longest word)) lookup time making it an optimal approach when space is not an is
class TrieNode: class TrieNode:
def __init__(self) -> None: def __init__(self) -> None:
self.nodes: dict[str, TrieNode] = dict() # Mapping from char to TrieNode self.nodes: dict[str, TrieNode] = {} # Mapping from char to TrieNode
self.is_leaf = False self.is_leaf = False
def insert_many(self, words: list[str]) -> None: def insert_many(self, words: list[str]) -> None:

View File

@ -54,7 +54,7 @@ def get_frequency_table(edge_array):
Returns Frequency Table Returns Frequency Table
""" """
distinct_edge = get_distinct_edge(edge_array) distinct_edge = get_distinct_edge(edge_array)
frequency_table = dict() frequency_table = {}
for item in distinct_edge: for item in distinct_edge:
bit = get_bitcode(edge_array, item) bit = get_bitcode(edge_array, item)

View File

@ -74,7 +74,7 @@ def find_minimum_change(denominations: list[int], value: str) -> list[int]:
# Driver Code # Driver Code
if __name__ == "__main__": if __name__ == "__main__":
denominations = list() denominations = []
value = "0" value = "0"
if ( if (

View File

@ -199,7 +199,7 @@ def find_pure_symbols(
{'A1': True, 'A2': False, 'A3': True, 'A5': False} {'A1': True, 'A2': False, 'A3': True, 'A5': False}
""" """
pure_symbols = [] pure_symbols = []
assignment: dict[str, bool | None] = dict() assignment: dict[str, bool | None] = {}
literals = [] literals = []
for clause in clauses: for clause in clauses:
@ -264,7 +264,7 @@ def find_unit_clauses(
n_count += 1 n_count += 1
if f_count == len(clause) - 1 and n_count == 1: if f_count == len(clause) - 1 and n_count == 1:
unit_symbols.append(sym) unit_symbols.append(sym)
assignment: dict[str, bool | None] = dict() assignment: dict[str, bool | None] = {}
for i in unit_symbols: for i in unit_symbols:
symbol = i[:2] symbol = i[:2]
assignment[symbol] = len(i) == 2 assignment[symbol] = len(i) == 2

View File

@ -100,7 +100,7 @@ def solution(filename: str = "p107_network.txt") -> int:
script_dir: str = os.path.abspath(os.path.dirname(__file__)) script_dir: str = os.path.abspath(os.path.dirname(__file__))
network_file: str = os.path.join(script_dir, filename) network_file: str = os.path.join(script_dir, filename)
adjacency_matrix: list[list[str]] adjacency_matrix: list[list[str]]
edges: dict[EdgeT, int] = dict() edges: dict[EdgeT, int] = {}
data: list[str] data: list[str]
edge1: int edge1: int
edge2: int edge2: int

View File

@ -51,7 +51,7 @@ def generate_neighbours(path):
with open(path) as f: with open(path) as f:
for line in f: for line in f:
if line.split()[0] not in dict_of_neighbours: if line.split()[0] not in dict_of_neighbours:
_list = list() _list = []
_list.append([line.split()[1], line.split()[2]]) _list.append([line.split()[1], line.split()[2]])
dict_of_neighbours[line.split()[0]] = _list dict_of_neighbours[line.split()[0]] = _list
else: else:
@ -59,7 +59,7 @@ def generate_neighbours(path):
[line.split()[1], line.split()[2]] [line.split()[1], line.split()[2]]
) )
if line.split()[1] not in dict_of_neighbours: if line.split()[1] not in dict_of_neighbours:
_list = list() _list = []
_list.append([line.split()[0], line.split()[2]]) _list.append([line.split()[0], line.split()[2]])
dict_of_neighbours[line.split()[1]] = _list dict_of_neighbours[line.split()[1]] = _list
else: else:
@ -206,7 +206,7 @@ def tabu_search(
""" """
count = 1 count = 1
solution = first_solution solution = first_solution
tabu_list = list() tabu_list = []
best_cost = distance_of_first_solution best_cost = distance_of_first_solution
best_solution_ever = solution best_solution_ever = solution

View File

@ -52,8 +52,8 @@ def _msd_radix_sort(list_of_ints: list[int], bit_position: int) -> list[int]:
if bit_position == 0 or len(list_of_ints) in [0, 1]: if bit_position == 0 or len(list_of_ints) in [0, 1]:
return list_of_ints return list_of_ints
zeros = list() zeros = []
ones = list() ones = []
# Split numbers based on bit at bit_position from the right # Split numbers based on bit at bit_position from the right
for number in list_of_ints: for number in list_of_ints:
if (number >> (bit_position - 1)) & 1: if (number >> (bit_position - 1)) & 1:

View File

@ -5,7 +5,7 @@ from collections import deque
class Automaton: class Automaton:
def __init__(self, keywords: list[str]): def __init__(self, keywords: list[str]):
self.adlist: list[dict] = list() self.adlist: list[dict] = []
self.adlist.append( self.adlist.append(
{"value": "", "next_states": [], "fail_state": 0, "output": []} {"value": "", "next_states": [], "fail_state": 0, "output": []}
) )