From e159120e000c34e1c2ee1785ff70415a6a6c552d Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Tue, 12 Jun 2018 19:21:17 +0800 Subject: [PATCH 01/19] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {data_structures => algorithms}/__init__.py | 0 .../arithmetic_analysis}/Bisection.py | 0 .../arithmetic_analysis}/Intersection.py | 0 .../arithmetic_analysis}/LUdecomposition.py | 0 .../arithmetic_analysis}/NeutonMethod.py | 0 .../NewtonRaphsonMethod.py | 0 .../Quine_McCluskey/QuineMcCluskey.py | 0 .../ciphers}/Onepad_Cipher.py | 0 .../ciphers}/Prehistoric Men.txt | 0 {ciphers => algorithms/ciphers}/XOR_cipher.py | 0 .../ciphers}/affine_cipher.py | 0 .../ciphers}/brute-force_caesar_cipher.py | 0 .../ciphers}/caesar_cipher.py | 0 .../ciphers}/cryptomath_module.py | 0 .../ciphers}/playfair_cipher.py | 0 .../ciphers}/rabin_miller.py | 0 {ciphers => algorithms/ciphers}/rot13.py | 0 {ciphers => algorithms/ciphers}/rsa_cipher.py | 0 .../ciphers}/rsa_key_generator.py | 0 .../ciphers}/simple_substitution_cipher.py | 0 .../ciphers}/transposition_cipher.py | 0 ...ansposition_cipher_encrypt-decrypt_file.py | 0 .../ciphers}/vigenere_cipher.py | 0 .../ftp_client_server.py | 0 .../ftp_send_receive.py | 0 {Graphs => algorithms/graphs}/a_star.py | 0 {Graphs => algorithms/graphs}/basic-graphs.py | 6 +- .../graphs}/minimum_spanning_tree_kruskal.py | 0 {Graphs => algorithms/graphs}/scc_kosaraju.py | 0 {Graphs => algorithms/graphs}/tarjans_scc.py | 0 .../hashes}/chaos_machine.py | 0 {hashes => algorithms/hashes}/md5.py | 0 .../linear_algebra_python/src/__init__.py | 0 .../maths}/ModularExponential.py | 0 {Maths => algorithms/maths}/SimpsonRule.py | 0 .../maths}/TrapezoidalRule.py | 0 .../networking_flow}/Ford_Fulkerson.py | 0 .../networking_flow}/Minimum_cut.py | 0 .../searches}/binary_search.py | 0 .../searches}/interpolation_search.py | 0 .../searches}/jump_search.py | 0 .../searches}/linear_search.py | 0 .../searches}/quick_select.py | 0 .../searches}/ternary_search.py | 0 {sorts => algorithms/sorts}/bogosort.py | 0 {sorts => algorithms/sorts}/bubble_sort.py | 0 {sorts => algorithms/sorts}/bucket_sort.py | 0 .../sorts}/cocktail_shaker_sort.py | 0 {sorts => algorithms/sorts}/counting_sort.py | 0 {sorts => algorithms/sorts}/countingsort.py | 0 {sorts => algorithms/sorts}/cyclesort.py | 0 {sorts => algorithms/sorts}/external-sort.py | 0 {sorts => algorithms/sorts}/gnome_sort.py | 0 {sorts => algorithms/sorts}/heap_sort.py | 0 {sorts => algorithms/sorts}/insertion_sort.py | 0 {sorts => algorithms/sorts}/merge_sort.py | 0 .../normal_distribution_QuickSort_README.md | 0 {sorts => algorithms/sorts}/pancake_sort.py | 0 {sorts => algorithms/sorts}/quick_sort.py | 0 {sorts => algorithms/sorts}/radix_sort.py | 0 .../random_normaldistribution_quicksort.py | 0 {sorts => algorithms/sorts}/selection_sort.py | 0 {sorts => algorithms/sorts}/shell_sort.py | 0 {sorts => algorithms/sorts}/sortinggraphs.png | Bin {sorts => algorithms/sorts}/timsort.py | 0 .../sorts}/topological_sort.py | 0 {sorts => algorithms/sorts}/tree_sort.py | 0 .../strings}/knuth-morris-pratt.py | 0 .../strings}/min-cost-string-conversion.py | 0 {strings => algorithms/strings}/rabin-karp.py | 0 .../traversals}/binary_tree_traversals.py | 0 data_structures/Trie/Trie.py | 2 +- linear-algebra-python/README.md | 74 ---- linear-algebra-python/src/lib.py | 364 ------------------ linear-algebra-python/src/tests.py | 133 ------- .../dynamic_programming}/FloydWarshall.py | 74 ++-- .../dynamic_programming}/abbreviation.py | 0 .../dynamic_programming}/coin_change.py | 0 .../dynamic_programming}/edit_distance.py | 0 .../dynamic_programming}/fastfibonacci.py | 0 .../dynamic_programming}/fibonacci.py | 0 .../dynamic_programming}/integer_partition.py | 0 .../k_means_clustering_tensorflow.py | 0 .../dynamic_programming}/knapsack.py | 0 .../longest_common_subsequence.py | 0 .../longest_increasing_subsequence.py | 0 ...longest_increasing_subsequence_O(nlogn).py | 0 .../dynamic_programming}/longest_sub_array.py | 0 .../matrix_chain_order.py | 0 .../dynamic_programming}/max_sub_array.py | 0 .../dynamic_programming}/minimum_partition.py | 0 .../machine_learning}/decision_tree.py | 2 +- .../machine_learning}/gradient_descent.py | 0 .../machine_learning}/k_means_clust.py | 0 .../machine_learning}/linear_regression.py | 2 +- .../machine_learning}/perceptron.py | 0 .../machine_learning}/scoring_functions.py | 0 .../neural_network}/bpnn.py | 0 .../convolution_neural_network.py | 0 .../neural_network}/perceptron.py | 0 other/project_euler/Problem 01/__init__.py | 0 .../project_euler}/Problem 01/sol1.py | 0 .../project_euler}/Problem 01/sol2.py | 0 .../project_euler}/Problem 01/sol3.py | 0 .../project_euler}/Problem 01/sol4.py | 60 +-- other/project_euler/Problem 02/__init__.py | 0 .../project_euler}/Problem 02/sol1.py | 0 .../project_euler}/Problem 02/sol2.py | 26 +- .../project_euler}/Problem 02/sol3.py | 0 other/project_euler/Problem 03/__init__.py | 0 .../project_euler}/Problem 03/sol1.py | 0 .../project_euler}/Problem 03/sol2.py | 0 other/project_euler/Problem 04/__init__.py | 0 .../project_euler}/Problem 04/sol1.py | 0 .../project_euler}/Problem 04/sol2.py | 0 other/project_euler/Problem 05/__init__.py | 0 .../project_euler}/Problem 05/sol1.py | 0 .../project_euler}/Problem 05/sol2.py | 0 other/project_euler/Problem 06/__init__.py | 0 .../project_euler}/Problem 06/sol1.py | 0 .../project_euler}/Problem 06/sol2.py | 0 other/project_euler/Problem 07/__init__.py | 0 .../project_euler}/Problem 07/sol1.py | 0 .../project_euler}/Problem 07/sol2.py | 0 other/project_euler/Problem 08/__init__.py | 0 .../project_euler}/Problem 08/sol1.py | 0 other/project_euler/Problem 10/__init__.py | 0 .../project_euler}/Problem 10/sol1.py | 0 other/project_euler/Problem 11/__init__.py | 0 .../project_euler}/Problem 11/grid.txt | 0 .../project_euler}/Problem 11/sol1.py | 0 .../project_euler}/Problem 11/sol2.py | 0 other/project_euler/Problem 12/__init__.py | 0 .../project_euler}/Problem 12/sol1.py | 0 other/project_euler/Problem 13/__init__.py | 0 .../project_euler}/Problem 13/sol1.py | 0 other/project_euler/Problem 14/__init__.py | 0 .../project_euler}/Problem 14/sol1.py | 0 other/project_euler/Problem 15/__init__.py | 0 .../project_euler}/Problem 15/sol1.py | 0 other/project_euler/Problem 16/__init__.py | 0 .../project_euler}/Problem 16/sol1.py | 0 other/project_euler/Problem 17/__init__.py | 0 .../project_euler}/Problem 17/sol1.py | 0 other/project_euler/Problem 19/__init__.py | 0 .../project_euler}/Problem 19/sol1.py | 0 other/project_euler/Problem 20/__init__.py | 0 .../project_euler}/Problem 20/sol1.py | 0 .../project_euler}/Problem 20/sol2.py | 0 other/project_euler/Problem 21/__init__.py | 0 .../project_euler}/Problem 21/sol1.py | 0 other/project_euler/Problem 22/__init__.py | 0 .../project_euler}/Problem 22/p022_names.txt | 0 .../project_euler}/Problem 22/sol1.py | 0 .../project_euler}/Problem 22/sol2.py | 0 other/project_euler/Problem 24/__init__.py | 0 .../project_euler}/Problem 24/sol1.py | 0 other/project_euler/Problem 25/__init__.py | 0 .../project_euler}/Problem 25/sol1.py | 0 other/project_euler/Problem 28/__init__.py | 0 .../project_euler}/Problem 28/sol1.py | 0 other/project_euler/Problem 29/__init__.py | 0 .../project_euler}/Problem 29/solution.py | 0 other/project_euler/Problem 36/__init__.py | 0 .../project_euler}/Problem 36/sol1.py | 0 other/project_euler/Problem 40/__init__.py | 0 .../project_euler}/Problem 40/sol1.py | 0 other/project_euler/Problem 48/__init__.py | 0 .../project_euler}/Problem 48/sol1.py | 0 other/project_euler/Problem 52/__init__.py | 0 .../project_euler}/Problem 52/sol1.py | 0 other/project_euler/Problem 53/__init__.py | 0 .../project_euler}/Problem 53/sol1.py | 0 other/project_euler/Problem 76/__init__.py | 0 .../project_euler}/Problem 76/sol1.py | 0 other/project_euler/Problem 9/__init__.py | 0 .../project_euler}/Problem 9/sol1.py | 0 .../project_euler}/Problem 9/sol2.py | 0 .../project_euler}/README.md | 0 other/sierpinski_triangle.py | 2 +- .../simple_client_server}/README.md | 0 .../simple_client_server}/client.py | 0 .../simple_client_server}/server.py | 0 183 files changed, 87 insertions(+), 658 deletions(-) rename {data_structures => algorithms}/__init__.py (100%) rename {ArithmeticAnalysis => algorithms/arithmetic_analysis}/Bisection.py (100%) rename {ArithmeticAnalysis => algorithms/arithmetic_analysis}/Intersection.py (100%) rename {ArithmeticAnalysis => algorithms/arithmetic_analysis}/LUdecomposition.py (100%) rename {ArithmeticAnalysis => algorithms/arithmetic_analysis}/NeutonMethod.py (100%) rename {ArithmeticAnalysis => algorithms/arithmetic_analysis}/NewtonRaphsonMethod.py (100%) rename {boolean_algebra => algorithms/boolean_algebra}/Quine_McCluskey/QuineMcCluskey.py (100%) rename {ciphers => algorithms/ciphers}/Onepad_Cipher.py (100%) rename {ciphers => algorithms/ciphers}/Prehistoric Men.txt (100%) rename {ciphers => algorithms/ciphers}/XOR_cipher.py (100%) rename {ciphers => algorithms/ciphers}/affine_cipher.py (100%) rename {ciphers => algorithms/ciphers}/brute-force_caesar_cipher.py (100%) rename {ciphers => algorithms/ciphers}/caesar_cipher.py (100%) rename {ciphers => algorithms/ciphers}/cryptomath_module.py (100%) rename {ciphers => algorithms/ciphers}/playfair_cipher.py (100%) rename {ciphers => algorithms/ciphers}/rabin_miller.py (100%) rename {ciphers => algorithms/ciphers}/rot13.py (100%) rename {ciphers => algorithms/ciphers}/rsa_cipher.py (100%) rename {ciphers => algorithms/ciphers}/rsa_key_generator.py (100%) rename {ciphers => algorithms/ciphers}/simple_substitution_cipher.py (100%) rename {ciphers => algorithms/ciphers}/transposition_cipher.py (100%) rename {ciphers => algorithms/ciphers}/transposition_cipher_encrypt-decrypt_file.py (100%) rename {ciphers => algorithms/ciphers}/vigenere_cipher.py (100%) rename {File_Transfer_Protocol => algorithms/file_transfer_protocol}/ftp_client_server.py (100%) rename {File_Transfer_Protocol => algorithms/file_transfer_protocol}/ftp_send_receive.py (100%) rename {Graphs => algorithms/graphs}/a_star.py (100%) rename {Graphs => algorithms/graphs}/basic-graphs.py (98%) rename {Graphs => algorithms/graphs}/minimum_spanning_tree_kruskal.py (100%) rename {Graphs => algorithms/graphs}/scc_kosaraju.py (100%) rename {Graphs => algorithms/graphs}/tarjans_scc.py (100%) rename {hashes => algorithms/hashes}/chaos_machine.py (100%) rename {hashes => algorithms/hashes}/md5.py (100%) create mode 100644 algorithms/linear_algebra_python/src/__init__.py rename {Maths => algorithms/maths}/ModularExponential.py (100%) rename {Maths => algorithms/maths}/SimpsonRule.py (100%) rename {Maths => algorithms/maths}/TrapezoidalRule.py (100%) rename {networking_flow => algorithms/networking_flow}/Ford_Fulkerson.py (100%) rename {networking_flow => algorithms/networking_flow}/Minimum_cut.py (100%) rename {searches => algorithms/searches}/binary_search.py (100%) rename {searches => algorithms/searches}/interpolation_search.py (100%) rename {searches => algorithms/searches}/jump_search.py (100%) rename {searches => algorithms/searches}/linear_search.py (100%) rename {searches => algorithms/searches}/quick_select.py (100%) rename {searches => algorithms/searches}/ternary_search.py (100%) rename {sorts => algorithms/sorts}/bogosort.py (100%) rename {sorts => algorithms/sorts}/bubble_sort.py (100%) rename {sorts => algorithms/sorts}/bucket_sort.py (100%) rename {sorts => algorithms/sorts}/cocktail_shaker_sort.py (100%) rename {sorts => algorithms/sorts}/counting_sort.py (100%) rename {sorts => algorithms/sorts}/countingsort.py (100%) rename {sorts => algorithms/sorts}/cyclesort.py (100%) rename {sorts => algorithms/sorts}/external-sort.py (100%) rename {sorts => algorithms/sorts}/gnome_sort.py (100%) rename {sorts => algorithms/sorts}/heap_sort.py (100%) rename {sorts => algorithms/sorts}/insertion_sort.py (100%) rename {sorts => algorithms/sorts}/merge_sort.py (100%) rename {sorts => algorithms/sorts}/normal_distribution_QuickSort_README.md (100%) rename {sorts => algorithms/sorts}/pancake_sort.py (100%) rename {sorts => algorithms/sorts}/quick_sort.py (100%) rename {sorts => algorithms/sorts}/radix_sort.py (100%) rename {sorts => algorithms/sorts}/random_normaldistribution_quicksort.py (100%) rename {sorts => algorithms/sorts}/selection_sort.py (100%) rename {sorts => algorithms/sorts}/shell_sort.py (100%) rename {sorts => algorithms/sorts}/sortinggraphs.png (100%) rename {sorts => algorithms/sorts}/timsort.py (100%) rename {sorts => algorithms/sorts}/topological_sort.py (100%) rename {sorts => algorithms/sorts}/tree_sort.py (100%) rename {strings => algorithms/strings}/knuth-morris-pratt.py (100%) rename {strings => algorithms/strings}/min-cost-string-conversion.py (100%) rename {strings => algorithms/strings}/rabin-karp.py (100%) rename {traversals => algorithms/traversals}/binary_tree_traversals.py (100%) delete mode 100644 linear-algebra-python/README.md delete mode 100644 linear-algebra-python/src/lib.py delete mode 100644 linear-algebra-python/src/tests.py rename {dynamic_programming => other/dynamic_programming}/FloydWarshall.py (96%) rename {dynamic_programming => other/dynamic_programming}/abbreviation.py (100%) rename {dynamic_programming => other/dynamic_programming}/coin_change.py (100%) rename {dynamic_programming => other/dynamic_programming}/edit_distance.py (100%) rename {dynamic_programming => other/dynamic_programming}/fastfibonacci.py (100%) rename {dynamic_programming => other/dynamic_programming}/fibonacci.py (100%) rename {dynamic_programming => other/dynamic_programming}/integer_partition.py (100%) rename {dynamic_programming => other/dynamic_programming}/k_means_clustering_tensorflow.py (100%) rename {dynamic_programming => other/dynamic_programming}/knapsack.py (100%) rename {dynamic_programming => other/dynamic_programming}/longest_common_subsequence.py (100%) rename {dynamic_programming => other/dynamic_programming}/longest_increasing_subsequence.py (100%) rename {dynamic_programming => other/dynamic_programming}/longest_increasing_subsequence_O(nlogn).py (100%) rename {dynamic_programming => other/dynamic_programming}/longest_sub_array.py (100%) rename {dynamic_programming => other/dynamic_programming}/matrix_chain_order.py (100%) rename {dynamic_programming => other/dynamic_programming}/max_sub_array.py (100%) rename {dynamic_programming => other/dynamic_programming}/minimum_partition.py (100%) rename {machine_learning => other/machine_learning}/decision_tree.py (98%) rename {machine_learning => other/machine_learning}/gradient_descent.py (100%) rename {machine_learning => other/machine_learning}/k_means_clust.py (100%) rename {machine_learning => other/machine_learning}/linear_regression.py (97%) rename {Neural_Network => other/machine_learning}/perceptron.py (100%) rename {machine_learning => other/machine_learning}/scoring_functions.py (100%) rename {Neural_Network => other/neural_network}/bpnn.py (100%) rename {Neural_Network => other/neural_network}/convolution_neural_network.py (100%) rename {machine_learning => other/neural_network}/perceptron.py (100%) create mode 100644 other/project_euler/Problem 01/__init__.py rename {Project Euler => other/project_euler}/Problem 01/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 01/sol2.py (100%) rename {Project Euler => other/project_euler}/Problem 01/sol3.py (100%) rename {Project Euler => other/project_euler}/Problem 01/sol4.py (94%) create mode 100644 other/project_euler/Problem 02/__init__.py rename {Project Euler => other/project_euler}/Problem 02/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 02/sol2.py (94%) rename {Project Euler => other/project_euler}/Problem 02/sol3.py (100%) create mode 100644 other/project_euler/Problem 03/__init__.py rename {Project Euler => other/project_euler}/Problem 03/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 03/sol2.py (100%) create mode 100644 other/project_euler/Problem 04/__init__.py rename {Project Euler => other/project_euler}/Problem 04/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 04/sol2.py (100%) create mode 100644 other/project_euler/Problem 05/__init__.py rename {Project Euler => other/project_euler}/Problem 05/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 05/sol2.py (100%) create mode 100644 other/project_euler/Problem 06/__init__.py rename {Project Euler => other/project_euler}/Problem 06/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 06/sol2.py (100%) create mode 100644 other/project_euler/Problem 07/__init__.py rename {Project Euler => other/project_euler}/Problem 07/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 07/sol2.py (100%) create mode 100644 other/project_euler/Problem 08/__init__.py rename {Project Euler => other/project_euler}/Problem 08/sol1.py (100%) create mode 100644 other/project_euler/Problem 10/__init__.py rename {Project Euler => other/project_euler}/Problem 10/sol1.py (100%) create mode 100644 other/project_euler/Problem 11/__init__.py rename {Project Euler => other/project_euler}/Problem 11/grid.txt (100%) rename {Project Euler => other/project_euler}/Problem 11/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 11/sol2.py (100%) create mode 100644 other/project_euler/Problem 12/__init__.py rename {Project Euler => other/project_euler}/Problem 12/sol1.py (100%) create mode 100644 other/project_euler/Problem 13/__init__.py rename {Project Euler => other/project_euler}/Problem 13/sol1.py (100%) create mode 100644 other/project_euler/Problem 14/__init__.py rename {Project Euler => other/project_euler}/Problem 14/sol1.py (100%) create mode 100644 other/project_euler/Problem 15/__init__.py rename {Project Euler => other/project_euler}/Problem 15/sol1.py (100%) create mode 100644 other/project_euler/Problem 16/__init__.py rename {Project Euler => other/project_euler}/Problem 16/sol1.py (100%) create mode 100644 other/project_euler/Problem 17/__init__.py rename {Project Euler => other/project_euler}/Problem 17/sol1.py (100%) create mode 100644 other/project_euler/Problem 19/__init__.py rename {Project Euler => other/project_euler}/Problem 19/sol1.py (100%) create mode 100644 other/project_euler/Problem 20/__init__.py rename {Project Euler => other/project_euler}/Problem 20/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 20/sol2.py (100%) create mode 100644 other/project_euler/Problem 21/__init__.py rename {Project Euler => other/project_euler}/Problem 21/sol1.py (100%) create mode 100644 other/project_euler/Problem 22/__init__.py rename {Project Euler => other/project_euler}/Problem 22/p022_names.txt (100%) rename {Project Euler => other/project_euler}/Problem 22/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 22/sol2.py (100%) create mode 100644 other/project_euler/Problem 24/__init__.py rename {Project Euler => other/project_euler}/Problem 24/sol1.py (100%) create mode 100644 other/project_euler/Problem 25/__init__.py rename {Project Euler => other/project_euler}/Problem 25/sol1.py (100%) create mode 100644 other/project_euler/Problem 28/__init__.py rename {Project Euler => other/project_euler}/Problem 28/sol1.py (100%) create mode 100644 other/project_euler/Problem 29/__init__.py rename {Project Euler => other/project_euler}/Problem 29/solution.py (100%) create mode 100644 other/project_euler/Problem 36/__init__.py rename {Project Euler => other/project_euler}/Problem 36/sol1.py (100%) create mode 100644 other/project_euler/Problem 40/__init__.py rename {Project Euler => other/project_euler}/Problem 40/sol1.py (100%) create mode 100644 other/project_euler/Problem 48/__init__.py rename {Project Euler => other/project_euler}/Problem 48/sol1.py (100%) create mode 100644 other/project_euler/Problem 52/__init__.py rename {Project Euler => other/project_euler}/Problem 52/sol1.py (100%) create mode 100644 other/project_euler/Problem 53/__init__.py rename {Project Euler => other/project_euler}/Problem 53/sol1.py (100%) create mode 100644 other/project_euler/Problem 76/__init__.py rename {Project Euler => other/project_euler}/Problem 76/sol1.py (100%) create mode 100644 other/project_euler/Problem 9/__init__.py rename {Project Euler => other/project_euler}/Problem 9/sol1.py (100%) rename {Project Euler => other/project_euler}/Problem 9/sol2.py (100%) rename {Project Euler => other/project_euler}/README.md (100%) rename {simple-client-server => other/simple_client_server}/README.md (100%) rename {simple-client-server => other/simple_client_server}/client.py (100%) rename {simple-client-server => other/simple_client_server}/server.py (100%) diff --git a/data_structures/__init__.py b/algorithms/__init__.py similarity index 100% rename from data_structures/__init__.py rename to algorithms/__init__.py diff --git a/ArithmeticAnalysis/Bisection.py b/algorithms/arithmetic_analysis/Bisection.py similarity index 100% rename from ArithmeticAnalysis/Bisection.py rename to algorithms/arithmetic_analysis/Bisection.py diff --git a/ArithmeticAnalysis/Intersection.py b/algorithms/arithmetic_analysis/Intersection.py similarity index 100% rename from ArithmeticAnalysis/Intersection.py rename to algorithms/arithmetic_analysis/Intersection.py diff --git a/ArithmeticAnalysis/LUdecomposition.py b/algorithms/arithmetic_analysis/LUdecomposition.py similarity index 100% rename from ArithmeticAnalysis/LUdecomposition.py rename to algorithms/arithmetic_analysis/LUdecomposition.py diff --git a/ArithmeticAnalysis/NeutonMethod.py b/algorithms/arithmetic_analysis/NeutonMethod.py similarity index 100% rename from ArithmeticAnalysis/NeutonMethod.py rename to algorithms/arithmetic_analysis/NeutonMethod.py diff --git a/ArithmeticAnalysis/NewtonRaphsonMethod.py b/algorithms/arithmetic_analysis/NewtonRaphsonMethod.py similarity index 100% rename from ArithmeticAnalysis/NewtonRaphsonMethod.py rename to algorithms/arithmetic_analysis/NewtonRaphsonMethod.py diff --git a/boolean_algebra/Quine_McCluskey/QuineMcCluskey.py b/algorithms/boolean_algebra/Quine_McCluskey/QuineMcCluskey.py similarity index 100% rename from boolean_algebra/Quine_McCluskey/QuineMcCluskey.py rename to algorithms/boolean_algebra/Quine_McCluskey/QuineMcCluskey.py diff --git a/ciphers/Onepad_Cipher.py b/algorithms/ciphers/Onepad_Cipher.py similarity index 100% rename from ciphers/Onepad_Cipher.py rename to algorithms/ciphers/Onepad_Cipher.py diff --git a/ciphers/Prehistoric Men.txt b/algorithms/ciphers/Prehistoric Men.txt similarity index 100% rename from ciphers/Prehistoric Men.txt rename to algorithms/ciphers/Prehistoric Men.txt diff --git a/ciphers/XOR_cipher.py b/algorithms/ciphers/XOR_cipher.py similarity index 100% rename from ciphers/XOR_cipher.py rename to algorithms/ciphers/XOR_cipher.py diff --git a/ciphers/affine_cipher.py b/algorithms/ciphers/affine_cipher.py similarity index 100% rename from ciphers/affine_cipher.py rename to algorithms/ciphers/affine_cipher.py diff --git a/ciphers/brute-force_caesar_cipher.py b/algorithms/ciphers/brute-force_caesar_cipher.py similarity index 100% rename from ciphers/brute-force_caesar_cipher.py rename to algorithms/ciphers/brute-force_caesar_cipher.py diff --git a/ciphers/caesar_cipher.py b/algorithms/ciphers/caesar_cipher.py similarity index 100% rename from ciphers/caesar_cipher.py rename to algorithms/ciphers/caesar_cipher.py diff --git a/ciphers/cryptomath_module.py b/algorithms/ciphers/cryptomath_module.py similarity index 100% rename from ciphers/cryptomath_module.py rename to algorithms/ciphers/cryptomath_module.py diff --git a/ciphers/playfair_cipher.py b/algorithms/ciphers/playfair_cipher.py similarity index 100% rename from ciphers/playfair_cipher.py rename to algorithms/ciphers/playfair_cipher.py diff --git a/ciphers/rabin_miller.py b/algorithms/ciphers/rabin_miller.py similarity index 100% rename from ciphers/rabin_miller.py rename to algorithms/ciphers/rabin_miller.py diff --git a/ciphers/rot13.py b/algorithms/ciphers/rot13.py similarity index 100% rename from ciphers/rot13.py rename to algorithms/ciphers/rot13.py diff --git a/ciphers/rsa_cipher.py b/algorithms/ciphers/rsa_cipher.py similarity index 100% rename from ciphers/rsa_cipher.py rename to algorithms/ciphers/rsa_cipher.py diff --git a/ciphers/rsa_key_generator.py b/algorithms/ciphers/rsa_key_generator.py similarity index 100% rename from ciphers/rsa_key_generator.py rename to algorithms/ciphers/rsa_key_generator.py diff --git a/ciphers/simple_substitution_cipher.py b/algorithms/ciphers/simple_substitution_cipher.py similarity index 100% rename from ciphers/simple_substitution_cipher.py rename to algorithms/ciphers/simple_substitution_cipher.py diff --git a/ciphers/transposition_cipher.py b/algorithms/ciphers/transposition_cipher.py similarity index 100% rename from ciphers/transposition_cipher.py rename to algorithms/ciphers/transposition_cipher.py diff --git a/ciphers/transposition_cipher_encrypt-decrypt_file.py b/algorithms/ciphers/transposition_cipher_encrypt-decrypt_file.py similarity index 100% rename from ciphers/transposition_cipher_encrypt-decrypt_file.py rename to algorithms/ciphers/transposition_cipher_encrypt-decrypt_file.py diff --git a/ciphers/vigenere_cipher.py b/algorithms/ciphers/vigenere_cipher.py similarity index 100% rename from ciphers/vigenere_cipher.py rename to algorithms/ciphers/vigenere_cipher.py diff --git a/File_Transfer_Protocol/ftp_client_server.py b/algorithms/file_transfer_protocol/ftp_client_server.py similarity index 100% rename from File_Transfer_Protocol/ftp_client_server.py rename to algorithms/file_transfer_protocol/ftp_client_server.py diff --git a/File_Transfer_Protocol/ftp_send_receive.py b/algorithms/file_transfer_protocol/ftp_send_receive.py similarity index 100% rename from File_Transfer_Protocol/ftp_send_receive.py rename to algorithms/file_transfer_protocol/ftp_send_receive.py diff --git a/Graphs/a_star.py b/algorithms/graphs/a_star.py similarity index 100% rename from Graphs/a_star.py rename to algorithms/graphs/a_star.py diff --git a/Graphs/basic-graphs.py b/algorithms/graphs/basic-graphs.py similarity index 98% rename from Graphs/basic-graphs.py rename to algorithms/graphs/basic-graphs.py index 6e433b5bd725..d4ab4459d03a 100644 --- a/Graphs/basic-graphs.py +++ b/algorithms/graphs/basic-graphs.py @@ -20,7 +20,7 @@ """ -------------------------------------------------------------------------------- - Accepting edges of Unweighted Directed Graphs + Accepting edges of Unweighted Directed graphs -------------------------------------------------------------------------------- """ for _ in xrange(m): @@ -29,7 +29,7 @@ """ -------------------------------------------------------------------------------- - Accepting edges of Unweighted Undirected Graphs + Accepting edges of Unweighted Undirected graphs -------------------------------------------------------------------------------- """ for _ in xrange(m): @@ -39,7 +39,7 @@ """ -------------------------------------------------------------------------------- - Accepting edges of Weighted Undirected Graphs + Accepting edges of Weighted Undirected graphs -------------------------------------------------------------------------------- """ for _ in xrange(m): diff --git a/Graphs/minimum_spanning_tree_kruskal.py b/algorithms/graphs/minimum_spanning_tree_kruskal.py similarity index 100% rename from Graphs/minimum_spanning_tree_kruskal.py rename to algorithms/graphs/minimum_spanning_tree_kruskal.py diff --git a/Graphs/scc_kosaraju.py b/algorithms/graphs/scc_kosaraju.py similarity index 100% rename from Graphs/scc_kosaraju.py rename to algorithms/graphs/scc_kosaraju.py diff --git a/Graphs/tarjans_scc.py b/algorithms/graphs/tarjans_scc.py similarity index 100% rename from Graphs/tarjans_scc.py rename to algorithms/graphs/tarjans_scc.py diff --git a/hashes/chaos_machine.py b/algorithms/hashes/chaos_machine.py similarity index 100% rename from hashes/chaos_machine.py rename to algorithms/hashes/chaos_machine.py diff --git a/hashes/md5.py b/algorithms/hashes/md5.py similarity index 100% rename from hashes/md5.py rename to algorithms/hashes/md5.py diff --git a/algorithms/linear_algebra_python/src/__init__.py b/algorithms/linear_algebra_python/src/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Maths/ModularExponential.py b/algorithms/maths/ModularExponential.py similarity index 100% rename from Maths/ModularExponential.py rename to algorithms/maths/ModularExponential.py diff --git a/Maths/SimpsonRule.py b/algorithms/maths/SimpsonRule.py similarity index 100% rename from Maths/SimpsonRule.py rename to algorithms/maths/SimpsonRule.py diff --git a/Maths/TrapezoidalRule.py b/algorithms/maths/TrapezoidalRule.py similarity index 100% rename from Maths/TrapezoidalRule.py rename to algorithms/maths/TrapezoidalRule.py diff --git a/networking_flow/Ford_Fulkerson.py b/algorithms/networking_flow/Ford_Fulkerson.py similarity index 100% rename from networking_flow/Ford_Fulkerson.py rename to algorithms/networking_flow/Ford_Fulkerson.py diff --git a/networking_flow/Minimum_cut.py b/algorithms/networking_flow/Minimum_cut.py similarity index 100% rename from networking_flow/Minimum_cut.py rename to algorithms/networking_flow/Minimum_cut.py diff --git a/searches/binary_search.py b/algorithms/searches/binary_search.py similarity index 100% rename from searches/binary_search.py rename to algorithms/searches/binary_search.py diff --git a/searches/interpolation_search.py b/algorithms/searches/interpolation_search.py similarity index 100% rename from searches/interpolation_search.py rename to algorithms/searches/interpolation_search.py diff --git a/searches/jump_search.py b/algorithms/searches/jump_search.py similarity index 100% rename from searches/jump_search.py rename to algorithms/searches/jump_search.py diff --git a/searches/linear_search.py b/algorithms/searches/linear_search.py similarity index 100% rename from searches/linear_search.py rename to algorithms/searches/linear_search.py diff --git a/searches/quick_select.py b/algorithms/searches/quick_select.py similarity index 100% rename from searches/quick_select.py rename to algorithms/searches/quick_select.py diff --git a/searches/ternary_search.py b/algorithms/searches/ternary_search.py similarity index 100% rename from searches/ternary_search.py rename to algorithms/searches/ternary_search.py diff --git a/sorts/bogosort.py b/algorithms/sorts/bogosort.py similarity index 100% rename from sorts/bogosort.py rename to algorithms/sorts/bogosort.py diff --git a/sorts/bubble_sort.py b/algorithms/sorts/bubble_sort.py similarity index 100% rename from sorts/bubble_sort.py rename to algorithms/sorts/bubble_sort.py diff --git a/sorts/bucket_sort.py b/algorithms/sorts/bucket_sort.py similarity index 100% rename from sorts/bucket_sort.py rename to algorithms/sorts/bucket_sort.py diff --git a/sorts/cocktail_shaker_sort.py b/algorithms/sorts/cocktail_shaker_sort.py similarity index 100% rename from sorts/cocktail_shaker_sort.py rename to algorithms/sorts/cocktail_shaker_sort.py diff --git a/sorts/counting_sort.py b/algorithms/sorts/counting_sort.py similarity index 100% rename from sorts/counting_sort.py rename to algorithms/sorts/counting_sort.py diff --git a/sorts/countingsort.py b/algorithms/sorts/countingsort.py similarity index 100% rename from sorts/countingsort.py rename to algorithms/sorts/countingsort.py diff --git a/sorts/cyclesort.py b/algorithms/sorts/cyclesort.py similarity index 100% rename from sorts/cyclesort.py rename to algorithms/sorts/cyclesort.py diff --git a/sorts/external-sort.py b/algorithms/sorts/external-sort.py similarity index 100% rename from sorts/external-sort.py rename to algorithms/sorts/external-sort.py diff --git a/sorts/gnome_sort.py b/algorithms/sorts/gnome_sort.py similarity index 100% rename from sorts/gnome_sort.py rename to algorithms/sorts/gnome_sort.py diff --git a/sorts/heap_sort.py b/algorithms/sorts/heap_sort.py similarity index 100% rename from sorts/heap_sort.py rename to algorithms/sorts/heap_sort.py diff --git a/sorts/insertion_sort.py b/algorithms/sorts/insertion_sort.py similarity index 100% rename from sorts/insertion_sort.py rename to algorithms/sorts/insertion_sort.py diff --git a/sorts/merge_sort.py b/algorithms/sorts/merge_sort.py similarity index 100% rename from sorts/merge_sort.py rename to algorithms/sorts/merge_sort.py diff --git a/sorts/normal_distribution_QuickSort_README.md b/algorithms/sorts/normal_distribution_QuickSort_README.md similarity index 100% rename from sorts/normal_distribution_QuickSort_README.md rename to algorithms/sorts/normal_distribution_QuickSort_README.md diff --git a/sorts/pancake_sort.py b/algorithms/sorts/pancake_sort.py similarity index 100% rename from sorts/pancake_sort.py rename to algorithms/sorts/pancake_sort.py diff --git a/sorts/quick_sort.py b/algorithms/sorts/quick_sort.py similarity index 100% rename from sorts/quick_sort.py rename to algorithms/sorts/quick_sort.py diff --git a/sorts/radix_sort.py b/algorithms/sorts/radix_sort.py similarity index 100% rename from sorts/radix_sort.py rename to algorithms/sorts/radix_sort.py diff --git a/sorts/random_normaldistribution_quicksort.py b/algorithms/sorts/random_normaldistribution_quicksort.py similarity index 100% rename from sorts/random_normaldistribution_quicksort.py rename to algorithms/sorts/random_normaldistribution_quicksort.py diff --git a/sorts/selection_sort.py b/algorithms/sorts/selection_sort.py similarity index 100% rename from sorts/selection_sort.py rename to algorithms/sorts/selection_sort.py diff --git a/sorts/shell_sort.py b/algorithms/sorts/shell_sort.py similarity index 100% rename from sorts/shell_sort.py rename to algorithms/sorts/shell_sort.py diff --git a/sorts/sortinggraphs.png b/algorithms/sorts/sortinggraphs.png similarity index 100% rename from sorts/sortinggraphs.png rename to algorithms/sorts/sortinggraphs.png diff --git a/sorts/timsort.py b/algorithms/sorts/timsort.py similarity index 100% rename from sorts/timsort.py rename to algorithms/sorts/timsort.py diff --git a/sorts/topological_sort.py b/algorithms/sorts/topological_sort.py similarity index 100% rename from sorts/topological_sort.py rename to algorithms/sorts/topological_sort.py diff --git a/sorts/tree_sort.py b/algorithms/sorts/tree_sort.py similarity index 100% rename from sorts/tree_sort.py rename to algorithms/sorts/tree_sort.py diff --git a/strings/knuth-morris-pratt.py b/algorithms/strings/knuth-morris-pratt.py similarity index 100% rename from strings/knuth-morris-pratt.py rename to algorithms/strings/knuth-morris-pratt.py diff --git a/strings/min-cost-string-conversion.py b/algorithms/strings/min-cost-string-conversion.py similarity index 100% rename from strings/min-cost-string-conversion.py rename to algorithms/strings/min-cost-string-conversion.py diff --git a/strings/rabin-karp.py b/algorithms/strings/rabin-karp.py similarity index 100% rename from strings/rabin-karp.py rename to algorithms/strings/rabin-karp.py diff --git a/traversals/binary_tree_traversals.py b/algorithms/traversals/binary_tree_traversals.py similarity index 100% rename from traversals/binary_tree_traversals.py rename to algorithms/traversals/binary_tree_traversals.py diff --git a/data_structures/Trie/Trie.py b/data_structures/Trie/Trie.py index b6234c6704c6..6a9d4e1477c4 100644 --- a/data_structures/Trie/Trie.py +++ b/data_structures/Trie/Trie.py @@ -1,6 +1,6 @@ """ A Trie/Prefix Tree is a kind of search tree used to provide quick lookup -of words/patterns in a set of words. A basic Trie however has O(n^2) space complexity +of words/patterns in a set of words. A algorithms Trie however has O(n^2) space complexity making it impractical in practice. It however provides O(max(search_string, length of longest word)) lookup time making it an optimal approach when space is not an issue. diff --git a/linear-algebra-python/README.md b/linear-algebra-python/README.md deleted file mode 100644 index ebfcdab7b179..000000000000 --- a/linear-algebra-python/README.md +++ /dev/null @@ -1,74 +0,0 @@ -# Linear algebra library for Python - -This module contains some useful classes and functions for dealing with linear algebra in python 2. - ---- - -## Overview - -- class Vector - - This class represents a vector of arbitray size and operations on it. - - **Overview about the methods:** - - - constructor(components : list) : init the vector - - set(components : list) : changes the vector components. - - __str__() : toString method - - component(i : int): gets the i-th component (start by 0) - - size() : gets the size of the vector (number of components) - - euclidLength() : returns the eulidean length of the vector. - - operator + : vector addition - - operator - : vector subtraction - - operator * : scalar multiplication and dot product - - copy() : copies this vector and returns it. - - changeComponent(pos,value) : changes the specified component. - -- function zeroVector(dimension) - - returns a zero vector of 'dimension' -- function unitBasisVector(dimension,pos) - - returns a unit basis vector with a One at index 'pos' (indexing at 0) -- function axpy(scalar,vector1,vector2) - - computes the axpy operation -- function randomVector(N,a,b) - - returns a random vector of size N, with random integer components between 'a' and 'b'. -- class Matrix - - This class represents a matrix of arbitrary size and operations on it. - - **Overview about the methods:** - - - __str__() : returns a string representation - - operator * : implements the matrix vector multiplication - implements the matrix-scalar multiplication. - - changeComponent(x,y,value) : changes the specified component. - - component(x,y) : returns the specified component. - - width() : returns the width of the matrix - - height() : returns the height of the matrix - - operator + : implements the matrix-addition. - - operator - _ implements the matrix-subtraction -- function squareZeroMatrix(N) - - returns a square zero-matrix of dimension NxN -- function randomMatrix(W,H,a,b) - - returns a random matrix WxH with integer components between 'a' and 'b' ---- - -## Documentation - -The module is well documented. You can use the python in-built ```help(...)``` function. -For instance: ```help(Vector)``` gives you all information about the Vector-class. -Or ```help(unitBasisVector)``` gives you all information you needed about the -global function ```unitBasisVector(...)```. If you need informations about a certain -method you type ```help(CLASSNAME.METHODNAME)```. - ---- - -## Usage - -You will find the module in the **src** directory its called ```lib.py```. You need to -import this module in your project. Alternative you can also use the file ```lib.pyc``` in python-bytecode. - ---- - -## Tests - -In the **src** directory you also find the test-suite, its called ```tests.py```. -The test-suite uses the built-in python-test-framework **unittest**. diff --git a/linear-algebra-python/src/lib.py b/linear-algebra-python/src/lib.py deleted file mode 100644 index 66f27ff8946e..000000000000 --- a/linear-algebra-python/src/lib.py +++ /dev/null @@ -1,364 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Mon Feb 26 14:29:11 2018 - -@author: Christian Bender -@license: MIT-license - -This module contains some useful classes and functions for dealing -with linear algebra in python. - -Overview: - -- class Vector -- function zeroVector(dimension) -- function unitBasisVector(dimension,pos) -- function axpy(scalar,vector1,vector2) -- function randomVector(N,a,b) -- class Matrix -- function squareZeroMatrix(N) -- function randomMatrix(W,H,a,b) -""" - - -import math -import random - - -class Vector(object): - """ - This class represents a vector of arbitray size. - You need to give the vector components. - - Overview about the methods: - - constructor(components : list) : init the vector - set(components : list) : changes the vector components. - __str__() : toString method - component(i : int): gets the i-th component (start by 0) - size() : gets the size of the vector (number of components) - euclidLength() : returns the eulidean length of the vector. - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product - copy() : copies this vector and returns it. - changeComponent(pos,value) : changes the specified component. - TODO: compare-operator - """ - def __init__(self,components): - """ - input: components or nothing - simple constructor for init the vector - """ - self.__components = components - def set(self,components): - """ - input: new components - changes the components of the vector. - replace the components with newer one. - """ - if len(components) > 0: - self.__components = components - else: - raise Exception("please give any vector") - def __str__(self): - """ - returns a string representation of the vector - """ - ans = "(" - length = len(self.__components) - for i in range(length): - if i != length-1: - ans += str(self.__components[i]) + "," - else: - ans += str(self.__components[i]) + ")" - if len(ans) == 1: - ans += ")" - return ans - def component(self,i): - """ - input: index (start at 0) - output: the i-th component of the vector. - """ - if i < len(self.__components) and i >= 0: - return self.__components[i] - else: - raise Exception("index out of range") - def size(self): - """ - returns the size of the vector - """ - return len(self.__components) - def eulidLength(self): - """ - returns the eulidean length of the vector - """ - summe = 0 - for c in self.__components: - summe += c**2 - return math.sqrt(summe) - def __add__(self,other): - """ - input: other vector - assumes: other vector has the same size - returns a new vector that represents the sum. - """ - size = self.size() - result = [] - if size == other.size(): - for i in range(size): - result.append(self.__components[i] + other.component(i)) - else: - raise Exception("must have the same size") - return Vector(result) - def __sub__(self,other): - """ - input: other vector - assumes: other vector has the same size - returns a new vector that represents the differenz. - """ - size = self.size() - result = [] - if size == other.size(): - for i in range(size): - result.append(self.__components[i] - other.component(i)) - else: # error case - raise Exception("must have the same size") - return Vector(result) - def __mul__(self,other): - """ - mul implements the scalar multiplication - and the dot-product - """ - ans = [] - if isinstance(other,float) or isinstance(other,int): - for c in self.__components: - ans.append(c*other) - elif (isinstance(other,Vector) and (self.size() == other.size())): - size = self.size() - summe = 0 - for i in range(size): - summe += self.__components[i] * other.component(i) - return summe - else: # error case - raise Exception("invalide operand!") - return Vector(ans) - def copy(self): - """ - copies this vector and returns it. - """ - components = [x for x in self.__components] - return Vector(components) - def changeComponent(self,pos,value): - """ - input: an index (pos) and a value - changes the specified component (pos) with the - 'value' - """ - #precondition - assert (pos >= 0 and pos < len(self.__components)) - self.__components[pos] = value - -def zeroVector(dimension): - """ - returns a zero-vector of size 'dimension' - """ - #precondition - assert(isinstance(dimension,int)) - ans = [] - for i in range(dimension): - ans.append(0) - return Vector(ans) - - -def unitBasisVector(dimension,pos): - """ - returns a unit basis vector with a One - at index 'pos' (indexing at 0) - """ - #precondition - assert(isinstance(dimension,int) and (isinstance(pos,int))) - ans = [] - for i in range(dimension): - if i != pos: - ans.append(0) - else: - ans.append(1) - return Vector(ans) - - -def axpy(scalar,x,y): - """ - input: a 'scalar' and two vectors 'x' and 'y' - output: a vector - computes the axpy operation - """ - # precondition - assert(isinstance(x,Vector) and (isinstance(y,Vector)) \ - and (isinstance(scalar,int) or isinstance(scalar,float))) - return (x*scalar + y) - - -def randomVector(N,a,b): - """ - input: size (N) of the vector. - random range (a,b) - output: returns a random vector of size N, with - random integer components between 'a' and 'b'. - """ - ans = zeroVector(N) - random.seed(None) - for i in range(N): - ans.changeComponent(i,random.randint(a,b)) - return ans - - -class Matrix(object): - """ - class: Matrix - This class represents a arbitrary matrix. - - Overview about the methods: - - __str__() : returns a string representation - operator * : implements the matrix vector multiplication - implements the matrix-scalar multiplication. - changeComponent(x,y,value) : changes the specified component. - component(x,y) : returns the specified component. - width() : returns the width of the matrix - height() : returns the height of the matrix - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction - """ - def __init__(self,matrix,w,h): - """ - simple constructor for initialzes - the matrix with components. - """ - self.__matrix = matrix - self.__width = w - self.__height = h - def __str__(self): - """ - returns a string representation of this - matrix. - """ - ans = "" - for i in range(self.__height): - ans += "|" - for j in range(self.__width): - if j < self.__width -1: - ans += str(self.__matrix[i][j]) + "," - else: - ans += str(self.__matrix[i][j]) + "|\n" - return ans - def changeComponent(self,x,y, value): - """ - changes the x-y component of this matrix - """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: - self.__matrix[x][y] = value - else: - raise Exception ("changeComponent: indices out of bounds") - def component(self,x,y): - """ - returns the specified (x,y) component - """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: - return self.__matrix[x][y] - else: - raise Exception ("changeComponent: indices out of bounds") - def width(self): - """ - getter for the width - """ - return self.__width - def height(self): - """ - getter for the height - """ - return self.__height - def __mul__(self,other): - """ - implements the matrix-vector multiplication. - implements the matrix-scalar multiplication - """ - if isinstance(other, Vector): # vector-matrix - if (other.size() == self.__width): - ans = zeroVector(self.__height) - for i in range(self.__height): - summe = 0 - for j in range(self.__width): - summe += other.component(j) * self.__matrix[i][j] - ans.changeComponent(i,summe) - summe = 0 - return ans - else: - raise Exception("vector must have the same size as the " - + "number of columns of the matrix!") - elif isinstance(other,int) or isinstance(other,float): # matrix-scalar - matrix = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] * other) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - def __add__(self,other): - """ - implements the matrix-addition. - """ - if (self.__width == other.width() and self.__height == other.height()): - matrix = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] + other.component(i,j)) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - else: - raise Exception("matrix must have the same dimension!") - def __sub__(self,other): - """ - implements the matrix-subtraction. - """ - if (self.__width == other.width() and self.__height == other.height()): - matrix = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] - other.component(i,j)) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - else: - raise Exception("matrix must have the same dimension!") - - -def squareZeroMatrix(N): - """ - returns a square zero-matrix of dimension NxN - """ - ans = [] - for i in range(N): - row = [] - for j in range(N): - row.append(0) - ans.append(row) - return Matrix(ans,N,N) - - -def randomMatrix(W,H,a,b): - """ - returns a random matrix WxH with integer components - between 'a' and 'b' - """ - matrix = [] - random.seed(None) - for i in range(H): - row = [] - for j in range(W): - row.append(random.randint(a,b)) - matrix.append(row) - return Matrix(matrix,W,H) - - \ No newline at end of file diff --git a/linear-algebra-python/src/tests.py b/linear-algebra-python/src/tests.py deleted file mode 100644 index b84612b4ced4..000000000000 --- a/linear-algebra-python/src/tests.py +++ /dev/null @@ -1,133 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Mon Feb 26 15:40:07 2018 - -@author: Christian Bender -@license: MIT-license - -This file contains the test-suite for the linear algebra library. -""" - -import unittest -from lib import * - -class Test(unittest.TestCase): - def test_component(self): - """ - test for method component - """ - x = Vector([1,2,3]) - self.assertEqual(x.component(0),1) - self.assertEqual(x.component(2),3) - try: - y = Vector() - self.assertTrue(False) - except: - self.assertTrue(True) - def test_str(self): - """ - test for toString() method - """ - x = Vector([0,0,0,0,0,1]) - self.assertEqual(x.__str__(),"(0,0,0,0,0,1)") - def test_size(self): - """ - test for size()-method - """ - x = Vector([1,2,3,4]) - self.assertEqual(x.size(),4) - def test_euclidLength(self): - """ - test for the eulidean length - """ - x = Vector([1,2]) - self.assertAlmostEqual(x.eulidLength(),2.236,3) - def test_add(self): - """ - test for + operator - """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x+y).component(0),2) - self.assertEqual((x+y).component(1),3) - self.assertEqual((x+y).component(2),4) - def test_sub(self): - """ - test for - operator - """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x-y).component(0),0) - self.assertEqual((x-y).component(1),1) - self.assertEqual((x-y).component(2),2) - def test_mul(self): - """ - test for * operator - """ - x = Vector([1,2,3]) - a = Vector([2,-1,4]) # for test of dot-product - b = Vector([1,-2,-1]) - self.assertEqual((x*3.0).__str__(),"(3.0,6.0,9.0)") - self.assertEqual((a*b),0) - def test_zeroVector(self): - """ - test for the global function zeroVector(...) - """ - self.assertTrue(zeroVector(10).__str__().count("0") == 10) - def test_unitBasisVector(self): - """ - test for the global function unitBasisVector(...) - """ - self.assertEqual(unitBasisVector(3,1).__str__(),"(0,1,0)") - def test_axpy(self): - """ - test for the global function axpy(...) (operation) - """ - x = Vector([1,2,3]) - y = Vector([1,0,1]) - self.assertEqual(axpy(2,x,y).__str__(),"(3,4,7)") - def test_copy(self): - """ - test for the copy()-method - """ - x = Vector([1,0,0,0,0,0]) - y = x.copy() - self.assertEqual(x.__str__(),y.__str__()) - def test_changeComponent(self): - """ - test for the changeComponent(...)-method - """ - x = Vector([1,0,0]) - x.changeComponent(0,0) - x.changeComponent(1,1) - self.assertEqual(x.__str__(),"(0,1,0)") - def test_str_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n",A.__str__()) - def test__mul__matrix(self): - A = Matrix([[1,2,3],[4,5,6],[7,8,9]],3,3) - x = Vector([1,2,3]) - self.assertEqual("(14,32,50)",(A*x).__str__()) - self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n",(A*2).__str__()) - def test_changeComponent_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - A.changeComponent(0,2,5) - self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n",A.__str__()) - def test_component_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual(7,A.component(2,1),0.01) - def test__add__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n",(A+B).__str__()) - def test__sub__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n",(A-B).__str__()) - def test_squareZeroMatrix(self): - self.assertEqual('|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|' - +'\n|0,0,0,0,0|\n',squareZeroMatrix(5).__str__()) - - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/dynamic_programming/FloydWarshall.py b/other/dynamic_programming/FloydWarshall.py similarity index 96% rename from dynamic_programming/FloydWarshall.py rename to other/dynamic_programming/FloydWarshall.py index 038499ca03b6..0f4e16cd91d7 100644 --- a/dynamic_programming/FloydWarshall.py +++ b/other/dynamic_programming/FloydWarshall.py @@ -1,37 +1,37 @@ -import math - -class Graph: - - def __init__(self, N = 0): # a graph with Node 0,1,...,N-1 - self.N = N - self.W = [[math.inf for j in range(0,N)] for i in range(0,N)] # adjacency matrix for weight - self.dp = [[math.inf for j in range(0,N)] for i in range(0,N)] # dp[i][j] stores minimum distance from i to j - - def addEdge(self, u, v, w): - self.dp[u][v] = w - - def floyd_warshall(self): - for k in range(0,self.N): - for i in range(0,self.N): - for j in range(0,self.N): - self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) - - def showMin(self, u, v): - return self.dp[u][v] - -if __name__ == '__main__': - graph = Graph(5) - graph.addEdge(0,2,9) - graph.addEdge(0,4,10) - graph.addEdge(1,3,5) - graph.addEdge(2,3,7) - graph.addEdge(3,0,10) - graph.addEdge(3,1,2) - graph.addEdge(3,2,1) - graph.addEdge(3,4,6) - graph.addEdge(4,1,3) - graph.addEdge(4,2,4) - graph.addEdge(4,3,9) - graph.floyd_warshall() - graph.showMin(1,4) - graph.showMin(0,3) +import math + +class Graph: + + def __init__(self, N = 0): # a graph with Node 0,1,...,N-1 + self.N = N + self.W = [[math.inf for j in range(0,N)] for i in range(0,N)] # adjacency matrix for weight + self.dp = [[math.inf for j in range(0,N)] for i in range(0,N)] # dp[i][j] stores minimum distance from i to j + + def addEdge(self, u, v, w): + self.dp[u][v] = w + + def floyd_warshall(self): + for k in range(0,self.N): + for i in range(0,self.N): + for j in range(0,self.N): + self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) + + def showMin(self, u, v): + return self.dp[u][v] + +if __name__ == '__main__': + graph = Graph(5) + graph.addEdge(0,2,9) + graph.addEdge(0,4,10) + graph.addEdge(1,3,5) + graph.addEdge(2,3,7) + graph.addEdge(3,0,10) + graph.addEdge(3,1,2) + graph.addEdge(3,2,1) + graph.addEdge(3,4,6) + graph.addEdge(4,1,3) + graph.addEdge(4,2,4) + graph.addEdge(4,3,9) + graph.floyd_warshall() + graph.showMin(1,4) + graph.showMin(0,3) diff --git a/dynamic_programming/abbreviation.py b/other/dynamic_programming/abbreviation.py similarity index 100% rename from dynamic_programming/abbreviation.py rename to other/dynamic_programming/abbreviation.py diff --git a/dynamic_programming/coin_change.py b/other/dynamic_programming/coin_change.py similarity index 100% rename from dynamic_programming/coin_change.py rename to other/dynamic_programming/coin_change.py diff --git a/dynamic_programming/edit_distance.py b/other/dynamic_programming/edit_distance.py similarity index 100% rename from dynamic_programming/edit_distance.py rename to other/dynamic_programming/edit_distance.py diff --git a/dynamic_programming/fastfibonacci.py b/other/dynamic_programming/fastfibonacci.py similarity index 100% rename from dynamic_programming/fastfibonacci.py rename to other/dynamic_programming/fastfibonacci.py diff --git a/dynamic_programming/fibonacci.py b/other/dynamic_programming/fibonacci.py similarity index 100% rename from dynamic_programming/fibonacci.py rename to other/dynamic_programming/fibonacci.py diff --git a/dynamic_programming/integer_partition.py b/other/dynamic_programming/integer_partition.py similarity index 100% rename from dynamic_programming/integer_partition.py rename to other/dynamic_programming/integer_partition.py diff --git a/dynamic_programming/k_means_clustering_tensorflow.py b/other/dynamic_programming/k_means_clustering_tensorflow.py similarity index 100% rename from dynamic_programming/k_means_clustering_tensorflow.py rename to other/dynamic_programming/k_means_clustering_tensorflow.py diff --git a/dynamic_programming/knapsack.py b/other/dynamic_programming/knapsack.py similarity index 100% rename from dynamic_programming/knapsack.py rename to other/dynamic_programming/knapsack.py diff --git a/dynamic_programming/longest_common_subsequence.py b/other/dynamic_programming/longest_common_subsequence.py similarity index 100% rename from dynamic_programming/longest_common_subsequence.py rename to other/dynamic_programming/longest_common_subsequence.py diff --git a/dynamic_programming/longest_increasing_subsequence.py b/other/dynamic_programming/longest_increasing_subsequence.py similarity index 100% rename from dynamic_programming/longest_increasing_subsequence.py rename to other/dynamic_programming/longest_increasing_subsequence.py diff --git a/dynamic_programming/longest_increasing_subsequence_O(nlogn).py b/other/dynamic_programming/longest_increasing_subsequence_O(nlogn).py similarity index 100% rename from dynamic_programming/longest_increasing_subsequence_O(nlogn).py rename to other/dynamic_programming/longest_increasing_subsequence_O(nlogn).py diff --git a/dynamic_programming/longest_sub_array.py b/other/dynamic_programming/longest_sub_array.py similarity index 100% rename from dynamic_programming/longest_sub_array.py rename to other/dynamic_programming/longest_sub_array.py diff --git a/dynamic_programming/matrix_chain_order.py b/other/dynamic_programming/matrix_chain_order.py similarity index 100% rename from dynamic_programming/matrix_chain_order.py rename to other/dynamic_programming/matrix_chain_order.py diff --git a/dynamic_programming/max_sub_array.py b/other/dynamic_programming/max_sub_array.py similarity index 100% rename from dynamic_programming/max_sub_array.py rename to other/dynamic_programming/max_sub_array.py diff --git a/dynamic_programming/minimum_partition.py b/other/dynamic_programming/minimum_partition.py similarity index 100% rename from dynamic_programming/minimum_partition.py rename to other/dynamic_programming/minimum_partition.py diff --git a/machine_learning/decision_tree.py b/other/machine_learning/decision_tree.py similarity index 98% rename from machine_learning/decision_tree.py rename to other/machine_learning/decision_tree.py index 71849904ccf2..62de95961a20 100644 --- a/machine_learning/decision_tree.py +++ b/other/machine_learning/decision_tree.py @@ -1,5 +1,5 @@ """ -Implementation of a basic regression decision tree. +Implementation of a algorithms regression decision tree. Input data set: The input data set must be 1-dimensional with continuous labels. Output: The decision tree maps a real number input to a real number output. """ diff --git a/machine_learning/gradient_descent.py b/other/machine_learning/gradient_descent.py similarity index 100% rename from machine_learning/gradient_descent.py rename to other/machine_learning/gradient_descent.py diff --git a/machine_learning/k_means_clust.py b/other/machine_learning/k_means_clust.py similarity index 100% rename from machine_learning/k_means_clust.py rename to other/machine_learning/k_means_clust.py diff --git a/machine_learning/linear_regression.py b/other/machine_learning/linear_regression.py similarity index 97% rename from machine_learning/linear_regression.py rename to other/machine_learning/linear_regression.py index eb1f019c502c..8564d59ff2d1 100644 --- a/machine_learning/linear_regression.py +++ b/other/machine_learning/linear_regression.py @@ -1,5 +1,5 @@ """ -Linear regression is the most basic type of regression commonly used for +Linear regression is the most algorithms type of regression commonly used for predictive analysis. The idea is preety simple, we have a dataset and we have a feature's associated with it. The Features should be choose very cautiously as they determine, how much our model will be able to make future predictions. diff --git a/Neural_Network/perceptron.py b/other/machine_learning/perceptron.py similarity index 100% rename from Neural_Network/perceptron.py rename to other/machine_learning/perceptron.py diff --git a/machine_learning/scoring_functions.py b/other/machine_learning/scoring_functions.py similarity index 100% rename from machine_learning/scoring_functions.py rename to other/machine_learning/scoring_functions.py diff --git a/Neural_Network/bpnn.py b/other/neural_network/bpnn.py similarity index 100% rename from Neural_Network/bpnn.py rename to other/neural_network/bpnn.py diff --git a/Neural_Network/convolution_neural_network.py b/other/neural_network/convolution_neural_network.py similarity index 100% rename from Neural_Network/convolution_neural_network.py rename to other/neural_network/convolution_neural_network.py diff --git a/machine_learning/perceptron.py b/other/neural_network/perceptron.py similarity index 100% rename from machine_learning/perceptron.py rename to other/neural_network/perceptron.py diff --git a/other/project_euler/Problem 01/__init__.py b/other/project_euler/Problem 01/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 01/sol1.py b/other/project_euler/Problem 01/sol1.py similarity index 100% rename from Project Euler/Problem 01/sol1.py rename to other/project_euler/Problem 01/sol1.py diff --git a/Project Euler/Problem 01/sol2.py b/other/project_euler/Problem 01/sol2.py similarity index 100% rename from Project Euler/Problem 01/sol2.py rename to other/project_euler/Problem 01/sol2.py diff --git a/Project Euler/Problem 01/sol3.py b/other/project_euler/Problem 01/sol3.py similarity index 100% rename from Project Euler/Problem 01/sol3.py rename to other/project_euler/Problem 01/sol3.py diff --git a/Project Euler/Problem 01/sol4.py b/other/project_euler/Problem 01/sol4.py similarity index 94% rename from Project Euler/Problem 01/sol4.py rename to other/project_euler/Problem 01/sol4.py index 7941f5fcd3fe..36a91e7043aa 100644 --- a/Project Euler/Problem 01/sol4.py +++ b/other/project_euler/Problem 01/sol4.py @@ -1,30 +1,30 @@ -def mulitples(limit): - xmulti = [] - zmulti = [] - z = 3 - x = 5 - temp = 1 - while True: - result = z * temp - if (result < limit): - zmulti.append(result) - temp += 1 - else: - temp = 1 - break - while True: - result = x * temp - if (result < limit): - xmulti.append(result) - temp += 1 - else: - break - collection = list(set(xmulti+zmulti)) - return (sum(collection)) - - - - - - -print (mulitples(1000)) +def mulitples(limit): + xmulti = [] + zmulti = [] + z = 3 + x = 5 + temp = 1 + while True: + result = z * temp + if (result < limit): + zmulti.append(result) + temp += 1 + else: + temp = 1 + break + while True: + result = x * temp + if (result < limit): + xmulti.append(result) + temp += 1 + else: + break + collection = list(set(xmulti+zmulti)) + return (sum(collection)) + + + + + + +print (mulitples(1000)) diff --git a/other/project_euler/Problem 02/__init__.py b/other/project_euler/Problem 02/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 02/sol1.py b/other/project_euler/Problem 02/sol1.py similarity index 100% rename from Project Euler/Problem 02/sol1.py rename to other/project_euler/Problem 02/sol1.py diff --git a/Project Euler/Problem 02/sol2.py b/other/project_euler/Problem 02/sol2.py similarity index 94% rename from Project Euler/Problem 02/sol2.py rename to other/project_euler/Problem 02/sol2.py index f0502a389707..2394148624a3 100644 --- a/Project Euler/Problem 02/sol2.py +++ b/other/project_euler/Problem 02/sol2.py @@ -1,13 +1,13 @@ -def fib(n): - ls = [] - a,b = 0,1 - n += 1 - for i in range(n): - if (b % 2 == 0): - ls.append(b) - else: - pass - a,b = b, a+b - print (sum(ls)) - return None -fib(10) +def fib(n): + ls = [] + a,b = 0,1 + n += 1 + for i in range(n): + if (b % 2 == 0): + ls.append(b) + else: + pass + a,b = b, a+b + print (sum(ls)) + return None +fib(10) diff --git a/Project Euler/Problem 02/sol3.py b/other/project_euler/Problem 02/sol3.py similarity index 100% rename from Project Euler/Problem 02/sol3.py rename to other/project_euler/Problem 02/sol3.py diff --git a/other/project_euler/Problem 03/__init__.py b/other/project_euler/Problem 03/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 03/sol1.py b/other/project_euler/Problem 03/sol1.py similarity index 100% rename from Project Euler/Problem 03/sol1.py rename to other/project_euler/Problem 03/sol1.py diff --git a/Project Euler/Problem 03/sol2.py b/other/project_euler/Problem 03/sol2.py similarity index 100% rename from Project Euler/Problem 03/sol2.py rename to other/project_euler/Problem 03/sol2.py diff --git a/other/project_euler/Problem 04/__init__.py b/other/project_euler/Problem 04/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 04/sol1.py b/other/project_euler/Problem 04/sol1.py similarity index 100% rename from Project Euler/Problem 04/sol1.py rename to other/project_euler/Problem 04/sol1.py diff --git a/Project Euler/Problem 04/sol2.py b/other/project_euler/Problem 04/sol2.py similarity index 100% rename from Project Euler/Problem 04/sol2.py rename to other/project_euler/Problem 04/sol2.py diff --git a/other/project_euler/Problem 05/__init__.py b/other/project_euler/Problem 05/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 05/sol1.py b/other/project_euler/Problem 05/sol1.py similarity index 100% rename from Project Euler/Problem 05/sol1.py rename to other/project_euler/Problem 05/sol1.py diff --git a/Project Euler/Problem 05/sol2.py b/other/project_euler/Problem 05/sol2.py similarity index 100% rename from Project Euler/Problem 05/sol2.py rename to other/project_euler/Problem 05/sol2.py diff --git a/other/project_euler/Problem 06/__init__.py b/other/project_euler/Problem 06/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 06/sol1.py b/other/project_euler/Problem 06/sol1.py similarity index 100% rename from Project Euler/Problem 06/sol1.py rename to other/project_euler/Problem 06/sol1.py diff --git a/Project Euler/Problem 06/sol2.py b/other/project_euler/Problem 06/sol2.py similarity index 100% rename from Project Euler/Problem 06/sol2.py rename to other/project_euler/Problem 06/sol2.py diff --git a/other/project_euler/Problem 07/__init__.py b/other/project_euler/Problem 07/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 07/sol1.py b/other/project_euler/Problem 07/sol1.py similarity index 100% rename from Project Euler/Problem 07/sol1.py rename to other/project_euler/Problem 07/sol1.py diff --git a/Project Euler/Problem 07/sol2.py b/other/project_euler/Problem 07/sol2.py similarity index 100% rename from Project Euler/Problem 07/sol2.py rename to other/project_euler/Problem 07/sol2.py diff --git a/other/project_euler/Problem 08/__init__.py b/other/project_euler/Problem 08/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 08/sol1.py b/other/project_euler/Problem 08/sol1.py similarity index 100% rename from Project Euler/Problem 08/sol1.py rename to other/project_euler/Problem 08/sol1.py diff --git a/other/project_euler/Problem 10/__init__.py b/other/project_euler/Problem 10/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 10/sol1.py b/other/project_euler/Problem 10/sol1.py similarity index 100% rename from Project Euler/Problem 10/sol1.py rename to other/project_euler/Problem 10/sol1.py diff --git a/other/project_euler/Problem 11/__init__.py b/other/project_euler/Problem 11/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 11/grid.txt b/other/project_euler/Problem 11/grid.txt similarity index 100% rename from Project Euler/Problem 11/grid.txt rename to other/project_euler/Problem 11/grid.txt diff --git a/Project Euler/Problem 11/sol1.py b/other/project_euler/Problem 11/sol1.py similarity index 100% rename from Project Euler/Problem 11/sol1.py rename to other/project_euler/Problem 11/sol1.py diff --git a/Project Euler/Problem 11/sol2.py b/other/project_euler/Problem 11/sol2.py similarity index 100% rename from Project Euler/Problem 11/sol2.py rename to other/project_euler/Problem 11/sol2.py diff --git a/other/project_euler/Problem 12/__init__.py b/other/project_euler/Problem 12/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 12/sol1.py b/other/project_euler/Problem 12/sol1.py similarity index 100% rename from Project Euler/Problem 12/sol1.py rename to other/project_euler/Problem 12/sol1.py diff --git a/other/project_euler/Problem 13/__init__.py b/other/project_euler/Problem 13/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 13/sol1.py b/other/project_euler/Problem 13/sol1.py similarity index 100% rename from Project Euler/Problem 13/sol1.py rename to other/project_euler/Problem 13/sol1.py diff --git a/other/project_euler/Problem 14/__init__.py b/other/project_euler/Problem 14/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 14/sol1.py b/other/project_euler/Problem 14/sol1.py similarity index 100% rename from Project Euler/Problem 14/sol1.py rename to other/project_euler/Problem 14/sol1.py diff --git a/other/project_euler/Problem 15/__init__.py b/other/project_euler/Problem 15/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 15/sol1.py b/other/project_euler/Problem 15/sol1.py similarity index 100% rename from Project Euler/Problem 15/sol1.py rename to other/project_euler/Problem 15/sol1.py diff --git a/other/project_euler/Problem 16/__init__.py b/other/project_euler/Problem 16/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 16/sol1.py b/other/project_euler/Problem 16/sol1.py similarity index 100% rename from Project Euler/Problem 16/sol1.py rename to other/project_euler/Problem 16/sol1.py diff --git a/other/project_euler/Problem 17/__init__.py b/other/project_euler/Problem 17/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 17/sol1.py b/other/project_euler/Problem 17/sol1.py similarity index 100% rename from Project Euler/Problem 17/sol1.py rename to other/project_euler/Problem 17/sol1.py diff --git a/other/project_euler/Problem 19/__init__.py b/other/project_euler/Problem 19/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 19/sol1.py b/other/project_euler/Problem 19/sol1.py similarity index 100% rename from Project Euler/Problem 19/sol1.py rename to other/project_euler/Problem 19/sol1.py diff --git a/other/project_euler/Problem 20/__init__.py b/other/project_euler/Problem 20/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 20/sol1.py b/other/project_euler/Problem 20/sol1.py similarity index 100% rename from Project Euler/Problem 20/sol1.py rename to other/project_euler/Problem 20/sol1.py diff --git a/Project Euler/Problem 20/sol2.py b/other/project_euler/Problem 20/sol2.py similarity index 100% rename from Project Euler/Problem 20/sol2.py rename to other/project_euler/Problem 20/sol2.py diff --git a/other/project_euler/Problem 21/__init__.py b/other/project_euler/Problem 21/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 21/sol1.py b/other/project_euler/Problem 21/sol1.py similarity index 100% rename from Project Euler/Problem 21/sol1.py rename to other/project_euler/Problem 21/sol1.py diff --git a/other/project_euler/Problem 22/__init__.py b/other/project_euler/Problem 22/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 22/p022_names.txt b/other/project_euler/Problem 22/p022_names.txt similarity index 100% rename from Project Euler/Problem 22/p022_names.txt rename to other/project_euler/Problem 22/p022_names.txt diff --git a/Project Euler/Problem 22/sol1.py b/other/project_euler/Problem 22/sol1.py similarity index 100% rename from Project Euler/Problem 22/sol1.py rename to other/project_euler/Problem 22/sol1.py diff --git a/Project Euler/Problem 22/sol2.py b/other/project_euler/Problem 22/sol2.py similarity index 100% rename from Project Euler/Problem 22/sol2.py rename to other/project_euler/Problem 22/sol2.py diff --git a/other/project_euler/Problem 24/__init__.py b/other/project_euler/Problem 24/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 24/sol1.py b/other/project_euler/Problem 24/sol1.py similarity index 100% rename from Project Euler/Problem 24/sol1.py rename to other/project_euler/Problem 24/sol1.py diff --git a/other/project_euler/Problem 25/__init__.py b/other/project_euler/Problem 25/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 25/sol1.py b/other/project_euler/Problem 25/sol1.py similarity index 100% rename from Project Euler/Problem 25/sol1.py rename to other/project_euler/Problem 25/sol1.py diff --git a/other/project_euler/Problem 28/__init__.py b/other/project_euler/Problem 28/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 28/sol1.py b/other/project_euler/Problem 28/sol1.py similarity index 100% rename from Project Euler/Problem 28/sol1.py rename to other/project_euler/Problem 28/sol1.py diff --git a/other/project_euler/Problem 29/__init__.py b/other/project_euler/Problem 29/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 29/solution.py b/other/project_euler/Problem 29/solution.py similarity index 100% rename from Project Euler/Problem 29/solution.py rename to other/project_euler/Problem 29/solution.py diff --git a/other/project_euler/Problem 36/__init__.py b/other/project_euler/Problem 36/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 36/sol1.py b/other/project_euler/Problem 36/sol1.py similarity index 100% rename from Project Euler/Problem 36/sol1.py rename to other/project_euler/Problem 36/sol1.py diff --git a/other/project_euler/Problem 40/__init__.py b/other/project_euler/Problem 40/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 40/sol1.py b/other/project_euler/Problem 40/sol1.py similarity index 100% rename from Project Euler/Problem 40/sol1.py rename to other/project_euler/Problem 40/sol1.py diff --git a/other/project_euler/Problem 48/__init__.py b/other/project_euler/Problem 48/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 48/sol1.py b/other/project_euler/Problem 48/sol1.py similarity index 100% rename from Project Euler/Problem 48/sol1.py rename to other/project_euler/Problem 48/sol1.py diff --git a/other/project_euler/Problem 52/__init__.py b/other/project_euler/Problem 52/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 52/sol1.py b/other/project_euler/Problem 52/sol1.py similarity index 100% rename from Project Euler/Problem 52/sol1.py rename to other/project_euler/Problem 52/sol1.py diff --git a/other/project_euler/Problem 53/__init__.py b/other/project_euler/Problem 53/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 53/sol1.py b/other/project_euler/Problem 53/sol1.py similarity index 100% rename from Project Euler/Problem 53/sol1.py rename to other/project_euler/Problem 53/sol1.py diff --git a/other/project_euler/Problem 76/__init__.py b/other/project_euler/Problem 76/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 76/sol1.py b/other/project_euler/Problem 76/sol1.py similarity index 100% rename from Project Euler/Problem 76/sol1.py rename to other/project_euler/Problem 76/sol1.py diff --git a/other/project_euler/Problem 9/__init__.py b/other/project_euler/Problem 9/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 9/sol1.py b/other/project_euler/Problem 9/sol1.py similarity index 100% rename from Project Euler/Problem 9/sol1.py rename to other/project_euler/Problem 9/sol1.py diff --git a/Project Euler/Problem 9/sol2.py b/other/project_euler/Problem 9/sol2.py similarity index 100% rename from Project Euler/Problem 9/sol2.py rename to other/project_euler/Problem 9/sol2.py diff --git a/Project Euler/README.md b/other/project_euler/README.md similarity index 100% rename from Project Euler/README.md rename to other/project_euler/README.md diff --git a/other/sierpinski_triangle.py b/other/sierpinski_triangle.py index e566f693f63b..7d5fe02cc0dc 100644 --- a/other/sierpinski_triangle.py +++ b/other/sierpinski_triangle.py @@ -5,7 +5,7 @@ What is Sierpinski Triangle? >>The Sierpinski triangle (also with the original orthography Sierpinski), also called the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller -equilateral triangles. Originally constructed as a curve, this is one of the basic examples of self-similar sets, i.e., +equilateral triangles. Originally constructed as a curve, this is one of the algorithms examples of self-similar sets, i.e., it is a mathematically generated pattern that can be reproducible at any magnification or reduction. It is named after the Polish mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries prior to the work of Sierpinski. diff --git a/simple-client-server/README.md b/other/simple_client_server/README.md similarity index 100% rename from simple-client-server/README.md rename to other/simple_client_server/README.md diff --git a/simple-client-server/client.py b/other/simple_client_server/client.py similarity index 100% rename from simple-client-server/client.py rename to other/simple_client_server/client.py diff --git a/simple-client-server/server.py b/other/simple_client_server/server.py similarity index 100% rename from simple-client-server/server.py rename to other/simple_client_server/server.py From 8bd35d6d7bc47a4b1ab26b84965759a49f148338 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Tue, 12 Jun 2018 19:24:23 +0800 Subject: [PATCH 02/19] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- other/detecting_english_programmatically.py | 2 +- other/{Dictionary.txt => dictionary.txt} | 0 other/{FindingPrimes.py => finding_primes.py} | 0 ...ongruentialGenerator.py => linear_congruential_generator.py} | 0 Multi_Hueristic_Astar.py => other/multi_hueristic_astar.py | 0 other/{two-sum.py => two_sum.py} | 0 other/word_patterns.py | 2 +- 7 files changed, 2 insertions(+), 2 deletions(-) rename other/{Dictionary.txt => dictionary.txt} (100%) rename other/{FindingPrimes.py => finding_primes.py} (100%) rename other/{LinearCongruentialGenerator.py => linear_congruential_generator.py} (100%) rename Multi_Hueristic_Astar.py => other/multi_hueristic_astar.py (100%) rename other/{two-sum.py => two_sum.py} (100%) diff --git a/other/detecting_english_programmatically.py b/other/detecting_english_programmatically.py index 305174e31cf7..1b63f8ad0e9b 100644 --- a/other/detecting_english_programmatically.py +++ b/other/detecting_english_programmatically.py @@ -5,7 +5,7 @@ def loadDictionary(): path = os.path.split(os.path.realpath(__file__)) - dictionaryFile = open(path[0] + '/Dictionary.txt') + dictionaryFile = open(path[0] + '/dictionary.txt') englishWords = {} for word in dictionaryFile.read().split('\n'): englishWords[word] = None diff --git a/other/Dictionary.txt b/other/dictionary.txt similarity index 100% rename from other/Dictionary.txt rename to other/dictionary.txt diff --git a/other/FindingPrimes.py b/other/finding_primes.py similarity index 100% rename from other/FindingPrimes.py rename to other/finding_primes.py diff --git a/other/LinearCongruentialGenerator.py b/other/linear_congruential_generator.py similarity index 100% rename from other/LinearCongruentialGenerator.py rename to other/linear_congruential_generator.py diff --git a/Multi_Hueristic_Astar.py b/other/multi_hueristic_astar.py similarity index 100% rename from Multi_Hueristic_Astar.py rename to other/multi_hueristic_astar.py diff --git a/other/two-sum.py b/other/two_sum.py similarity index 100% rename from other/two-sum.py rename to other/two_sum.py diff --git a/other/word_patterns.py b/other/word_patterns.py index c33d520087f7..ef5ea65b395c 100644 --- a/other/word_patterns.py +++ b/other/word_patterns.py @@ -18,7 +18,7 @@ def main(): startTime = time.time() allPatterns = {} - with open('Dictionary.txt') as fo: + with open('dictionary.txt') as fo: wordList = fo.read().split('\n') for word in wordList: From 6abb7e8f08c09b01ec3fd3e03d515d7a345e79be Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Tue, 12 Jun 2018 19:28:20 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 177 ++--------------------------------------- algorithms/__init__.py | 0 post/algorithms.md | 174 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 171 deletions(-) delete mode 100644 algorithms/__init__.py create mode 100644 post/algorithms.md diff --git a/README.md b/README.md index 9b394e001d66..24a25226c922 100644 --- a/README.md +++ b/README.md @@ -1,174 +1,9 @@ -# The Algorithms - Python -### All algorithms implemented in Python (for education) -These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. +## 算法&数据结构 +1、 +2、 -## Sort Algorithms - - -### Bubble -![alt text][bubble-image] - -From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. - -__Properties__ -* Worst case performance O(n^2) -* Best case performance O(n) -* Average case performance O(n^2) - -###### View the algorithm in [action][bubble-toptal] - - - -### Insertion -![alt text][insertion-image] - -From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. - -__Properties__ -* Worst case performance O(n^2) -* Best case performance O(n) -* Average case performance O(n^2) - -###### View the algorithm in [action][insertion-toptal] - - -### Merge -![alt text][merge-image] - -From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. - -__Properties__ -* Worst case performance O(n log n) -* Best case performance O(n) -* Average case performance O(n) - - -###### View the algorithm in [action][merge-toptal] - -### Quick -![alt text][quick-image] - -From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. - -__Properties__ -* Worst case performance O(n^2) -* Best case performance O(n log n) or O(n) with three-way partition -* Average case performance O(n log n) - -###### View the algorithm in [action][quick-toptal] - -### Selection -![alt text][selection-image] - -From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. - -__Properties__ -* Worst case performance O(n^2) -* Best case performance O(n^2) -* Average case performance O(n^2) - -###### View the algorithm in [action][selection-toptal] - -### Shell -![alt text][shell-image] - -From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted. - -__Properties__ -* Worst case performance O(nlog2 2n) -* Best case performance O(n log n) -* Average case performance depends on gap sequence - -###### View the algorithm in [action][shell-toptal] - -### Time-Complexity Graphs - -Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) - -[Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) - ----------------------------------------------------------------------------------- - -## Search Algorithms - -### Linear -![alt text][linear-image] - -From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. - Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. - -__Properties__ -* Worst case performance O(n) -* Best case performance O(1) -* Average case performance O(n) -* Worst case space complexity O(1) iterative - -### Binary -![alt text][binary-image] - -From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. - -__Properties__ -* Worst case performance O(log n) -* Best case performance O(1) -* Average case performance O(log n) -* Worst case space complexity O(1) - ----------------------------------------------------------------------------------------------------------------------- - -## Ciphers - -### Caesar -![alt text][caesar]
-In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.
-It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
-The method is named after **Julius Caesar**, who used it in his private correspondence.
-The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security. -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher) - -### Vigenère -The **Vigenère cipher** is a method of encrypting alphabetic text by using a series of **interwoven Caesar ciphers** based on the letters of a keyword. It is **a form of polyalphabetic substitution**.
-The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del. Sig. Giovan Battista Bellaso; however, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the "Vigenère cipher".
-Though the cipher is easy to understand and implement, for three centuries it resisted all attempts to break it; this earned it the description **le chiffre indéchiffrable**(French for 'the indecipherable cipher'). -Many people have tried to implement encryption schemes that are essentially Vigenère ciphers. Friedrich Kasiski was the first to publish a general method of deciphering a Vigenère cipher in 1863. -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) - -### Transposition -In cryptography, a **transposition cipher** is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).
-Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) - -[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort -[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort -[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" - -[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort -[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort -[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort" - -[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort -[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort -[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" - -[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort -[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort -[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort" - -[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort -[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort -[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" - -[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort -[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort -[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" - -[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search -[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif - -[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm -[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png - - -[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg +## 参考文献 +1、[o(1), o(n), o(logn), o(nlogn)](https://blog.csdn.net/Mars93/article/details/75194138) +2、 diff --git a/algorithms/__init__.py b/algorithms/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/post/algorithms.md b/post/algorithms.md new file mode 100644 index 000000000000..9b394e001d66 --- /dev/null +++ b/post/algorithms.md @@ -0,0 +1,174 @@ +# The Algorithms - Python + +### All algorithms implemented in Python (for education) + +These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. + +## Sort Algorithms + + +### Bubble +![alt text][bubble-image] + +From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. + +__Properties__ +* Worst case performance O(n^2) +* Best case performance O(n) +* Average case performance O(n^2) + +###### View the algorithm in [action][bubble-toptal] + + + +### Insertion +![alt text][insertion-image] + +From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. + +__Properties__ +* Worst case performance O(n^2) +* Best case performance O(n) +* Average case performance O(n^2) + +###### View the algorithm in [action][insertion-toptal] + + +### Merge +![alt text][merge-image] + +From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. + +__Properties__ +* Worst case performance O(n log n) +* Best case performance O(n) +* Average case performance O(n) + + +###### View the algorithm in [action][merge-toptal] + +### Quick +![alt text][quick-image] + +From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. + +__Properties__ +* Worst case performance O(n^2) +* Best case performance O(n log n) or O(n) with three-way partition +* Average case performance O(n log n) + +###### View the algorithm in [action][quick-toptal] + +### Selection +![alt text][selection-image] + +From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. + +__Properties__ +* Worst case performance O(n^2) +* Best case performance O(n^2) +* Average case performance O(n^2) + +###### View the algorithm in [action][selection-toptal] + +### Shell +![alt text][shell-image] + +From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted. + +__Properties__ +* Worst case performance O(nlog2 2n) +* Best case performance O(n log n) +* Average case performance depends on gap sequence + +###### View the algorithm in [action][shell-toptal] + +### Time-Complexity Graphs + +Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) + +[Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) + +---------------------------------------------------------------------------------- + +## Search Algorithms + +### Linear +![alt text][linear-image] + +From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. + Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. + +__Properties__ +* Worst case performance O(n) +* Best case performance O(1) +* Average case performance O(n) +* Worst case space complexity O(1) iterative + +### Binary +![alt text][binary-image] + +From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. + +__Properties__ +* Worst case performance O(log n) +* Best case performance O(1) +* Average case performance O(log n) +* Worst case space complexity O(1) + +---------------------------------------------------------------------------------------------------------------------- + +## Ciphers + +### Caesar +![alt text][caesar]
+In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.
+It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
+The method is named after **Julius Caesar**, who used it in his private correspondence.
+The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security. +###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher) + +### Vigenère +The **Vigenère cipher** is a method of encrypting alphabetic text by using a series of **interwoven Caesar ciphers** based on the letters of a keyword. It is **a form of polyalphabetic substitution**.
+The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del. Sig. Giovan Battista Bellaso; however, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the "Vigenère cipher".
+Though the cipher is easy to understand and implement, for three centuries it resisted all attempts to break it; this earned it the description **le chiffre indéchiffrable**(French for 'the indecipherable cipher'). +Many people have tried to implement encryption schemes that are essentially Vigenère ciphers. Friedrich Kasiski was the first to publish a general method of deciphering a Vigenère cipher in 1863. +###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) + +### Transposition +In cryptography, a **transposition cipher** is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).
+Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. +###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) + +[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort +[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort +[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" + +[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort +[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort +[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort" + +[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort +[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort +[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" + +[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort +[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort +[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort" + +[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort +[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort +[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" + +[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort +[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort +[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" + +[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search +[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif + +[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm +[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png + + +[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg From 7bff4aec87e71ad1db704284f4e50b60b778be49 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Tue, 12 Jun 2018 20:02:17 +0800 Subject: [PATCH 04/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- ...25\345\244\215\346\235\202\345\272\246.md" | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 "post/\347\256\227\346\263\225\345\244\215\346\235\202\345\272\246.md" diff --git a/README.md b/README.md index 24a25226c922..fb8d1165c316 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,5 @@ 2、 ## 参考文献 -1、[o(1), o(n), o(logn), o(nlogn)](https://blog.csdn.net/Mars93/article/details/75194138) +1、 2、 diff --git "a/post/\347\256\227\346\263\225\345\244\215\346\235\202\345\272\246.md" "b/post/\347\256\227\346\263\225\345\244\215\346\235\202\345\272\246.md" new file mode 100644 index 000000000000..5238fe56e045 --- /dev/null +++ "b/post/\347\256\227\346\263\225\345\244\215\346\235\202\345\272\246.md" @@ -0,0 +1,42 @@ + +## 算法复杂度 +1、算法时空复杂度的表示:O(1)、O(n)、O(n^2)、O(logn)、O(nlogn) +2、O后面括号中有一个函数,指明算法的耗时/耗空间与数据增长量之间的关系, +3、其中n代表输入数据的量 +4、复杂度排序:O(n^2) -> O(nlogn) -> O(n) -> O(logn) -> O(1) + +### 复杂度为O(1) +1、O(1)代表最低的时空复杂度, +2、耗时/耗空间与输入数据量大小无关, +3、无论输入数据增大多少倍,耗时/耗空间都不变, + +比如哈希算法就是典型的O(1)时间复杂度, +无论数据规模多大,都可以在一次计算后找到目标(不考虑冲突的话) + +### 复杂度为O(n) +1、O(n)代表当数据量增大几倍,耗时/耗空间也增大几倍, + +比如常见的遍历算法 + +### 复杂度为O(n^2) +1、O(n^2)代表当数据量增大n倍时,耗时/耗空间增大n的平方倍, +2、这是比线性更高的时空复杂度, + +比如冒泡排序,就是典型的O(n^2)的算法,对n个数排序,需要扫描n×n次 + +### 复杂度为O(logn) +1、O(logn)代表当数据增大n倍时,耗时/耗空间增大logn倍(这里的log是以2为底的) +2、当数据量增大256倍时,耗时只增大8倍,是比线性还要低的时间复杂度 + +比如二分查找就是O(logn)的算法,每找一次排除一半的可能, +256个数据中最多只要查找8次就可以找到目标 + +### 复杂度为O(nlogn) +1、O(nlogn)代表当数据量增大n倍时,耗时/耗空间增大n乘以logn倍, +2、当数据量增大256倍时,耗时/耗空间增大256*8=2048倍, +3、这个复杂度高于线性低于平方, + +归并排序就是O(nlogn)的时间复杂度 + +## 参考文献 +1、[o(1), o(n), o(logn), o(nlogn)](https://blog.csdn.net/Mars93/article/details/75194138) From a857ef60b8d4ec795c9dde54908c0b75ebfaacdc Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Tue, 12 Jun 2018 20:05:02 +0800 Subject: [PATCH 05/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fb8d1165c316..7e25c8046875 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ ## 算法&数据结构 -1、 -2、 +1、包含算法和数据结构两大部分 +2、Other收录其他python代码 +3、具体文档请看Post ## 参考文献 -1、 -2、 +1、[TheAlgorithms](https://github.com/TheAlgorithms/Python) From 15d69b4be1a80ac12b33d99f1718584fd3c1a2c3 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 13 Jun 2018 15:52:05 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0hashtable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/hash_table.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 algorithms/hashes/hash_table.py diff --git a/algorithms/hashes/hash_table.py b/algorithms/hashes/hash_table.py new file mode 100644 index 000000000000..809695078d7a --- /dev/null +++ b/algorithms/hashes/hash_table.py @@ -0,0 +1,46 @@ +# -*- coding: UTF-8 -*- + +# Filename : downloader.py +# author by : WeiQi + +class HashTable: + def __init__(self, size): + self.elem = [None for i in range(size)] # 使用list数据结构作为哈希表元素保存方法 + self.count = size # 最大表长 + + def hash(self, key): + return key % self.count # 散列函数采用除留余数法 + + def insert_hash(self, key): + print key + """插入关键字到哈希表内""" + address = self.hash(key) # 求散列地址 + # print address + while self.elem[address] != None: # 当前位置已经有数据了,发生冲突。 + address = self.hash((address+1)) # 线性探测下一地址是否可用 + # print address + self.elem[address] = key # 没有冲突则直接保存。 + print self.elem + + def search_hash(self, key): + """查找关键字,返回布尔值""" + star = address = self.hash(key) + while self.elem[address] != key: + address = self.hash((address + 1)) + if not self.elem[address] or address == star: # 说明没找到或者循环到了开始的位置 + return False + return True + +if __name__ == '__main__': + + list_a = [0, 12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34] + hash_table = HashTable(len(list_a)+100) + + for i in list_a: + hash_table.insert_hash(i) + + for i in hash_table.elem: + print((hash_table.elem.index(i),i)) + + print(hash_table.search_hash(15)) + print(hash_table.search_hash(33)) \ No newline at end of file From 5aa813a75dccd42f0ebdd4698a76f62505d851c8 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 13 Jun 2018 18:31:41 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0hashtable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/hash_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algorithms/hashes/hash_table.py b/algorithms/hashes/hash_table.py index 809695078d7a..331c1adf2b5b 100644 --- a/algorithms/hashes/hash_table.py +++ b/algorithms/hashes/hash_table.py @@ -9,7 +9,7 @@ def __init__(self, size): self.count = size # 最大表长 def hash(self, key): - return key % self.count # 散列函数采用除留余数法 + return key % self.count # 散列函数采用简单的取模运算 def insert_hash(self, key): print key @@ -34,7 +34,7 @@ def search_hash(self, key): if __name__ == '__main__': list_a = [0, 12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34] - hash_table = HashTable(len(list_a)+100) + hash_table = HashTable(len(list_a)) for i in list_a: hash_table.insert_hash(i) From 4e10a52ec51d263392a243e768ff837d10b9b1a2 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 13 Jun 2018 18:33:02 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0hash=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "post/hash\347\256\227\346\263\225.md" | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "post/hash\347\256\227\346\263\225.md" diff --git "a/post/hash\347\256\227\346\263\225.md" "b/post/hash\347\256\227\346\263\225.md" new file mode 100644 index 000000000000..d2ff2c187803 --- /dev/null +++ "b/post/hash\347\256\227\346\263\225.md" @@ -0,0 +1,53 @@ + +## hash algorithm(哈希算法、散列算法) +1、在互联网世界里传送文件来说,如何标志一个文件的身份, +2、我们就需要一种指纹一样的标志来检查文件的可靠性, +3、这种指纹就是我们现在所用的Hash算法(也叫散列算法) + +### hash algorithm是什么 +1、散列算法,是一种从任意文件中创造小的数字「指纹」的方法, +2、与指纹一样,散列算法就是一种以较短的信息来保证文件唯一性的标志, +3、这种标志与文件的每一个字节都相关,而且难以找到逆向规律, +4、因此,当原有文件发生改变时,其标志值也会发生改变, +5、从而告诉文件使用者当前的文件已经不是你所需要的文件, + +### hash algorithm存在的意义 +1、现在大部分的网络部署和版本控制工具都在使用散列算法来保证文件可靠性, +2、而另一方面,我们在进行文件系统同步、备份等工具时, +3、使用散列算法来标志文件唯一性能帮助我们减少系统开销,这一点在很多云存储服务器中都有应用, +4、以Git为代表的众多版本控制工具都在使用SHA1等散列函数检查文件更新, +5、作为一种指纹,散列算法最重要的用途在于给证书、文档、密码等高安全系数的内容添加加密保护, +6、这一方面的用途主要是得益于散列算法的不可逆性, +7、这种不可逆性体现在,你不仅不可能根据一段通过散列算法得到的指纹来获得原有的文件, +8、也不可能简单地创造一个文件并让它的指纹与一段目标指纹相一致, +9、散列算法的这种不可逆性维持着很多安全框架的运营, + +### hash算法特点 +1、正向快速:给定明文和hash算法,在有限时间和有限资源内能计算出hash值, +2、逆向困难:给定(若干)hash值,在有限时间内很难(基本不可能)逆推出明文, +3、输入敏感:原始输入信息修改一点信息,产生的hash值看起来应该都有很大不同, +4、冲突避免:很难找到两段内容不同的明文,使得它们的hash值一致(发生冲突), + +### Hash在在密码学中的应用 +1、在密码学中,hash算法的作用主要是用于消息摘要和签名, +2、换句话说,它主要用于对整个消息的完整性进行校验, + +比如网站密码如果明文保存,特别不安全, +使用hash算法生成一个密码的签名,网站后台只保存这个签名值, +由于hash算法是不可逆的,那么黑客即便得到这个签名,也丝毫没有用处, +而如果你在网站登陆界面上输入你的密码,那么网站后台就会重新计算一下这个hash值, +与网站中储存的原hash值进行比对,如果相同,证明你拥有这个账户的密码,那么就会允许你登陆 + +### hash算法实现 +1、,, +2、 + + + +## 参考文献 +1、[Hash算法总结](https://blog.csdn.net/asdzheng/article/details/70226007) +2、 + + + + From c58c5ce9a0f90035c6559ac06f4bb2043ae964d0 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 13 Jun 2018 21:38:28 +0800 Subject: [PATCH 09/19] =?UTF-8?q?md5=E7=A2=B0=E6=92=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/hash_table.py | 2 +- algorithms/hashes/md5_collision.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 algorithms/hashes/md5_collision.py diff --git a/algorithms/hashes/hash_table.py b/algorithms/hashes/hash_table.py index 331c1adf2b5b..07790078a094 100644 --- a/algorithms/hashes/hash_table.py +++ b/algorithms/hashes/hash_table.py @@ -1,6 +1,6 @@ # -*- coding: UTF-8 -*- -# Filename : downloader.py +# Filename : hash_table.py # author by : WeiQi class HashTable: diff --git a/algorithms/hashes/md5_collision.py b/algorithms/hashes/md5_collision.py new file mode 100644 index 000000000000..5e23f51a41e1 --- /dev/null +++ b/algorithms/hashes/md5_collision.py @@ -0,0 +1,16 @@ +# -*- coding: UTF-8 -*- + +# Filename : md5_collision.py +# author by : WeiQi + +import hashlib + +# 两段HEX字节串,注意它们有细微差别 +a = bytearray.fromhex("0e306561559aa787d00bc6f70bbdfe3404cf03659e704f8534c00ffb659c4c8740cc942feb2da115a3f4155cbb8607497386656d7d1f34a42059d78f5a8dd1ef") +b = bytearray.fromhex("0e306561559aa787d00bc6f70bbdfe3404cf03659e744f8534c00ffb659c4c8740cc942feb2da115a3f415dcbb8607497386656d7d1f34a42059d78f5a8dd1ef") + +if __name__ == "__main__": + # 输出MD5,它们的结果一致 + print(hashlib.md5(a).hexdigest()) + print(len(hashlib.md5(a).hexdigest())) + print(hashlib.md5(b).hexdigest()) \ No newline at end of file From f24c35d6f62e80f0309c15baaadf685f928e7d0d Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 13 Jun 2018 21:38:54 +0800 Subject: [PATCH 10/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0hash=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "post/hash\347\256\227\346\263\225.md" | 30 ++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git "a/post/hash\347\256\227\346\263\225.md" "b/post/hash\347\256\227\346\263\225.md" index d2ff2c187803..dbb8bc15884b 100644 --- "a/post/hash\347\256\227\346\263\225.md" +++ "b/post/hash\347\256\227\346\263\225.md" @@ -39,10 +39,36 @@ 与网站中储存的原hash值进行比对,如果相同,证明你拥有这个账户的密码,那么就会允许你登陆 ### hash算法实现 -1、,, -2、 +1、作为散列算法,首要的功能就是要使用一种算法把原有的体积很大的文件信息用若干个字符来记录, +2、还要保证每一个字节都会对最终结果产生影响。 +3、那么大家也许已经想到了,求模这种算法就能满足我们的需要, +4、事实上,求模算法作为一种不可逆的计算方法,已经成为了整个现代密码学的根基, +5、只要是涉及到计算机安全和加密的领域,都会有模计算的身影, +6、散列算法也并不例外,一种最原始的散列算法就是单纯地选择一个数进行模运算, + +### hash有那些流行算法 +1、MD5 +2、SHA-1 +3、SHA-2 + +### hash算法的'碰撞' +1、作为一种可用的散列算法,其位数一定是有限的, +2、也就是说它能记录的文件是有限的,而文件数量是无限的, +3、两个文件指纹发生碰撞的概率永远不会是零 + +但这并不意味着散列算法就不能用了,因为凡事都要考虑代价, +现代散列算法所存在的理由就是,它的不可逆性能在较大概率上得到实现, +也就是说,发现碰撞的概率很小,这种碰撞能被利用的概率更小, +通过穷举仅仅找到一组碰撞并没有什么实际意义, +更有意义的是,如果我们已经有了一组指纹,能否找到一个原始文件, +让它的散列计算结果等于这组指纹, +如果这一点被实现,我们就可以很容易地篡改和伪造网络证书、密码等关键信息 +### MD5实际碰撞案例 +1、我们之所以说MD5过时,是因为它在某些时候已经很难表现出散列算法的某些优势, +2、比如在应对文件的微小修改时,散列算法得到的指纹结果应当有显著的不同, +2、而algorithms/hashes/md5_collision.py说明了MD5并不能实现这一点, ## 参考文献 1、[Hash算法总结](https://blog.csdn.net/asdzheng/article/details/70226007) From 0a0fc6008e4ec749f4065cb4b26bdbc9163c96e6 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Thu, 14 Jun 2018 00:36:59 +0800 Subject: [PATCH 11/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0hash=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/test_hash.py | 12 ++++++++++++ "post/hash\347\256\227\346\263\225.md" | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 algorithms/hashes/test_hash.py diff --git a/algorithms/hashes/test_hash.py b/algorithms/hashes/test_hash.py new file mode 100644 index 000000000000..ec7ca243ce74 --- /dev/null +++ b/algorithms/hashes/test_hash.py @@ -0,0 +1,12 @@ +# -*- coding: UTF-8 -*- + +# Filename : test_hash.py +# author by : WeiQi + +if __name__ == '__main__': + a=11 + a=a<<1 + print(a) + a=11 + a=a>>1 + print(a) \ No newline at end of file diff --git "a/post/hash\347\256\227\346\263\225.md" "b/post/hash\347\256\227\346\263\225.md" index dbb8bc15884b..965c6c55947d 100644 --- "a/post/hash\347\256\227\346\263\225.md" +++ "b/post/hash\347\256\227\346\263\225.md" @@ -70,6 +70,10 @@ 2、比如在应对文件的微小修改时,散列算法得到的指纹结果应当有显著的不同, 2、而algorithms/hashes/md5_collision.py说明了MD5并不能实现这一点, +### SHA1碰撞 +1、[如何评价2月23日谷歌宣布实现了SHA-1碰撞](https://www.zhihu.com/question/56234281/answer/148349930) +2、对于一些大的商业机构来说,MD5和SHA1已经不够安全,推荐至少使用SHA2-256算法 + ## 参考文献 1、[Hash算法总结](https://blog.csdn.net/asdzheng/article/details/70226007) 2、 From 573589d03b331a08e7b01bddd117d6dc502bf802 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Thu, 14 Jun 2018 16:48:44 +0800 Subject: [PATCH 12/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0hash=5Fmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/hash_map.py | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 algorithms/hashes/hash_map.py diff --git a/algorithms/hashes/hash_map.py b/algorithms/hashes/hash_map.py new file mode 100644 index 000000000000..38f18b2105ef --- /dev/null +++ b/algorithms/hashes/hash_map.py @@ -0,0 +1,76 @@ +# -*- coding: UTF-8 -*- + +# Filename : hash_map.py +# author by : WeiQi + +class LinearMap(object): + def __init__(self): + self.items = [] + + def add(self, k, v): + self.items.append((k, v)) + + def get(self, k): + for key, val in self.items: + if key == k: + return val + raise KeyError + +class BetterMap(object): + def __init__(self, n=100): + self.maps = [] + for i in range(n): + self.maps.append(LinearMap()) + + def find_map(self, k): + index = hash(k) % len(self.maps) + return self.maps[index] + + def add(self, k, v): + m = self.find_map(k) + m.add(k, v) + + def get(self, k): + m = self.find_map(k) + return m.get(k) + +class HashMap(object): + def __init__(self): + self.maps = BetterMap(2) + self.num = 0 + + def get(self, k): + return self.maps.get(k) + + def add(self, k, v): + if self.num == len(self.maps.maps): + self.resize() + + self.maps.add(k, v) + self.num += 1 + print(self.maps.maps) + + def resize(self): + new_map = BetterMap(self.num * 2) + for m in self.maps.maps: + for k, v in m.items: + new_map.add(k, v) + + self.maps = new_map + print('resize -> %d'%len(self.maps.maps)) + +def main(): + import string + m=HashMap() + s='0e306561559aa787d00bc6f70bbdfe3404cf03659e704f8534c00ffb659c4c8740cc942feb2da115a3f4155cbb8607497386656d7d1f34a42059d78f5a8dd1ef' + #s=string.ascii_letters + #s=string.ascii_lowercase + print(s) + for k, v in enumerate(s): + m.add(k, v) + + for k in range(len(s)): + print k, m.get(k) + +if __name__ == '__main__': + main() \ No newline at end of file From 95fce31d1996ba4960a882ac6c380c7096583019 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Fri, 15 Jun 2018 17:05:38 +0800 Subject: [PATCH 13/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0test=5Fhash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/hashes/test_hash.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/hashes/test_hash.py b/algorithms/hashes/test_hash.py index ec7ca243ce74..f931d62e85f1 100644 --- a/algorithms/hashes/test_hash.py +++ b/algorithms/hashes/test_hash.py @@ -9,4 +9,4 @@ print(a) a=11 a=a>>1 - print(a) \ No newline at end of file + print(a) From b7f77a62a12c46272034e06a19715646a0591d0e Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:31:19 +0800 Subject: [PATCH 14/19] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/traversals/binary_tree_traversals.py | 2 +- data_structures/AVL/AVL.py | 2 +- data_structures/{Binary Tree => BinaryTree}/FenwickTree.py | 0 data_structures/{Binary Tree => BinaryTree}/LazySegmentTree.py | 0 data_structures/{Binary Tree => BinaryTree}/SegmentTree.py | 0 .../{Binary Tree => BinaryTree}/binary_search_tree.py | 0 data_structures/{hashing => Hashing}/__init__.py | 0 data_structures/{hashing => Hashing}/double_hash.py | 0 data_structures/{hashing => Hashing}/hash_table.py | 0 .../{hashing => Hashing}/hash_table_with_linked_list.py | 0 data_structures/Hashing/number_theory/__init__.py | 0 .../{hashing => Hashing}/number_theory/prime_numbers.py | 0 data_structures/{hashing => Hashing}/quadratic_probing.py | 0 13 files changed, 2 insertions(+), 2 deletions(-) rename data_structures/{Binary Tree => BinaryTree}/FenwickTree.py (100%) rename data_structures/{Binary Tree => BinaryTree}/LazySegmentTree.py (100%) rename data_structures/{Binary Tree => BinaryTree}/SegmentTree.py (100%) rename data_structures/{Binary Tree => BinaryTree}/binary_search_tree.py (100%) rename data_structures/{hashing => Hashing}/__init__.py (100%) rename data_structures/{hashing => Hashing}/double_hash.py (100%) rename data_structures/{hashing => Hashing}/hash_table.py (100%) rename data_structures/{hashing => Hashing}/hash_table_with_linked_list.py (100%) create mode 100644 data_structures/Hashing/number_theory/__init__.py rename data_structures/{hashing => Hashing}/number_theory/prime_numbers.py (100%) rename data_structures/{hashing => Hashing}/quadratic_probing.py (100%) diff --git a/algorithms/traversals/binary_tree_traversals.py b/algorithms/traversals/binary_tree_traversals.py index cbcaf08b7b03..1df20ea96541 100644 --- a/algorithms/traversals/binary_tree_traversals.py +++ b/algorithms/traversals/binary_tree_traversals.py @@ -86,7 +86,7 @@ def level_order(node): if __name__ == '__main__': - print("\n********* Binary Tree Traversals ************\n") + print("\n********* BinaryTree Traversals ************\n") node = build_tree() print("\n********* Pre Order Traversal ************") diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index d01e8f825368..6b8deea1c715 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -60,7 +60,7 @@ def insert(self, value): self.root.height = 0 self.size = 1 else: - # Same as Binary Tree + # Same as BinaryTree dad_node = None curr_node = self.root diff --git a/data_structures/Binary Tree/FenwickTree.py b/data_structures/BinaryTree/FenwickTree.py similarity index 100% rename from data_structures/Binary Tree/FenwickTree.py rename to data_structures/BinaryTree/FenwickTree.py diff --git a/data_structures/Binary Tree/LazySegmentTree.py b/data_structures/BinaryTree/LazySegmentTree.py similarity index 100% rename from data_structures/Binary Tree/LazySegmentTree.py rename to data_structures/BinaryTree/LazySegmentTree.py diff --git a/data_structures/Binary Tree/SegmentTree.py b/data_structures/BinaryTree/SegmentTree.py similarity index 100% rename from data_structures/Binary Tree/SegmentTree.py rename to data_structures/BinaryTree/SegmentTree.py diff --git a/data_structures/Binary Tree/binary_search_tree.py b/data_structures/BinaryTree/binary_search_tree.py similarity index 100% rename from data_structures/Binary Tree/binary_search_tree.py rename to data_structures/BinaryTree/binary_search_tree.py diff --git a/data_structures/hashing/__init__.py b/data_structures/Hashing/__init__.py similarity index 100% rename from data_structures/hashing/__init__.py rename to data_structures/Hashing/__init__.py diff --git a/data_structures/hashing/double_hash.py b/data_structures/Hashing/double_hash.py similarity index 100% rename from data_structures/hashing/double_hash.py rename to data_structures/Hashing/double_hash.py diff --git a/data_structures/hashing/hash_table.py b/data_structures/Hashing/hash_table.py similarity index 100% rename from data_structures/hashing/hash_table.py rename to data_structures/Hashing/hash_table.py diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/Hashing/hash_table_with_linked_list.py similarity index 100% rename from data_structures/hashing/hash_table_with_linked_list.py rename to data_structures/Hashing/hash_table_with_linked_list.py diff --git a/data_structures/Hashing/number_theory/__init__.py b/data_structures/Hashing/number_theory/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/data_structures/hashing/number_theory/prime_numbers.py b/data_structures/Hashing/number_theory/prime_numbers.py similarity index 100% rename from data_structures/hashing/number_theory/prime_numbers.py rename to data_structures/Hashing/number_theory/prime_numbers.py diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/Hashing/quadratic_probing.py similarity index 100% rename from data_structures/hashing/quadratic_probing.py rename to data_structures/Hashing/quadratic_probing.py From 36d2b4195994f1dae83cc2e8f2f53e6226f82411 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:31:30 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0timing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__init__.py | 0 data_structures/Fundamentals/timing1.py | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+) rename data_structures/{hashing/number_theory => Fundamentals}/__init__.py (100%) create mode 100644 data_structures/Fundamentals/timing1.py diff --git a/data_structures/hashing/number_theory/__init__.py b/data_structures/Fundamentals/__init__.py similarity index 100% rename from data_structures/hashing/number_theory/__init__.py rename to data_structures/Fundamentals/__init__.py diff --git a/data_structures/Fundamentals/timing1.py b/data_structures/Fundamentals/timing1.py new file mode 100644 index 000000000000..3f5a855fae74 --- /dev/null +++ b/data_structures/Fundamentals/timing1.py @@ -0,0 +1,21 @@ +""" +File: timing1.py +Prints the running times for probolem sizes that double, +using a single loop. +""" +import time + +problemSize = 10000000 +print("%12s%16s" % ("Problem Size", "Seconds")) +for count in range(5): + start = time.time() + # The start of the algorithm + work = 1 + for x in range(problemSize): + work += 1 + work -= 1 + # The end of the algorithm + elapsed = time.time() - start + + print("%12d%16.3f" % (problemSize, elapsed)) + problemSize *= 2 \ No newline at end of file From 46c7630ec69a7c80d039443207a7d63c30c4b4d3 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:35:54 +0800 Subject: [PATCH 16/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0counting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structures/Fundamentals/counting.py | 22 +++++++++++++++++++ data_structures/Fundamentals/timing1.py | 2 +- .../Hashing/number_theory/__init__.py | 0 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 data_structures/Fundamentals/counting.py delete mode 100644 data_structures/Hashing/number_theory/__init__.py diff --git a/data_structures/Fundamentals/counting.py b/data_structures/Fundamentals/counting.py new file mode 100644 index 000000000000..24c197737494 --- /dev/null +++ b/data_structures/Fundamentals/counting.py @@ -0,0 +1,22 @@ +""" +File: counting.py +Prints the running times for problem sizes that double, +using a single loop. +""" +import time + +problemSize = 1000 +print("%12s%16s" % ("Problem Size", "Iterations")) +for count in range(5): + number = 0 + # The start of the algorithm + work = 1 + for j in range(problemSize): + for x in range(problemSize): + number += 1 + work += 1 + work -= 1 + # The end of the algorithm + + print("%12d%16.3f" % (problemSize, number)) + problemSize *= 2 \ No newline at end of file diff --git a/data_structures/Fundamentals/timing1.py b/data_structures/Fundamentals/timing1.py index 3f5a855fae74..8f142e91bb51 100644 --- a/data_structures/Fundamentals/timing1.py +++ b/data_structures/Fundamentals/timing1.py @@ -1,6 +1,6 @@ """ File: timing1.py -Prints the running times for probolem sizes that double, +Prints the running times for problem sizes that double, using a single loop. """ import time diff --git a/data_structures/Hashing/number_theory/__init__.py b/data_structures/Hashing/number_theory/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 From 54cc2893a40f6e1cbb0d10e3262bf5f4a3958697 Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:36:31 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0counting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structures/Fundamentals/counting.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/Fundamentals/counting.py b/data_structures/Fundamentals/counting.py index 24c197737494..dfe291058f37 100644 --- a/data_structures/Fundamentals/counting.py +++ b/data_structures/Fundamentals/counting.py @@ -3,7 +3,6 @@ Prints the running times for problem sizes that double, using a single loop. """ -import time problemSize = 1000 print("%12s%16s" % ("Problem Size", "Iterations")) From 180ca72bf15e62fcbfbb101e631e725e1cdb2a3e Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:38:04 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0counting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structures/Fundamentals/countfib.py | 21 +++++++++++++++++++++ data_structures/Fundamentals/counting.py | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 data_structures/Fundamentals/countfib.py diff --git a/data_structures/Fundamentals/countfib.py b/data_structures/Fundamentals/countfib.py new file mode 100644 index 000000000000..292357bc047f --- /dev/null +++ b/data_structures/Fundamentals/countfib.py @@ -0,0 +1,21 @@ +""" +File: countfib.py +Prints the running times for problem sizes that double, +using a single loop. +""" + +problemSize = 1000 +print("%12s%16s" % ("Problem Size", "Iterations")) +for count in range(5): + number = 0 + # The start of the algorithm + work = 1 + for j in range(problemSize): + for x in range(problemSize): + number += 1 + work += 1 + work -= 1 + # The end of the algorithm + + print("%12d%16.3f" % (problemSize, number)) + problemSize *= 2 \ No newline at end of file diff --git a/data_structures/Fundamentals/counting.py b/data_structures/Fundamentals/counting.py index dfe291058f37..52b318845761 100644 --- a/data_structures/Fundamentals/counting.py +++ b/data_structures/Fundamentals/counting.py @@ -1,7 +1,7 @@ """ File: counting.py -Prints the running times for problem sizes that double, -using a single loop. +Prints the number of iterations for problem sizes +that double, using a nested loop. """ problemSize = 1000 From 2bddf4192c02cf6ae458d843610a40cc9259d47d Mon Sep 17 00:00:00 2001 From: huangweiqi Date: Wed, 1 Aug 2018 01:53:50 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0countfib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structures/Fundamentals/countfib.py | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/data_structures/Fundamentals/countfib.py b/data_structures/Fundamentals/countfib.py index 292357bc047f..a29d5b148924 100644 --- a/data_structures/Fundamentals/countfib.py +++ b/data_structures/Fundamentals/countfib.py @@ -1,21 +1,28 @@ """ File: countfib.py -Prints the running times for problem sizes that double, -using a single loop. +Prints the number of calls of a recursive Fibonacci +function with problem that double. """ +from counter import Counter -problemSize = 1000 -print("%12s%16s" % ("Problem Size", "Iterations")) +def fib(n, counter): + """Count the number of calls of the Fibonacci + function.""" + counter.increment() + if n < 3: + return 1 + else: + return fib(n-1, counter) + fib(n-2, counter) + +problemSize = 2 + +print("%12s%16s" % ("Problem Size", "Calls")) for count in range(5): - number = 0 + counter = Counter() + # The start of the algorithm - work = 1 - for j in range(problemSize): - for x in range(problemSize): - number += 1 - work += 1 - work -= 1 + fib(problemSize, counter) # The end of the algorithm - print("%12d%16.3f" % (problemSize, number)) + print("%12d%16.3f" % (problemSize, counter)) problemSize *= 2 \ No newline at end of file