Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
Permalink
Browse files

psf/black code formatting (#1277)

  • Loading branch information...
WilliamHYZhang authored and cclauss committed Oct 5, 2019
1 parent 07f04a2 commit 9eac17a4083ad08c4bb0520cb0b8e5ce385f9ce0
Showing with 6,100 additions and 4,657 deletions.
  1. +9 −4 arithmetic_analysis/bisection.py
  2. +2 −5 arithmetic_analysis/in_static_equilibrium.py
  3. +14 −7 arithmetic_analysis/intersection.py
  4. +1 −3 arithmetic_analysis/lu_decomposition.py
  5. +3 −3 arithmetic_analysis/newton_method.py
  6. +12 −11 arithmetic_analysis/newton_raphson_method.py
  7. +2 −2 backtracking/all_combinations.py
  8. +17 −17 backtracking/all_permutations.py
  9. +14 −14 backtracking/all_subsequences.py
  10. +23 −17 backtracking/minimax.py
  11. +26 −22 backtracking/n_queens.py
  12. +33 −23 backtracking/sum_of_subsets.py
  13. +129 −112 boolean_algebra/quine_mc_cluskey.py
  14. +44 −26 ciphers/affine_cipher.py
  15. +4 −4 ciphers/atbash.py
  16. +7 −5 ciphers/base16.py
  17. +7 −5 ciphers/base32.py
  18. +35 −28 ciphers/base64_cipher.py
  19. +7 −5 ciphers/base85.py
  20. +4 −1 ciphers/brute_force_caesar_cipher.py
  21. +13 −12 ciphers/caesar_cipher.py
  22. +3 −2 ciphers/cryptomath_module.py
  23. +23 −18 ciphers/elgamal_key_generator.py
  24. +37 −25 ciphers/hill_cipher.py
  25. +60 −35 ciphers/morse_code_implementation.py
  26. +7 −7 ciphers/onepad_cipher.py
  27. +31 −29 ciphers/playfair_cipher.py
  28. +179 −19 ciphers/rabin_miller.py
  29. +7 −7 ciphers/rot13.py
  30. +41 −31 ciphers/rsa_cipher.py
  31. +26 −17 ciphers/rsa_key_generator.py
  32. +23 −16 ciphers/simple_substitution_cipher.py
  33. +50 −16 ciphers/trafid_cipher.py
  34. +21 −11 ciphers/transposition_cipher.py
  35. +16 −14 ciphers/transposition_cipher_encrypt_decrypt_file.py
  36. +24 −19 ciphers/vigenere_cipher.py
  37. +84 −88 ciphers/xor_cipher.py
  38. +3 −10 compression/burrows_wheeler.py
  39. +7 −2 compression/huffman.py
  40. +8 −5 compression/peak_signal_to_noise_ratio.py
  41. +1 −0 conversions/decimal_to_binary.py
  42. +23 −20 conversions/decimal_to_hexadecimal.py
  43. +2 −2 conversions/decimal_to_octal.py
  44. +88 −54 data_structures/binary_tree/avl_tree.py
  45. +14 −8 data_structures/binary_tree/basic_binary_tree.py
  46. +74 −70 data_structures/binary_tree/binary_search_tree.py
  47. +11 −11 data_structures/binary_tree/fenwick_tree.py
  48. +40 −36 data_structures/binary_tree/lazy_segment_tree.py
  49. +1 −1 data_structures/binary_tree/lca.py
  50. +0 −1 data_structures/binary_tree/red_black_tree.py
  51. +21 −15 data_structures/binary_tree/segment_tree.py
  52. +1 −0 data_structures/binary_tree/treap.py
  53. +15 −6 data_structures/hashing/double_hash.py
  54. +5 −7 data_structures/hashing/hash_table.py
  55. +10 −8 data_structures/hashing/hash_table_with_linked_list.py
  56. +10 −10 data_structures/hashing/number_theory/prime_numbers.py
  57. +8 −5 data_structures/hashing/quadratic_probing.py
  58. +16 −52 data_structures/heap/binomial_heap.py
  59. +70 −68 data_structures/heap/heap.py
  60. +1 −0 data_structures/linked_list/__init__.py
  61. +35 −30 data_structures/linked_list/doubly_linked_list.py
  62. +21 −15 data_structures/linked_list/singly_linked_list.py
  63. +5 −8 data_structures/linked_list/swap_nodes.py
  64. +3 −3 data_structures/queue/double_ended_queue.py
  65. +12 −6 data_structures/queue/queue_on_list.py
  66. +13 −6 data_structures/queue/queue_on_pseudo_stack.py
  67. +17 −18 data_structures/stacks/__init__.py
  68. +7 −7 data_structures/stacks/balanced_parentheses.py
  69. +15 −18 data_structures/stacks/infix_to_postfix_conversion.py
  70. +60 −26 data_structures/stacks/infix_to_prefix_conversion.py
  71. +3 −2 data_structures/stacks/next_greater_element.py
  72. +34 −14 data_structures/stacks/postfix_evaluation.py
  73. +11 −11 data_structures/stacks/stack.py
  74. +9 −7 data_structures/stacks/stock_span_problem.py
  75. +28 −18 digital_image_processing/edge_detection/canny.py
  76. +6 −6 digital_image_processing/filters/convolve.py
  77. +10 −10 digital_image_processing/filters/gaussian_filter.py
  78. +5 −5 digital_image_processing/filters/median_filter.py
  79. +7 −7 digital_image_processing/filters/sobel_filter.py
  80. +23 −20 divide_and_conquer/closest_pair_of_points.py
  81. +35 −17 divide_and_conquer/convex_hull.py
  82. +6 −8 divide_and_conquer/inversions.py
  83. +6 −5 divide_and_conquer/max_subarray_sum.py
  84. +27 −26 dynamic_programming/bitmask.py
  85. +3 −1 dynamic_programming/coin_change.py
  86. +42 −39 dynamic_programming/edit_distance.py
  87. +15 −11 dynamic_programming/factorial.py
  88. +4 −4 dynamic_programming/fibonacci.py
  89. +27 −22 dynamic_programming/floyd_warshall.py
  90. +14 −6 dynamic_programming/fractional_knapsack.py
  91. +28 −25 dynamic_programming/integer_partition.py
  92. +58 −52 dynamic_programming/k_means_clustering_tensorflow.py
  93. +34 −27 dynamic_programming/knapsack.py
  94. +5 −5 dynamic_programming/longest_common_subsequence.py
  95. +38 −33 dynamic_programming/longest_increasing_subsequence.py
  96. +24 −24 dynamic_programming/longest_increasing_subsequence_o(nlogn).py
  97. +11 −11 dynamic_programming/longest_sub_array.py
  98. +36 −26 dynamic_programming/matrix_chain_order.py
  99. +44 −42 dynamic_programming/max_sub_array.py
  100. +13 −11 dynamic_programming/minimum_partition.py
  101. +60 −53 dynamic_programming/rod_cutting.py
  102. +39 −35 dynamic_programming/subset_generation.py
  103. +12 −11 dynamic_programming/sum_of_subset.py
  104. +7 −7 file_transfer/recieve_file.py
  105. +15 −11 file_transfer/send_file.py
  106. +32 −30 graphs/a_star.py
  107. +13 −2 graphs/articulation_points.py
  108. +1 −1 graphs/basic_graphs.py
  109. +31 −31 graphs/bellman_ford.py
  110. +10 −8 graphs/bfs.py
  111. +17 −13 graphs/bfs_shortest_path.py
  112. +6 −5 graphs/breadth_first_search.py
  113. +3 −2 graphs/check_bipartite_graph_bfs.py
  114. +2 −2 graphs/check_bipartite_graph_dfs.py
  115. +7 −6 graphs/depth_first_search.py
  116. +13 −9 graphs/dfs.py
  117. +1 −0 graphs/dijkstra.py
  118. +37 −34 graphs/dijkstra_2.py
  119. +15 −11 graphs/dijkstra_algorithm.py
  120. +487 −462 graphs/directed_and_undirected_(weighted)_graph.py
  121. +23 −12 graphs/edmonds_karp_multiple_source_and_sink.py
  122. +4 −26 graphs/eulerian_path_and_circuit_for_undirected_graph.py
  123. +2 −12 graphs/even_tree.py
  124. +15 −4 graphs/finding_bridges.py
  125. +5 −3 graphs/graph_list.py
  126. +8 −12 graphs/graph_matrix.py
  127. +76 −75 graphs/graphs_floyd_warshall.py
  128. +4 −3 graphs/kahns_algorithm_long.py
  129. +3 −2 graphs/kahns_algorithm_topo.py
  130. +12 −12 graphs/minimum_spanning_tree_kruskal.py
  131. +15 −12 graphs/minimum_spanning_tree_prims.py
  132. +281 −222 graphs/multi_hueristic_astar.py
  133. +19 −19 graphs/page_rank.py
  134. +3 −3 graphs/prim.py
  135. +14 −9 graphs/scc_kosaraju.py
  136. +7 −3 graphs/tarjans_scc.py
  137. +57 −55 hashes/chaos_machine.py
  138. +3 −2 hashes/enigma_machine.py
  139. +104 −38 hashes/md5.py
  140. +41 −26 hashes/sha1.py
  141. +93 −65 linear_algebra/src/lib.py
  142. +75 −54 linear_algebra/src/tests.py
  143. +15 −10 machine_learning/decision_tree.py
  144. +29 −17 machine_learning/gradient_descent.py
  145. +63 −36 machine_learning/k_means_clust.py
  146. +15 −12 machine_learning/knn_sklearn.py
  147. +12 −12 machine_learning/linear_regression.py
  148. +19 −28 machine_learning/logistic_regression.py
  149. +53 −21 machine_learning/random_forest_classification/random_forest_classification.py
  150. +8 −7 machine_learning/random_forest_regression/random_forest_regression.py
  151. +13 −9 machine_learning/scoring_functions.py
  152. +154 −53 machine_learning/sequential_minimum_optimization.py
  153. +16 −9 maths/3n+1.py
  154. +1 −1 maths/abs.py
  155. +9 −6 maths/abs_max.py
  156. +3 −3 maths/abs_min.py
  157. +1 −1 maths/average_mean.py
  158. +3 −1 maths/average_median.py
  159. +3 −3 maths/basic_maths.py
  160. +2 −2 maths/binary_exponentiation.py
  161. +6 −5 maths/collatz_sequence.py
  162. +2 −2 maths/extended_euclidean_algorithm.py
  163. +1 −1 maths/factorial_recursive.py
  164. +2 −2 maths/fermat_little_theorem.py
  165. +15 −8 maths/fibonacci.py
  166. +6 −2 maths/fibonacci_sequence_recursion.py
  167. +8 −8 maths/find_lcm.py
  168. +8 −5 maths/find_max.py
  169. +2 −1 maths/find_min.py
  170. +0 −2 maths/gaussian.py
  171. +2 −2 maths/greater_common_divisor.py
  172. +1 −0 maths/lucas_series.py
  173. +3 −2 maths/matrix_exponentiation.py
  174. +1 −1 maths/modular_exponential.py
  175. +21 −15 maths/newton_raphson.py
  176. +1 −1 maths/polynomial_evaluation.py
  177. +8 −7 maths/prime_check.py
  178. +13 −55 maths/radix2_fft.py
  179. +1 −1 maths/segmented_sieve.py
  180. +23 −18 maths/simpson_rule.py
  181. +38 −32 maths/trapezoidal_rule.py
  182. +25 −25 maths/zellers_congruence.py
  183. +7 −3 matrix/matrix_class.py
  184. +33 −20 matrix/matrix_operation.py
  185. +2 −0 matrix/nth_fibonacci_using_matrix_exponentiation.py
  186. +4 −9 matrix/searching_in_sorted_matrix.py
  187. +36 −23 matrix/sherman_morrison.py
  188. +4 −2 matrix/spiral_print.py
  189. +13 −8 matrix/tests/test_matrix_operation.py
  190. +24 −19 networking_flow/ford_fulkerson.py
  191. +64 −49 neural_network/back_propagation_neural_network.py
  192. +183 −129 neural_network/convolution_neural_network.py
  193. +54 −19 neural_network/perceptron.py
  194. +13 −10 other/anagrams.py
  195. +4 −3 other/binary_exponentiation.py
  196. +3 −3 other/binary_exponentiation_2.py
  197. +12 −6 other/detecting_english_programmatically.py
  198. +4 −1 other/euclidean_gcd.py
  199. +10 −8 other/fischer_yates_shuffle.py
  200. +71 −19 other/frequency_finder.py
  201. +49 −38 other/game_of_life.py
  202. +7 −5 other/linear_congruential_generator.py
  203. +10 −6 other/nested_brackets.py
  204. +2 −2 other/palindrome.py
  205. +5 −6 other/password_generator.py
  206. +133 −90 other/primelib.py
  207. +38 −33 other/sierpinski_triangle.py
  208. +12 −9 other/tower_of_hanoi.py
  209. +9 −7 other/two_sum.py
  210. +9 −6 other/word_patterns.py
  211. +2 −0 project_euler/problem_01/sol1.py
  212. +2 −0 project_euler/problem_01/sol3.py
  213. +2 −0 project_euler/problem_01/sol4.py
  214. +2 −0 project_euler/problem_01/sol6.py
  215. +2 −0 project_euler/problem_02/sol1.py
  216. +2 −0 project_euler/problem_02/sol2.py
  217. +2 −0 project_euler/problem_02/sol3.py
  218. +3 −3 project_euler/problem_04/sol1.py
  219. +2 −0 project_euler/problem_04/sol2.py
  220. +2 −0 project_euler/problem_05/sol1.py
  221. +2 −0 project_euler/problem_06/sol1.py
  222. +2 −0 project_euler/problem_06/sol2.py
  223. +2 −0 project_euler/problem_07/sol2.py
  224. +2 −0 project_euler/problem_09/sol2.py
  225. +2 −0 project_euler/problem_09/sol3.py
  226. +3 −9 project_euler/problem_11/sol1.py
  227. +2 −12 project_euler/problem_11/sol2.py
  228. +4 −6 project_euler/problem_12/sol2.py
  229. +3 −7 project_euler/problem_14/sol1.py
  230. +2 −0 project_euler/problem_14/sol2.py
  231. +1 −3 project_euler/problem_15/sol1.py
  232. +3 −3 project_euler/problem_18/solution.py
  233. +3 −2 project_euler/problem_21/sol1.py
  234. +3 −2 project_euler/problem_23/sol1.py
  235. +15 −14 project_euler/problem_234/sol1.py
  236. +1 −0 project_euler/problem_25/sol1.py
  237. +2 −0 project_euler/problem_29/solution.py
  238. +2 −0 project_euler/problem_31/sol1.py
  239. +7 −9 project_euler/problem_32/sol32.py
  240. +2 −0 project_euler/problem_36/sol1.py
  241. +2 −0 project_euler/problem_40/sol1.py
  242. +7 −9 project_euler/problem_42/solution42.py
  243. +1 −1 project_euler/problem_551/sol1.py
  244. +10 −4 project_euler/problem_56/sol1.py
  245. +3 −3 project_euler/problem_67/sol1.py
  246. +4 −2 scripts/build_directory_md.py
  247. +1 −0 scripts/validate_filenames.py
  248. +14 −11 searches/binary_search.py
  249. +34 −24 searches/interpolation_search.py
  250. +5 −4 searches/jump_search.py
  251. +6 −6 searches/linear_search.py
  252. +13 −10 searches/quick_select.py
  253. +7 −6 searches/sentinel_linear_search.py
  254. +42 −13 searches/tabu_search.py
  255. +44 −41 searches/ternary_search.py
  256. +8 −8 sorts/bitonic_sort.py
  257. +4 −3 sorts/bogo_sort.py
  258. +8 −8 sorts/bubble_sort.py
  259. +9 −9 sorts/bucket_sort.py
  260. +9 −8 sorts/cocktail_shaker_sort.py
  261. +6 −5 sorts/comb_sort.py
  262. +7 −6 sorts/counting_sort.py
  263. +4 −4 sorts/cycle_sort.py
  264. +21 −22 sorts/external_sort.py
  265. +4 −4 sorts/gnome_sort.py
  266. +9 −7 sorts/heap_sort.py
  267. +13 −5 sorts/insertion_sort.py
  268. +10 −6 sorts/merge_sort.py
  269. +8 −6 sorts/merge_sort_fastest.py
  270. +54 −25 sorts/odd_even_transposition_parallel.py
  271. +4 −1 sorts/odd_even_transposition_single_threaded.py
  272. +6 −5 sorts/pancake_sort.py
  273. +18 −12 sorts/pigeon_sort.py
  274. +6 −4 sorts/quick_sort.py
  275. +5 −4 sorts/quick_sort_3_partition.py
  276. +15 −15 sorts/radix_sort.py
  277. +36 −38 sorts/random_normal_distribution_quicksort.py
  278. +17 −6 sorts/random_pivot_quick_sort.py
  279. +6 −6 sorts/selection_sort.py
  280. +6 −3 sorts/shell_sort.py
  281. +13 −16 sorts/stooge_sort.py
  282. +4 −4 sorts/topological_sort.py
  283. +2 −2 sorts/tree_sort.py
  284. +1 −1 sorts/wiggle_sort.py
  285. +9 −14 strings/boyer_moore_search.py
  286. +2 −2 strings/knuth_morris_pratt.py
  287. +8 −5 strings/levenshtein_distance.py
  288. +19 −15 strings/manacher.py
  289. +102 −98 strings/min_cost_string_conversion.py
  290. +15 −12 strings/naive_string_search.py
  291. +1 −0 traversals/binary_tree_traversals.py
@@ -1,21 +1,25 @@
import math


def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
def bisection(
function, a, b
): # finds where the function becomes 0 in [a,b] using bolzano

start = a
end = b
if function(a) == 0: # one of the a or b is a root for the function
return a
elif function(b) == 0:
return b
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
elif (
function(a) * function(b) > 0
): # if none of these are root and they are both positive or negative,
# then his algorithm can't find the root
print("couldn't find root in [a,b]")
return
else:
mid = start + (end - start) / 2.0
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
@@ -27,7 +31,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us


def f(x):
return math.pow(x, 3) - 2*x - 5
return math.pow(x, 3) - 2 * x - 5


if __name__ == "__main__":
print(bisection(f, 1, 1000))
@@ -54,11 +54,8 @@ def in_static_equilibrium(
if __name__ == "__main__":
# Test to check if it works
forces = array(
[
polar_force(718.4, 180 - 30),
polar_force(879.54, 45),
polar_force(100, -90)
])
[polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)]
)

location = array([[0, 0], [0, 0], [0, 0]])

@@ -1,17 +1,24 @@
import math

def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points

def intersection(
function, x0, x1
): # function is the f we want to find its root and x0 and x1 are two random starting points
x_n = x0
x_n1 = x1
while True:
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
if abs(x_n2 - x_n1) < 10**-5:
x_n2 = x_n1 - (
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
)
if abs(x_n2 - x_n1) < 10 ** -5:
return x_n2
x_n=x_n1
x_n1=x_n2
x_n = x_n1
x_n1 = x_n2


def f(x):
return math.pow(x , 3) - (2 * x) -5
return math.pow(x, 3) - (2 * x) - 5


if __name__ == "__main__":
print(intersection(f,3,3.5))
print(intersection(f, 3, 3.5))
@@ -28,9 +28,7 @@ def LUDecompose(table):


if __name__ == "__main__":
matrix = numpy.array([[2, -2, 1],
[0, 1, 2],
[5, 3, 1]])
matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
L, U = LUDecompose(matrix)
print(L)
print(U)
@@ -8,17 +8,17 @@ def newton(function, function1, startingInt):
x_n = startingInt
while True:
x_n1 = x_n - function(x_n) / function1(x_n)
if abs(x_n - x_n1) < 10**-5:
if abs(x_n - x_n1) < 10 ** -5:
return x_n1
x_n = x_n1


def f(x):
return (x**3) - (2 * x) - 5
return (x ** 3) - (2 * x) - 5


def f1(x):
return 3 * (x**2) - 2
return 3 * (x ** 2) - 2


if __name__ == "__main__":
@@ -1,33 +1,34 @@
# Implementing Newton Raphson method in Python
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
#The Newton-Raphson method (also known as Newton's method) is a way to
#quickly find a good approximation for the root of a real-valued function
# The Newton-Raphson method (also known as Newton's method) is a way to
# quickly find a good approximation for the root of a real-valued function
from sympy import diff
from decimal import Decimal


def NewtonRaphson(func, a):
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
""" Finds root from the point 'a' onwards by Newton-Raphson method """
while True:
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func)))))

a = c

# This number dictates the accuracy of the answer
if abs(eval(func)) < 10**-15:
return c
if abs(eval(func)) < 10 ** -15:
return c


# Let's Execute
if __name__ == '__main__':
if __name__ == "__main__":
# Find root of trigonometric function
# Find value of pi
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))
print("sin(x) = 0", NewtonRaphson("sin(x)", 2))

# Find root of polynomial
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
print("x**2 - 5*x +2 = 0", NewtonRaphson("x**2 - 5*x +2", 0.4))

# Find Square Root of 5
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
print("x**2 - 5 = 0", NewtonRaphson("x**2 - 5", 0.1))

# Exponential Roots
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
print("exp(x) - 1 = 0", NewtonRaphson("exp(x) - 1", 0))
@@ -12,7 +12,7 @@ def generate_all_combinations(n: int, k: int) -> [[int]]:
>>> generate_all_combinations(n=4, k=2)
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
"""

result = []
create_all_state(1, n, k, [], result)
return result
@@ -34,7 +34,7 @@ def print_all_state(total_list):
print(*i)


if __name__ == '__main__':
if __name__ == "__main__":
n = 4
k = 2
total_list = generate_all_combinations(n, k)
@@ -1,42 +1,42 @@
'''
"""
In this problem, we want to determine all possible permutations
of the given sequence. We use backtracking to solve this problem.
Time complexity: O(n! * n),
where n denotes the length of the given sequence.
'''
"""


def generate_all_permutations(sequence):
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])


def create_state_space_tree(sequence, current_sequence, index, index_used):
'''
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly len(sequence) - index children.
It terminates when it reaches the end of the given sequence.
'''
"""

if index == len(sequence):
print(current_sequence)
return
if index == len(sequence):
print(current_sequence)
return

for i in range(len(sequence)):
if not index_used[i]:
current_sequence.append(sequence[i])
index_used[i] = True
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
current_sequence.pop()
index_used[i] = False
for i in range(len(sequence)):
if not index_used[i]:
current_sequence.append(sequence[i])
index_used[i] = True
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
current_sequence.pop()
index_used[i] = False


'''
"""
remove the comment to take an input from the user
print("Enter the elements")
sequence = list(map(int, input().split()))
'''
"""

sequence = [3, 1, 2, 4]
generate_all_permutations(sequence)
@@ -1,39 +1,39 @@
'''
"""
In this problem, we want to determine all possible subsequences
of the given sequence. We use backtracking to solve this problem.
Time complexity: O(2^n),
where n denotes the length of the given sequence.
'''
"""


def generate_all_subsequences(sequence):
create_state_space_tree(sequence, [], 0)
create_state_space_tree(sequence, [], 0)


def create_state_space_tree(sequence, current_subsequence, index):
'''
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly two children.
It terminates when it reaches the end of the given sequence.
'''
"""

if index == len(sequence):
print(current_subsequence)
return
if index == len(sequence):
print(current_subsequence)
return

create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.append(sequence[index])
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.pop()
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.append(sequence[index])
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.pop()


'''
"""
remove the comment to take an input from the user
print("Enter the elements")
sequence = list(map(int, input().split()))
'''
"""

sequence = [3, 1, 2, 4]
generate_all_subsequences(sequence)
@@ -1,28 +1,34 @@
import math
import math

''' Minimax helps to achieve maximum score in a game by checking all possible moves
""" Minimax helps to achieve maximum score in a game by checking all possible moves
depth is current depth in game tree.
nodeIndex is index of current node in scores[].
if move is of maximizer return true else false
leaves of game tree is stored in scores[]
height is maximum height of Game tree
'''
"""

def minimax (Depth, nodeIndex, isMax, scores, height):

if Depth == height:
return scores[nodeIndex]
def minimax(Depth, nodeIndex, isMax, scores, height):

if isMax:
return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)))
return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)))
if Depth == height:
return scores[nodeIndex]

if __name__ == "__main__":

scores = [90, 23, 6, 33, 21, 65, 123, 34423]
height = math.log(len(scores), 2)
if isMax:
return max(
minimax(Depth + 1, nodeIndex * 2, False, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height),
)
return min(
minimax(Depth + 1, nodeIndex * 2, True, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height),
)

print("Optimal value : ", end = "")
print(minimax(0, 0, True, scores, height))

if __name__ == "__main__":

scores = [90, 23, 6, 33, 21, 65, 123, 34423]
height = math.log(len(scores), 2)

print("Optimal value : ", end="")
print(minimax(0, 0, True, scores, height))

0 comments on commit 9eac17a

Please sign in to comment.
You can’t perform that action at this time.