Skip to content

Remove some print statements within algorithmic functions #7499

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 14 commits into from
Oct 22, 2022
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
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion cellular_automata/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand Down
1 change: 0 additions & 1 deletion digital_image_processing/index_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 7 additions & 5 deletions divide_and_conquer/max_subarray_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
3 changes: 1 addition & 2 deletions divide_and_conquer/strassen_matrix_multiplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def matrix_dimensions(matrix: list) -> tuple[int, int]:


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


def actual_strassen(matrix_a: list, matrix_b: list) -> list:
Expand Down
1 change: 0 additions & 1 deletion dynamic_programming/longest_sub_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/max_non_adjacent_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions dynamic_programming/subset_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
14 changes: 6 additions & 8 deletions dynamic_programming/sum_of_subset.py
Original file line number Diff line number Diff line change
@@ -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] * (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
Expand All @@ -25,10 +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]]

# uncomment to print the subset
# for i in range(arrLen+1):
# print(subset[i])
print(subset[arr_len][required_sum])
return subset[arr_len][required_sum]


if __name__ == "__main__":
Expand Down
96 changes: 50 additions & 46 deletions machine_learning/forecasting/run.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -107,50 +107,54 @@ 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


# 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 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)
# 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 = [
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
not_str = "" if data_safety_checker(res_vote, tst_user) else "not "
print("Today's data is {not_str}safe.")
Copy link
Contributor Author

@tianyizheng02 tianyizheng02 Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss This string needs to be an f-string for not_str to print correctly