Permalink
Please
sign in to comment.
Showing
with
6,100 additions
and 4,657 deletions.
- +9 −4 arithmetic_analysis/bisection.py
- +2 −5 arithmetic_analysis/in_static_equilibrium.py
- +14 −7 arithmetic_analysis/intersection.py
- +1 −3 arithmetic_analysis/lu_decomposition.py
- +3 −3 arithmetic_analysis/newton_method.py
- +12 −11 arithmetic_analysis/newton_raphson_method.py
- +2 −2 backtracking/all_combinations.py
- +17 −17 backtracking/all_permutations.py
- +14 −14 backtracking/all_subsequences.py
- +23 −17 backtracking/minimax.py
- +26 −22 backtracking/n_queens.py
- +33 −23 backtracking/sum_of_subsets.py
- +129 −112 boolean_algebra/quine_mc_cluskey.py
- +44 −26 ciphers/affine_cipher.py
- +4 −4 ciphers/atbash.py
- +7 −5 ciphers/base16.py
- +7 −5 ciphers/base32.py
- +35 −28 ciphers/base64_cipher.py
- +7 −5 ciphers/base85.py
- +4 −1 ciphers/brute_force_caesar_cipher.py
- +13 −12 ciphers/caesar_cipher.py
- +3 −2 ciphers/cryptomath_module.py
- +23 −18 ciphers/elgamal_key_generator.py
- +37 −25 ciphers/hill_cipher.py
- +60 −35 ciphers/morse_code_implementation.py
- +7 −7 ciphers/onepad_cipher.py
- +31 −29 ciphers/playfair_cipher.py
- +179 −19 ciphers/rabin_miller.py
- +7 −7 ciphers/rot13.py
- +41 −31 ciphers/rsa_cipher.py
- +26 −17 ciphers/rsa_key_generator.py
- +23 −16 ciphers/simple_substitution_cipher.py
- +50 −16 ciphers/trafid_cipher.py
- +21 −11 ciphers/transposition_cipher.py
- +16 −14 ciphers/transposition_cipher_encrypt_decrypt_file.py
- +24 −19 ciphers/vigenere_cipher.py
- +84 −88 ciphers/xor_cipher.py
- +3 −10 compression/burrows_wheeler.py
- +7 −2 compression/huffman.py
- +8 −5 compression/peak_signal_to_noise_ratio.py
- +1 −0 conversions/decimal_to_binary.py
- +23 −20 conversions/decimal_to_hexadecimal.py
- +2 −2 conversions/decimal_to_octal.py
- +88 −54 data_structures/binary_tree/avl_tree.py
- +14 −8 data_structures/binary_tree/basic_binary_tree.py
- +74 −70 data_structures/binary_tree/binary_search_tree.py
- +11 −11 data_structures/binary_tree/fenwick_tree.py
- +40 −36 data_structures/binary_tree/lazy_segment_tree.py
- +1 −1 data_structures/binary_tree/lca.py
- +0 −1 data_structures/binary_tree/red_black_tree.py
- +21 −15 data_structures/binary_tree/segment_tree.py
- +1 −0 data_structures/binary_tree/treap.py
- +15 −6 data_structures/hashing/double_hash.py
- +5 −7 data_structures/hashing/hash_table.py
- +10 −8 data_structures/hashing/hash_table_with_linked_list.py
- +10 −10 data_structures/hashing/number_theory/prime_numbers.py
- +8 −5 data_structures/hashing/quadratic_probing.py
- +16 −52 data_structures/heap/binomial_heap.py
- +70 −68 data_structures/heap/heap.py
- +1 −0 data_structures/linked_list/__init__.py
- +35 −30 data_structures/linked_list/doubly_linked_list.py
- +21 −15 data_structures/linked_list/singly_linked_list.py
- +5 −8 data_structures/linked_list/swap_nodes.py
- +3 −3 data_structures/queue/double_ended_queue.py
- +12 −6 data_structures/queue/queue_on_list.py
- +13 −6 data_structures/queue/queue_on_pseudo_stack.py
- +17 −18 data_structures/stacks/__init__.py
- +7 −7 data_structures/stacks/balanced_parentheses.py
- +15 −18 data_structures/stacks/infix_to_postfix_conversion.py
- +60 −26 data_structures/stacks/infix_to_prefix_conversion.py
- +3 −2 data_structures/stacks/next_greater_element.py
- +34 −14 data_structures/stacks/postfix_evaluation.py
- +11 −11 data_structures/stacks/stack.py
- +9 −7 data_structures/stacks/stock_span_problem.py
- +28 −18 digital_image_processing/edge_detection/canny.py
- +6 −6 digital_image_processing/filters/convolve.py
- +10 −10 digital_image_processing/filters/gaussian_filter.py
- +5 −5 digital_image_processing/filters/median_filter.py
- +7 −7 digital_image_processing/filters/sobel_filter.py
- +23 −20 divide_and_conquer/closest_pair_of_points.py
- +35 −17 divide_and_conquer/convex_hull.py
- +6 −8 divide_and_conquer/inversions.py
- +6 −5 divide_and_conquer/max_subarray_sum.py
- +27 −26 dynamic_programming/bitmask.py
- +3 −1 dynamic_programming/coin_change.py
- +42 −39 dynamic_programming/edit_distance.py
- +15 −11 dynamic_programming/factorial.py
- +4 −4 dynamic_programming/fibonacci.py
- +27 −22 dynamic_programming/floyd_warshall.py
- +14 −6 dynamic_programming/fractional_knapsack.py
- +28 −25 dynamic_programming/integer_partition.py
- +58 −52 dynamic_programming/k_means_clustering_tensorflow.py
- +34 −27 dynamic_programming/knapsack.py
- +5 −5 dynamic_programming/longest_common_subsequence.py
- +38 −33 dynamic_programming/longest_increasing_subsequence.py
- +24 −24 dynamic_programming/longest_increasing_subsequence_o(nlogn).py
- +11 −11 dynamic_programming/longest_sub_array.py
- +36 −26 dynamic_programming/matrix_chain_order.py
- +44 −42 dynamic_programming/max_sub_array.py
- +13 −11 dynamic_programming/minimum_partition.py
- +60 −53 dynamic_programming/rod_cutting.py
- +39 −35 dynamic_programming/subset_generation.py
- +12 −11 dynamic_programming/sum_of_subset.py
- +7 −7 file_transfer/recieve_file.py
- +15 −11 file_transfer/send_file.py
- +32 −30 graphs/a_star.py
- +13 −2 graphs/articulation_points.py
- +1 −1 graphs/basic_graphs.py
- +31 −31 graphs/bellman_ford.py
- +10 −8 graphs/bfs.py
- +17 −13 graphs/bfs_shortest_path.py
- +6 −5 graphs/breadth_first_search.py
- +3 −2 graphs/check_bipartite_graph_bfs.py
- +2 −2 graphs/check_bipartite_graph_dfs.py
- +7 −6 graphs/depth_first_search.py
- +13 −9 graphs/dfs.py
- +1 −0 graphs/dijkstra.py
- +37 −34 graphs/dijkstra_2.py
- +15 −11 graphs/dijkstra_algorithm.py
- +487 −462 graphs/directed_and_undirected_(weighted)_graph.py
- +23 −12 graphs/edmonds_karp_multiple_source_and_sink.py
- +4 −26 graphs/eulerian_path_and_circuit_for_undirected_graph.py
- +2 −12 graphs/even_tree.py
- +15 −4 graphs/finding_bridges.py
- +5 −3 graphs/graph_list.py
- +8 −12 graphs/graph_matrix.py
- +76 −75 graphs/graphs_floyd_warshall.py
- +4 −3 graphs/kahns_algorithm_long.py
- +3 −2 graphs/kahns_algorithm_topo.py
- +12 −12 graphs/minimum_spanning_tree_kruskal.py
- +15 −12 graphs/minimum_spanning_tree_prims.py
- +281 −222 graphs/multi_hueristic_astar.py
- +19 −19 graphs/page_rank.py
- +3 −3 graphs/prim.py
- +14 −9 graphs/scc_kosaraju.py
- +7 −3 graphs/tarjans_scc.py
- +57 −55 hashes/chaos_machine.py
- +3 −2 hashes/enigma_machine.py
- +104 −38 hashes/md5.py
- +41 −26 hashes/sha1.py
- +93 −65 linear_algebra/src/lib.py
- +75 −54 linear_algebra/src/tests.py
- +15 −10 machine_learning/decision_tree.py
- +29 −17 machine_learning/gradient_descent.py
- +63 −36 machine_learning/k_means_clust.py
- +15 −12 machine_learning/knn_sklearn.py
- +12 −12 machine_learning/linear_regression.py
- +19 −28 machine_learning/logistic_regression.py
- +53 −21 machine_learning/random_forest_classification/random_forest_classification.py
- +8 −7 machine_learning/random_forest_regression/random_forest_regression.py
- +13 −9 machine_learning/scoring_functions.py
- +154 −53 machine_learning/sequential_minimum_optimization.py
- +16 −9 maths/3n+1.py
- +1 −1 maths/abs.py
- +9 −6 maths/abs_max.py
- +3 −3 maths/abs_min.py
- +1 −1 maths/average_mean.py
- +3 −1 maths/average_median.py
- +3 −3 maths/basic_maths.py
- +2 −2 maths/binary_exponentiation.py
- +6 −5 maths/collatz_sequence.py
- +2 −2 maths/extended_euclidean_algorithm.py
- +1 −1 maths/factorial_recursive.py
- +2 −2 maths/fermat_little_theorem.py
- +15 −8 maths/fibonacci.py
- +6 −2 maths/fibonacci_sequence_recursion.py
- +8 −8 maths/find_lcm.py
- +8 −5 maths/find_max.py
- +2 −1 maths/find_min.py
- +0 −2 maths/gaussian.py
- +2 −2 maths/greater_common_divisor.py
- +1 −0 maths/lucas_series.py
- +3 −2 maths/matrix_exponentiation.py
- +1 −1 maths/modular_exponential.py
- +21 −15 maths/newton_raphson.py
- +1 −1 maths/polynomial_evaluation.py
- +8 −7 maths/prime_check.py
- +13 −55 maths/radix2_fft.py
- +1 −1 maths/segmented_sieve.py
- +23 −18 maths/simpson_rule.py
- +38 −32 maths/trapezoidal_rule.py
- +25 −25 maths/zellers_congruence.py
- +7 −3 matrix/matrix_class.py
- +33 −20 matrix/matrix_operation.py
- +2 −0 matrix/nth_fibonacci_using_matrix_exponentiation.py
- +4 −9 matrix/searching_in_sorted_matrix.py
- +36 −23 matrix/sherman_morrison.py
- +4 −2 matrix/spiral_print.py
- +13 −8 matrix/tests/test_matrix_operation.py
- +24 −19 networking_flow/ford_fulkerson.py
- +64 −49 neural_network/back_propagation_neural_network.py
- +183 −129 neural_network/convolution_neural_network.py
- +54 −19 neural_network/perceptron.py
- +13 −10 other/anagrams.py
- +4 −3 other/binary_exponentiation.py
- +3 −3 other/binary_exponentiation_2.py
- +12 −6 other/detecting_english_programmatically.py
- +4 −1 other/euclidean_gcd.py
- +10 −8 other/fischer_yates_shuffle.py
- +71 −19 other/frequency_finder.py
- +49 −38 other/game_of_life.py
- +7 −5 other/linear_congruential_generator.py
- +10 −6 other/nested_brackets.py
- +2 −2 other/palindrome.py
- +5 −6 other/password_generator.py
- +133 −90 other/primelib.py
- +38 −33 other/sierpinski_triangle.py
- +12 −9 other/tower_of_hanoi.py
- +9 −7 other/two_sum.py
- +9 −6 other/word_patterns.py
- +2 −0 project_euler/problem_01/sol1.py
- +2 −0 project_euler/problem_01/sol3.py
- +2 −0 project_euler/problem_01/sol4.py
- +2 −0 project_euler/problem_01/sol6.py
- +2 −0 project_euler/problem_02/sol1.py
- +2 −0 project_euler/problem_02/sol2.py
- +2 −0 project_euler/problem_02/sol3.py
- +3 −3 project_euler/problem_04/sol1.py
- +2 −0 project_euler/problem_04/sol2.py
- +2 −0 project_euler/problem_05/sol1.py
- +2 −0 project_euler/problem_06/sol1.py
- +2 −0 project_euler/problem_06/sol2.py
- +2 −0 project_euler/problem_07/sol2.py
- +2 −0 project_euler/problem_09/sol2.py
- +2 −0 project_euler/problem_09/sol3.py
- +3 −9 project_euler/problem_11/sol1.py
- +2 −12 project_euler/problem_11/sol2.py
- +4 −6 project_euler/problem_12/sol2.py
- +3 −7 project_euler/problem_14/sol1.py
- +2 −0 project_euler/problem_14/sol2.py
- +1 −3 project_euler/problem_15/sol1.py
- +3 −3 project_euler/problem_18/solution.py
- +3 −2 project_euler/problem_21/sol1.py
- +3 −2 project_euler/problem_23/sol1.py
- +15 −14 project_euler/problem_234/sol1.py
- +1 −0 project_euler/problem_25/sol1.py
- +2 −0 project_euler/problem_29/solution.py
- +2 −0 project_euler/problem_31/sol1.py
- +7 −9 project_euler/problem_32/sol32.py
- +2 −0 project_euler/problem_36/sol1.py
- +2 −0 project_euler/problem_40/sol1.py
- +7 −9 project_euler/problem_42/solution42.py
- +1 −1 project_euler/problem_551/sol1.py
- +10 −4 project_euler/problem_56/sol1.py
- +3 −3 project_euler/problem_67/sol1.py
- +4 −2 scripts/build_directory_md.py
- +1 −0 scripts/validate_filenames.py
- +14 −11 searches/binary_search.py
- +34 −24 searches/interpolation_search.py
- +5 −4 searches/jump_search.py
- +6 −6 searches/linear_search.py
- +13 −10 searches/quick_select.py
- +7 −6 searches/sentinel_linear_search.py
- +42 −13 searches/tabu_search.py
- +44 −41 searches/ternary_search.py
- +8 −8 sorts/bitonic_sort.py
- +4 −3 sorts/bogo_sort.py
- +8 −8 sorts/bubble_sort.py
- +9 −9 sorts/bucket_sort.py
- +9 −8 sorts/cocktail_shaker_sort.py
- +6 −5 sorts/comb_sort.py
- +7 −6 sorts/counting_sort.py
- +4 −4 sorts/cycle_sort.py
- +21 −22 sorts/external_sort.py
- +4 −4 sorts/gnome_sort.py
- +9 −7 sorts/heap_sort.py
- +13 −5 sorts/insertion_sort.py
- +10 −6 sorts/merge_sort.py
- +8 −6 sorts/merge_sort_fastest.py
- +54 −25 sorts/odd_even_transposition_parallel.py
- +4 −1 sorts/odd_even_transposition_single_threaded.py
- +6 −5 sorts/pancake_sort.py
- +18 −12 sorts/pigeon_sort.py
- +6 −4 sorts/quick_sort.py
- +5 −4 sorts/quick_sort_3_partition.py
- +15 −15 sorts/radix_sort.py
- +36 −38 sorts/random_normal_distribution_quicksort.py
- +17 −6 sorts/random_pivot_quick_sort.py
- +6 −6 sorts/selection_sort.py
- +6 −3 sorts/shell_sort.py
- +13 −16 sorts/stooge_sort.py
- +4 −4 sorts/topological_sort.py
- +2 −2 sorts/tree_sort.py
- +1 −1 sorts/wiggle_sort.py
- +9 −14 strings/boyer_moore_search.py
- +2 −2 strings/knuth_morris_pratt.py
- +8 −5 strings/levenshtein_distance.py
- +19 −15 strings/manacher.py
- +102 −98 strings/min_cost_string_conversion.py
- +15 −12 strings/naive_string_search.py
- +1 −0 traversals/binary_tree_traversals.py
@@ -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)) |
@@ -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)) |
@@ -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)) |
Oops, something went wrong.
0 comments on commit
9eac17a