From de9c415e05fa21dfa64644a0ca7c7163f73a6e53 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:17:19 -0400 Subject: [PATCH 01/13] Remove commented-out print statements in algorithmic functions --- cellular_automata/game_of_life.py | 1 - digital_image_processing/index_calculation.py | 1 - dynamic_programming/sum_of_subset.py | 3 --- 3 files changed, 5 deletions(-) diff --git a/cellular_automata/game_of_life.py b/cellular_automata/game_of_life.py index c5324da73dbf..8e54702519b9 100644 --- a/cellular_automata/game_of_life.py +++ b/cellular_automata/game_of_life.py @@ -66,7 +66,6 @@ def run(canvas: list[list[bool]]) -> list[list[bool]]: next_gen_canvas = np.array(create_canvas(current_canvas.shape[0])) for r, row in enumerate(current_canvas): for c, pt in enumerate(row): - # print(r-1,r+2,c-1,c+2) next_gen_canvas[r][c] = __judge_point( pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) diff --git a/digital_image_processing/index_calculation.py b/digital_image_processing/index_calculation.py index 01cd79fc18ff..be1855e99d10 100644 --- a/digital_image_processing/index_calculation.py +++ b/digital_image_processing/index_calculation.py @@ -105,7 +105,6 @@ class IndexCalculation: """ def __init__(self, red=None, green=None, blue=None, red_edge=None, nir=None): - # print("Numpy version: " + np.__version__) self.set_matricies(red=red, green=green, blue=blue, red_edge=red_edge, nir=nir) def set_matricies(self, red=None, green=None, blue=None, red_edge=None, nir=None): diff --git a/dynamic_programming/sum_of_subset.py b/dynamic_programming/sum_of_subset.py index 77672b0b83e5..76138db3d070 100644 --- a/dynamic_programming/sum_of_subset.py +++ b/dynamic_programming/sum_of_subset.py @@ -25,9 +25,6 @@ def is_sum_subset(arr, arr_len, required_sum): if arr[i - 1] <= j: subset[i][j] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] - # uncomment to print the subset - # for i in range(arrLen+1): - # print(subset[i]) print(subset[arr_len][required_sum]) From a21f1c1963ac50e094b74ca7b1a4549f38ffd59a Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:27:10 -0400 Subject: [PATCH 02/13] Encapsulate non-algorithmic code in __main__ --- divide_and_conquer/max_subarray_sum.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/divide_and_conquer/max_subarray_sum.py b/divide_and_conquer/max_subarray_sum.py index 43f58086e078..f23e81719025 100644 --- a/divide_and_conquer/max_subarray_sum.py +++ b/divide_and_conquer/max_subarray_sum.py @@ -69,8 +69,10 @@ def max_subarray_sum(array, left, right): return max(left_half_sum, right_half_sum, cross_sum) -array = [-2, -5, 6, -2, -3, 1, 5, -6] -array_length = len(array) -print( - "Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1) -) +if __name__ == "__main__": + array = [-2, -5, 6, -2, -3, 1, 5, -6] + array_length = len(array) + print( + "Maximum sum of contiguous subarray:", + max_subarray_sum(array, 0, array_length - 1), + ) From 478c5bd923c46f601d8dd3bde2b54da29b8396c1 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:29:27 -0400 Subject: [PATCH 03/13] Remove unused print_matrix function --- divide_and_conquer/strassen_matrix_multiplication.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index 0ee426e4b39a..075a0ffd2989 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -67,11 +67,6 @@ def matrix_dimensions(matrix: list) -> tuple[int, int]: return len(matrix), len(matrix[0]) -def print_matrix(matrix: list) -> None: - for i in range(len(matrix)): - print(matrix[i]) - - def actual_strassen(matrix_a: list, matrix_b: list) -> list: """ Recursive function to calculate the product of two matrices, using the Strassen From 4aee01bef1b7d120ab5e94bd9901819df2729345 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:32:03 -0400 Subject: [PATCH 04/13] Remove print statement in __init__ --- dynamic_programming/longest_sub_array.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 30159a1386c3..b477acf61e66 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -14,7 +14,6 @@ class SubArray: def __init__(self, arr): # we need a list not a string, so do something to change the type self.array = arr.split(",") - print(("the input array is:", self.array)) def solve_sub_array(self): rear = [int(self.array[0])] * len(self.array) From 8d3ea1ca3a5de4d1e1be81012b888b73f564181c Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:34:06 -0400 Subject: [PATCH 05/13] Remove print statement from doctest --- dynamic_programming/max_non_adjacent_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py index 5362b22ca9dc..e3cc23f4983e 100644 --- a/dynamic_programming/max_non_adjacent_sum.py +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -7,7 +7,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])) + >>> maximum_non_adjacent_sum([1, 2, 3]) 4 >>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6]) 18 From d99b7fbfb972ca0b8f8ac0cdcee75e0793768ca1 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:37:18 -0400 Subject: [PATCH 06/13] Encapsulate non-algorithmic code in __main__ --- dynamic_programming/subset_generation.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 4781b23b32eb..d88ee93f12e1 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -37,7 +37,8 @@ def print_combination(arr, n, r): combination_util(arr, n, r, 0, data, 0) -# Driver function to check for above function -arr = [10, 20, 30, 40, 50] -print_combination(arr, len(arr), 3) -# This code is contributed by Ambuj sahu +if __name__ == "__main__": + # Driver function to check for above function + arr = [10, 20, 30, 40, 50] + print_combination(arr, len(arr), 3) + # This code is contributed by Ambuj sahu From 5ac15b8635dc2c5fae998af903e2f8960079a7f5 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:41:01 -0400 Subject: [PATCH 07/13] Modify algorithm to return instead of print --- dynamic_programming/sum_of_subset.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/sum_of_subset.py b/dynamic_programming/sum_of_subset.py index 76138db3d070..4500c9676677 100644 --- a/dynamic_programming/sum_of_subset.py +++ b/dynamic_programming/sum_of_subset.py @@ -1,13 +1,14 @@ -def is_sum_subset(arr, arr_len, required_sum): +def is_sum_subset(arr: list[int], required_sum: int) -> bool: """ - >>> is_sum_subset([2, 4, 6, 8], 4, 5) + >>> is_sum_subset([2, 4, 6, 8], 5) False - >>> is_sum_subset([2, 4, 6, 8], 4, 14) + >>> is_sum_subset([2, 4, 6, 8], 14) True """ # a subset value says 1 if that subset sum can be formed else 0 # initially no subsets can be formed hence False/0 - subset = [[False for i in range(required_sum + 1)] for i in range(arr_len + 1)] + arr_len = len(arr) + subset = [[False for _ in range(required_sum + 1)] for _ in range(arr_len + 1)] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 @@ -25,7 +26,7 @@ def is_sum_subset(arr, arr_len, required_sum): if arr[i - 1] <= j: subset[i][j] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] - print(subset[arr_len][required_sum]) + return subset[arr_len][required_sum] if __name__ == "__main__": From 72874f643a70677746f81d314d951bbfb58fd808 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:43:47 -0400 Subject: [PATCH 08/13] Encapsulate non-algorithmic code in __main__ --- machine_learning/forecasting/run.py | 82 +++++++++++++++-------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/machine_learning/forecasting/run.py b/machine_learning/forecasting/run.py index b11a230129eb..ea4313c5c6db 100644 --- a/machine_learning/forecasting/run.py +++ b/machine_learning/forecasting/run.py @@ -1,7 +1,7 @@ """ this is code for forecasting but i modified it and used it for safety checker of data -for ex: you have a online shop and for some reason some data are +for ex: you have an online shop and for some reason some data are missing (the amount of data that u expected are not supposed to be) then we can use it *ps : 1. ofc we can use normal statistic method but in this case @@ -113,44 +113,46 @@ def data_safety_checker(list_vote: list, actual_result: float) -> None: print(f"Today's data is {'not ' if safe <= not_safe else ''}safe.") -# data_input_df = pd.read_csv("ex_data.csv", header=None) -data_input = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]] -data_input_df = pd.DataFrame(data_input, columns=["total_user", "total_even", "days"]) +if __name__ == "__main__": + # data_input_df = pd.read_csv("ex_data.csv", header=None) + data_input = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]] + data_input_df = pd.DataFrame( + data_input, columns=["total_user", "total_even", "days"] + ) -""" -data column = total user in a day, how much online event held in one day, -what day is that(sunday-saturday) -""" + """ + data column = total user in a day, how much online event held in one day, + what day is that(sunday-saturday) + """ + + # start normalization + normalize_df = Normalizer().fit_transform(data_input_df.values) + # split data + total_date = normalize_df[:, 2].tolist() + total_user = normalize_df[:, 0].tolist() + total_match = normalize_df[:, 1].tolist() + + # for svr (input variable = total date and total match) + x = normalize_df[:, [1, 2]].tolist() + x_train = x[: len(x) - 1] + x_test = x[len(x) - 1 :] + + # for linear regression & sarimax + trn_date = total_date[: len(total_date) - 1] + trn_user = total_user[: len(total_user) - 1] + trn_match = total_match[: len(total_match) - 1] + + tst_date = total_date[len(total_date) - 1 :] + tst_user = total_user[len(total_user) - 1 :] + tst_match = total_match[len(total_match) - 1 :] + + # voting system with forecasting + res_vote = [] + res_vote.append( + linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match) + ) + res_vote.append(sarimax_predictor(trn_user, trn_match, tst_match)) + res_vote.append(support_vector_regressor(x_train, x_test, trn_user)) -# start normalization -normalize_df = Normalizer().fit_transform(data_input_df.values) -# split data -total_date = normalize_df[:, 2].tolist() -total_user = normalize_df[:, 0].tolist() -total_match = normalize_df[:, 1].tolist() - -# for svr (input variable = total date and total match) -x = normalize_df[:, [1, 2]].tolist() -x_train = x[: len(x) - 1] -x_test = x[len(x) - 1 :] - -# for linear reression & sarimax -trn_date = total_date[: len(total_date) - 1] -trn_user = total_user[: len(total_user) - 1] -trn_match = total_match[: len(total_match) - 1] - -tst_date = total_date[len(total_date) - 1 :] -tst_user = total_user[len(total_user) - 1 :] -tst_match = total_match[len(total_match) - 1 :] - - -# voting system with forecasting -res_vote = [] -res_vote.append( - linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match) -) -res_vote.append(sarimax_predictor(trn_user, trn_match, tst_match)) -res_vote.append(support_vector_regressor(x_train, x_test, trn_user)) - -# check the safety of todays'data^^ -data_safety_checker(res_vote, tst_user) + # check the safety of today's data + data_safety_checker(res_vote, tst_user) From 63afdaf754b1f9ff9c0309867cd1432b6338ba40 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 21 Oct 2022 21:47:36 -0400 Subject: [PATCH 09/13] Refactor data_safety_checker to return instead of print --- machine_learning/forecasting/run.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/machine_learning/forecasting/run.py b/machine_learning/forecasting/run.py index ea4313c5c6db..61fd79f973b9 100644 --- a/machine_learning/forecasting/run.py +++ b/machine_learning/forecasting/run.py @@ -91,14 +91,14 @@ def interquartile_range_checker(train_user: list) -> float: return low_lim -def data_safety_checker(list_vote: list, actual_result: float) -> None: +def data_safety_checker(list_vote: list, actual_result: float) -> bool: """ Used to review all the votes (list result prediction) and compare it to the actual result. input : list of predictions output : print whether it's safe or not - >>> data_safety_checker([2,3,4],5.0) - Today's data is not safe. + >>> data_safety_checker([2, 3, 4], 5.0) + False """ safe = 0 not_safe = 0 @@ -107,10 +107,10 @@ def data_safety_checker(list_vote: list, actual_result: float) -> None: safe = not_safe + 1 else: if abs(abs(i) - abs(actual_result)) <= 0.1: - safe = safe + 1 + safe += 1 else: - not_safe = not_safe + 1 - print(f"Today's data is {'not ' if safe <= not_safe else ''}safe.") + not_safe += 1 + return safe > not_safe if __name__ == "__main__": @@ -155,4 +155,7 @@ def data_safety_checker(list_vote: list, actual_result: float) -> None: res_vote.append(support_vector_regressor(x_train, x_test, trn_user)) # check the safety of today's data - data_safety_checker(res_vote, tst_user) + if data_safety_checker(res_vote, tst_user): + print("Today's data is safe.") + else: + print("Today's data is not safe.") From 6976193efb59cae10f54af348c04026fd6590fcd Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 22 Oct 2022 02:05:46 +0000 Subject: [PATCH 10/13] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index fae9a5183f04..94ec42832e41 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -642,6 +642,7 @@ * [Tower Of Hanoi](other/tower_of_hanoi.py) ## Physics + * [Casimir Effect](physics/casimir_effect.py) * [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py) * [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py) * [N Body Simulation](physics/n_body_simulation.py) @@ -928,6 +929,7 @@ * [Deutsch Jozsa](quantum/deutsch_jozsa.py) * [Half Adder](quantum/half_adder.py) * [Not Gate](quantum/not_gate.py) + * [Q Full Adder](quantum/q_full_adder.py) * [Quantum Entanglement](quantum/quantum_entanglement.py) * [Ripple Adder Classic](quantum/ripple_adder_classic.py) * [Single Qubit Measure](quantum/single_qubit_measure.py) From 5ba6e063509ae0df26825dbb36a5a2389793cb0a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 22 Oct 2022 02:06:26 +0000 Subject: [PATCH 11/13] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1fad287988c4..70644d0639dc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -360,6 +360,7 @@ * [Dijkstra](graphs/dijkstra.py) * [Dijkstra 2](graphs/dijkstra_2.py) * [Dijkstra Algorithm](graphs/dijkstra_algorithm.py) + * [Dijkstra Alternate](graphs/dijkstra_alternate.py) * [Dinic](graphs/dinic.py) * [Directed And Undirected (Weighted) Graph](graphs/directed_and_undirected_(weighted)_graph.py) * [Edmonds Karp Multiple Source And Sink](graphs/edmonds_karp_multiple_source_and_sink.py) @@ -460,6 +461,7 @@ * [Similarity Search](machine_learning/similarity_search.py) * [Support Vector Machines](machine_learning/support_vector_machines.py) * [Word Frequency Functions](machine_learning/word_frequency_functions.py) + * [Xgboostclassifier](machine_learning/xgboostclassifier.py) ## Maths * [3N Plus 1](maths/3n_plus_1.py) @@ -534,6 +536,7 @@ * [Line Length](maths/line_length.py) * [Lucas Lehmer Primality Test](maths/lucas_lehmer_primality_test.py) * [Lucas Series](maths/lucas_series.py) + * [Maclaurin Sin](maths/maclaurin_sin.py) * [Matrix Exponentiation](maths/matrix_exponentiation.py) * [Max Sum Sliding Window](maths/max_sum_sliding_window.py) * [Median Of Two Arrays](maths/median_of_two_arrays.py) @@ -936,6 +939,7 @@ * [Not Gate](quantum/not_gate.py) * [Q Full Adder](quantum/q_full_adder.py) * [Quantum Entanglement](quantum/quantum_entanglement.py) + * [Quantum Random](quantum/quantum_random.py) * [Ripple Adder Classic](quantum/ripple_adder_classic.py) * [Single Qubit Measure](quantum/single_qubit_measure.py) From 375fa3f4357041792eb6a15b5f105eb5b4af4039 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Oct 2022 13:21:32 +0200 Subject: [PATCH 12/13] Apply suggestions from code review --- .../strassen_matrix_multiplication.py | 4 ++++ dynamic_programming/subset_generation.py | 2 +- dynamic_programming/sum_of_subset.py | 2 +- machine_learning/forecasting/run.py | 17 +++++++---------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index 075a0ffd2989..371605d6d4d4 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -67,6 +67,10 @@ def matrix_dimensions(matrix: list) -> tuple[int, int]: return len(matrix), len(matrix[0]) +def print_matrix(matrix: list) -> None: + print("\n".join(str(line) for line in matrix)) + + def actual_strassen(matrix_a: list, matrix_b: list) -> list: """ Recursive function to calculate the product of two matrices, using the Strassen diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index d88ee93f12e1..819fd8106def 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -38,7 +38,7 @@ def print_combination(arr, n, r): if __name__ == "__main__": - # Driver function to check for above function + # Driver code to check the function above arr = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu diff --git a/dynamic_programming/sum_of_subset.py b/dynamic_programming/sum_of_subset.py index 4500c9676677..96ebcf583a4b 100644 --- a/dynamic_programming/sum_of_subset.py +++ b/dynamic_programming/sum_of_subset.py @@ -8,7 +8,7 @@ def is_sum_subset(arr: list[int], required_sum: int) -> bool: # a subset value says 1 if that subset sum can be formed else 0 # initially no subsets can be formed hence False/0 arr_len = len(arr) - subset = [[False for _ in range(required_sum + 1)] for _ in range(arr_len + 1)] + subset = [[False] * (required_sum + 1) for _ in range(arr_len + 1)] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 diff --git a/machine_learning/forecasting/run.py b/machine_learning/forecasting/run.py index 61fd79f973b9..4dac0f1e2bd4 100644 --- a/machine_learning/forecasting/run.py +++ b/machine_learning/forecasting/run.py @@ -147,15 +147,12 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool: tst_match = total_match[len(total_match) - 1 :] # voting system with forecasting - res_vote = [] - res_vote.append( - linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match) - ) - res_vote.append(sarimax_predictor(trn_user, trn_match, tst_match)) - res_vote.append(support_vector_regressor(x_train, x_test, trn_user)) + res_vote = [ + linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match), + sarimax_predictor(trn_user, trn_match, tst_match), + support_vector_regressor(x_train, x_test, trn_user), + ] # check the safety of today's data - if data_safety_checker(res_vote, tst_user): - print("Today's data is safe.") - else: - print("Today's data is not safe.") + not_str = "" if data_safety_checker(res_vote, tst_user) else "not " + print("Today's data is {not_str}safe.") From 1715bad34c6e170181b470eb85fa477da9457969 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Oct 2022 11:23:18 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/forecasting/run.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/forecasting/run.py b/machine_learning/forecasting/run.py index 4dac0f1e2bd4..0909b76d8907 100644 --- a/machine_learning/forecasting/run.py +++ b/machine_learning/forecasting/run.py @@ -148,7 +148,9 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool: # voting system with forecasting res_vote = [ - linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match), + linear_regression_prediction( + trn_date, trn_user, trn_match, tst_date, tst_match + ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ]