Skip to content

Commit cc10b20

Browse files
tianyizheng02github-actionscclausspre-commit-ci[bot]
authored
Remove some print statements within algorithmic functions (TheAlgorithms#7499)
* Remove commented-out print statements in algorithmic functions * Encapsulate non-algorithmic code in __main__ * Remove unused print_matrix function * Remove print statement in __init__ * Remove print statement from doctest * Encapsulate non-algorithmic code in __main__ * Modify algorithm to return instead of print * Encapsulate non-algorithmic code in __main__ * Refactor data_safety_checker to return instead of print * updating DIRECTORY.md * updating DIRECTORY.md * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 717f0e4 commit cc10b20

File tree

10 files changed

+74
-69
lines changed

10 files changed

+74
-69
lines changed

DIRECTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@
360360
* [Dijkstra](graphs/dijkstra.py)
361361
* [Dijkstra 2](graphs/dijkstra_2.py)
362362
* [Dijkstra Algorithm](graphs/dijkstra_algorithm.py)
363+
* [Dijkstra Alternate](graphs/dijkstra_alternate.py)
363364
* [Dinic](graphs/dinic.py)
364365
* [Directed And Undirected (Weighted) Graph](graphs/directed_and_undirected_(weighted)_graph.py)
365366
* [Edmonds Karp Multiple Source And Sink](graphs/edmonds_karp_multiple_source_and_sink.py)
@@ -460,6 +461,7 @@
460461
* [Similarity Search](machine_learning/similarity_search.py)
461462
* [Support Vector Machines](machine_learning/support_vector_machines.py)
462463
* [Word Frequency Functions](machine_learning/word_frequency_functions.py)
464+
* [Xgboostclassifier](machine_learning/xgboostclassifier.py)
463465

464466
## Maths
465467
* [3N Plus 1](maths/3n_plus_1.py)
@@ -534,6 +536,7 @@
534536
* [Line Length](maths/line_length.py)
535537
* [Lucas Lehmer Primality Test](maths/lucas_lehmer_primality_test.py)
536538
* [Lucas Series](maths/lucas_series.py)
539+
* [Maclaurin Sin](maths/maclaurin_sin.py)
537540
* [Matrix Exponentiation](maths/matrix_exponentiation.py)
538541
* [Max Sum Sliding Window](maths/max_sum_sliding_window.py)
539542
* [Median Of Two Arrays](maths/median_of_two_arrays.py)
@@ -936,6 +939,7 @@
936939
* [Not Gate](quantum/not_gate.py)
937940
* [Q Full Adder](quantum/q_full_adder.py)
938941
* [Quantum Entanglement](quantum/quantum_entanglement.py)
942+
* [Quantum Random](quantum/quantum_random.py)
939943
* [Ripple Adder Classic](quantum/ripple_adder_classic.py)
940944
* [Single Qubit Measure](quantum/single_qubit_measure.py)
941945

cellular_automata/game_of_life.py

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def run(canvas: list[list[bool]]) -> list[list[bool]]:
6666
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
6767
for r, row in enumerate(current_canvas):
6868
for c, pt in enumerate(row):
69-
# print(r-1,r+2,c-1,c+2)
7069
next_gen_canvas[r][c] = __judge_point(
7170
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
7271
)

digital_image_processing/index_calculation.py

-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class IndexCalculation:
105105
"""
106106

107107
def __init__(self, red=None, green=None, blue=None, red_edge=None, nir=None):
108-
# print("Numpy version: " + np.__version__)
109108
self.set_matricies(red=red, green=green, blue=blue, red_edge=red_edge, nir=nir)
110109

111110
def set_matricies(self, red=None, green=None, blue=None, red_edge=None, nir=None):

divide_and_conquer/max_subarray_sum.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def max_subarray_sum(array, left, right):
6969
return max(left_half_sum, right_half_sum, cross_sum)
7070

7171

72-
array = [-2, -5, 6, -2, -3, 1, 5, -6]
73-
array_length = len(array)
74-
print(
75-
"Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1)
76-
)
72+
if __name__ == "__main__":
73+
array = [-2, -5, 6, -2, -3, 1, 5, -6]
74+
array_length = len(array)
75+
print(
76+
"Maximum sum of contiguous subarray:",
77+
max_subarray_sum(array, 0, array_length - 1),
78+
)

divide_and_conquer/strassen_matrix_multiplication.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def matrix_dimensions(matrix: list) -> tuple[int, int]:
6868

6969

7070
def print_matrix(matrix: list) -> None:
71-
for i in range(len(matrix)):
72-
print(matrix[i])
71+
print("\n".join(str(line) for line in matrix))
7372

7473

7574
def actual_strassen(matrix_a: list, matrix_b: list) -> list:

dynamic_programming/longest_sub_array.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class SubArray:
1414
def __init__(self, arr):
1515
# we need a list not a string, so do something to change the type
1616
self.array = arr.split(",")
17-
print(("the input array is:", self.array))
1817

1918
def solve_sub_array(self):
2019
rear = [int(self.array[0])] * len(self.array)

dynamic_programming/max_non_adjacent_sum.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def maximum_non_adjacent_sum(nums: list[int]) -> int:
77
"""
88
Find the maximum non-adjacent sum of the integers in the nums input list
99
10-
>>> print(maximum_non_adjacent_sum([1, 2, 3]))
10+
>>> maximum_non_adjacent_sum([1, 2, 3])
1111
4
1212
>>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6])
1313
18

dynamic_programming/subset_generation.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def print_combination(arr, n, r):
3737
combination_util(arr, n, r, 0, data, 0)
3838

3939

40-
# Driver function to check for above function
41-
arr = [10, 20, 30, 40, 50]
42-
print_combination(arr, len(arr), 3)
43-
# This code is contributed by Ambuj sahu
40+
if __name__ == "__main__":
41+
# Driver code to check the function above
42+
arr = [10, 20, 30, 40, 50]
43+
print_combination(arr, len(arr), 3)
44+
# This code is contributed by Ambuj sahu

dynamic_programming/sum_of_subset.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
def is_sum_subset(arr, arr_len, required_sum):
1+
def is_sum_subset(arr: list[int], required_sum: int) -> bool:
22
"""
3-
>>> is_sum_subset([2, 4, 6, 8], 4, 5)
3+
>>> is_sum_subset([2, 4, 6, 8], 5)
44
False
5-
>>> is_sum_subset([2, 4, 6, 8], 4, 14)
5+
>>> is_sum_subset([2, 4, 6, 8], 14)
66
True
77
"""
88
# a subset value says 1 if that subset sum can be formed else 0
99
# initially no subsets can be formed hence False/0
10-
subset = [[False for i in range(required_sum + 1)] for i in range(arr_len + 1)]
10+
arr_len = len(arr)
11+
subset = [[False] * (required_sum + 1) for _ in range(arr_len + 1)]
1112

1213
# for each arr value, a sum of zero(0) can be formed by not taking any element
1314
# hence True/1
@@ -25,10 +26,7 @@ def is_sum_subset(arr, arr_len, required_sum):
2526
if arr[i - 1] <= j:
2627
subset[i][j] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]]
2728

28-
# uncomment to print the subset
29-
# for i in range(arrLen+1):
30-
# print(subset[i])
31-
print(subset[arr_len][required_sum])
29+
return subset[arr_len][required_sum]
3230

3331

3432
if __name__ == "__main__":

machine_learning/forecasting/run.py

+50-46
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
this is code for forecasting
33
but i modified it and used it for safety checker of data
4-
for ex: you have a online shop and for some reason some data are
4+
for ex: you have an online shop and for some reason some data are
55
missing (the amount of data that u expected are not supposed to be)
66
then we can use it
77
*ps : 1. ofc we can use normal statistic method but in this case
@@ -91,14 +91,14 @@ def interquartile_range_checker(train_user: list) -> float:
9191
return low_lim
9292

9393

94-
def data_safety_checker(list_vote: list, actual_result: float) -> None:
94+
def data_safety_checker(list_vote: list, actual_result: float) -> bool:
9595
"""
9696
Used to review all the votes (list result prediction)
9797
and compare it to the actual result.
9898
input : list of predictions
9999
output : print whether it's safe or not
100-
>>> data_safety_checker([2,3,4],5.0)
101-
Today's data is not safe.
100+
>>> data_safety_checker([2, 3, 4], 5.0)
101+
False
102102
"""
103103
safe = 0
104104
not_safe = 0
@@ -107,50 +107,54 @@ def data_safety_checker(list_vote: list, actual_result: float) -> None:
107107
safe = not_safe + 1
108108
else:
109109
if abs(abs(i) - abs(actual_result)) <= 0.1:
110-
safe = safe + 1
110+
safe += 1
111111
else:
112-
not_safe = not_safe + 1
113-
print(f"Today's data is {'not ' if safe <= not_safe else ''}safe.")
112+
not_safe += 1
113+
return safe > not_safe
114114

115115

116-
# data_input_df = pd.read_csv("ex_data.csv", header=None)
117-
data_input = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]]
118-
data_input_df = pd.DataFrame(data_input, columns=["total_user", "total_even", "days"])
116+
if __name__ == "__main__":
117+
# data_input_df = pd.read_csv("ex_data.csv", header=None)
118+
data_input = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]]
119+
data_input_df = pd.DataFrame(
120+
data_input, columns=["total_user", "total_even", "days"]
121+
)
119122

120-
"""
121-
data column = total user in a day, how much online event held in one day,
122-
what day is that(sunday-saturday)
123-
"""
123+
"""
124+
data column = total user in a day, how much online event held in one day,
125+
what day is that(sunday-saturday)
126+
"""
124127

125-
# start normalization
126-
normalize_df = Normalizer().fit_transform(data_input_df.values)
127-
# split data
128-
total_date = normalize_df[:, 2].tolist()
129-
total_user = normalize_df[:, 0].tolist()
130-
total_match = normalize_df[:, 1].tolist()
131-
132-
# for svr (input variable = total date and total match)
133-
x = normalize_df[:, [1, 2]].tolist()
134-
x_train = x[: len(x) - 1]
135-
x_test = x[len(x) - 1 :]
136-
137-
# for linear reression & sarimax
138-
trn_date = total_date[: len(total_date) - 1]
139-
trn_user = total_user[: len(total_user) - 1]
140-
trn_match = total_match[: len(total_match) - 1]
141-
142-
tst_date = total_date[len(total_date) - 1 :]
143-
tst_user = total_user[len(total_user) - 1 :]
144-
tst_match = total_match[len(total_match) - 1 :]
145-
146-
147-
# voting system with forecasting
148-
res_vote = []
149-
res_vote.append(
150-
linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match)
151-
)
152-
res_vote.append(sarimax_predictor(trn_user, trn_match, tst_match))
153-
res_vote.append(support_vector_regressor(x_train, x_test, trn_user))
154-
155-
# check the safety of todays'data^^
156-
data_safety_checker(res_vote, tst_user)
128+
# start normalization
129+
normalize_df = Normalizer().fit_transform(data_input_df.values)
130+
# split data
131+
total_date = normalize_df[:, 2].tolist()
132+
total_user = normalize_df[:, 0].tolist()
133+
total_match = normalize_df[:, 1].tolist()
134+
135+
# for svr (input variable = total date and total match)
136+
x = normalize_df[:, [1, 2]].tolist()
137+
x_train = x[: len(x) - 1]
138+
x_test = x[len(x) - 1 :]
139+
140+
# for linear regression & sarimax
141+
trn_date = total_date[: len(total_date) - 1]
142+
trn_user = total_user[: len(total_user) - 1]
143+
trn_match = total_match[: len(total_match) - 1]
144+
145+
tst_date = total_date[len(total_date) - 1 :]
146+
tst_user = total_user[len(total_user) - 1 :]
147+
tst_match = total_match[len(total_match) - 1 :]
148+
149+
# voting system with forecasting
150+
res_vote = [
151+
linear_regression_prediction(
152+
trn_date, trn_user, trn_match, tst_date, tst_match
153+
),
154+
sarimax_predictor(trn_user, trn_match, tst_match),
155+
support_vector_regressor(x_train, x_test, trn_user),
156+
]
157+
158+
# check the safety of today's data
159+
not_str = "" if data_safety_checker(res_vote, tst_user) else "not "
160+
print("Today's data is {not_str}safe.")

0 commit comments

Comments
 (0)