mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Create codespell.yml (#1698)
* fixup! Format Python code with psf/black push * Create codespell.yml * fixup! Format Python code with psf/black push
This commit is contained in:
parent
c01d178798
commit
bfcb95b297
14
.github/workflows/codespell.yml
vendored
Normal file
14
.github/workflows/codespell.yml
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
# GitHub Action to automate the identification of common misspellings in text files
|
||||||
|
# https://github.com/codespell-project/codespell
|
||||||
|
name: codespell
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
codespell:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v1
|
||||||
|
- run: pip install codespell flake8
|
||||||
|
- run: |
|
||||||
|
SKIP="./.*,./other/dictionary.txt,./other/words,./project_euler/problem_22/p022_names.txt,*.bak,*.gif,*.jpeg,*.jpg,*.json,*.png,*.pyc"
|
||||||
|
codespell -L ans,fo,hist,iff,secant,tim --skip=$SKIP
|
12
DIRECTORY.md
12
DIRECTORY.md
|
@ -177,14 +177,14 @@
|
||||||
* [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py)
|
* [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py)
|
||||||
* [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py)
|
* [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py)
|
||||||
* [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py)
|
* [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py)
|
||||||
* [Max Sum Contigous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contigous_subsequence.py)
|
* [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py)
|
||||||
* [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py)
|
* [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py)
|
||||||
* [Rod Cutting](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/rod_cutting.py)
|
* [Rod Cutting](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/rod_cutting.py)
|
||||||
* [Subset Generation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/subset_generation.py)
|
* [Subset Generation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/subset_generation.py)
|
||||||
* [Sum Of Subset](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/sum_of_subset.py)
|
* [Sum Of Subset](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/sum_of_subset.py)
|
||||||
|
|
||||||
## File Transfer
|
## File Transfer
|
||||||
* [Recieve File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/recieve_file.py)
|
* [Receive File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/receive_file.py)
|
||||||
* [Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/send_file.py)
|
* [Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/send_file.py)
|
||||||
|
|
||||||
## Fuzzy Logic
|
## Fuzzy Logic
|
||||||
|
@ -219,7 +219,7 @@
|
||||||
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
|
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
|
||||||
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
|
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
|
||||||
* [Minimum Spanning Tree Prims](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims.py)
|
* [Minimum Spanning Tree Prims](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims.py)
|
||||||
* [Multi Hueristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_hueristic_astar.py)
|
* [Multi Heuristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_heuristic_astar.py)
|
||||||
* [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py)
|
* [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py)
|
||||||
* [Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/prim.py)
|
* [Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/prim.py)
|
||||||
* [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py)
|
* [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py)
|
||||||
|
@ -319,6 +319,7 @@
|
||||||
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py)
|
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py)
|
||||||
* [Simpson Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/simpson_rule.py)
|
* [Simpson Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/simpson_rule.py)
|
||||||
* [Softmax](https://github.com/TheAlgorithms/Python/blob/master/maths/softmax.py)
|
* [Softmax](https://github.com/TheAlgorithms/Python/blob/master/maths/softmax.py)
|
||||||
|
* [Square Root](https://github.com/TheAlgorithms/Python/blob/master/maths/square_root.py)
|
||||||
* [Sum Of Arithmetic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_arithmetic_series.py)
|
* [Sum Of Arithmetic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_arithmetic_series.py)
|
||||||
* [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py)
|
* [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py)
|
||||||
* [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py)
|
* [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py)
|
||||||
|
@ -469,6 +470,8 @@
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_28/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_28/sol1.py)
|
||||||
* Problem 29
|
* Problem 29
|
||||||
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
|
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
|
||||||
|
* Problem 30
|
||||||
|
* [Soln](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/soln.py)
|
||||||
* Problem 31
|
* Problem 31
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
|
||||||
* Problem 32
|
* Problem 32
|
||||||
|
@ -508,6 +511,7 @@
|
||||||
* [Quick Select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py)
|
* [Quick Select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py)
|
||||||
* [Sentinel Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py)
|
* [Sentinel Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py)
|
||||||
* [Simple-Binary-Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple-binary-search.py)
|
* [Simple-Binary-Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple-binary-search.py)
|
||||||
|
* [Simulated Annealing](https://github.com/TheAlgorithms/Python/blob/master/searches/simulated_annealing.py)
|
||||||
* [Tabu Search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py)
|
* [Tabu Search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py)
|
||||||
* [Ternary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py)
|
* [Ternary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py)
|
||||||
|
|
||||||
|
@ -564,7 +568,7 @@
|
||||||
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)
|
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)
|
||||||
* [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py)
|
* [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py)
|
||||||
* [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py)
|
* [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py)
|
||||||
* [Word Occurence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurence.py)
|
* [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py)
|
||||||
|
|
||||||
## Traversals
|
## Traversals
|
||||||
* [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/traversals/binary_tree_traversals.py)
|
* [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/traversals/binary_tree_traversals.py)
|
||||||
|
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
|
@ -40,8 +40,8 @@ def isSafe(board, row, column):
|
||||||
|
|
||||||
def solve(board, row):
|
def solve(board, row):
|
||||||
"""
|
"""
|
||||||
It creates a state space tree and calls the safe function untill it receives a
|
It creates a state space tree and calls the safe function until it receives a
|
||||||
False Boolean and terminates that brach and backtracks to the next
|
False Boolean and terminates that branch and backtracks to the next
|
||||||
poosible solution branch.
|
poosible solution branch.
|
||||||
"""
|
"""
|
||||||
if row >= len(board):
|
if row >= len(board):
|
||||||
|
@ -58,7 +58,7 @@ def solve(board, row):
|
||||||
"""
|
"""
|
||||||
For every row it iterates through each column to check if it is feesible to place a
|
For every row it iterates through each column to check if it is feesible to place a
|
||||||
queen there.
|
queen there.
|
||||||
If all the combinations for that particaular branch are successfull the board is
|
If all the combinations for that particular branch are successful the board is
|
||||||
reinitialized for the next possible combination.
|
reinitialized for the next possible combination.
|
||||||
"""
|
"""
|
||||||
if isSafe(board, row, i):
|
if isSafe(board, row, i):
|
||||||
|
@ -70,7 +70,7 @@ def solve(board, row):
|
||||||
|
|
||||||
def printboard(board):
|
def printboard(board):
|
||||||
"""
|
"""
|
||||||
Prints the boards that have a successfull combination.
|
Prints the boards that have a successful combination.
|
||||||
"""
|
"""
|
||||||
for i in range(len(board)):
|
for i in range(len(board)):
|
||||||
for j in range(len(board)):
|
for j in range(len(board)):
|
||||||
|
|
|
@ -3,15 +3,15 @@
|
||||||
Hill Cipher:
|
Hill Cipher:
|
||||||
The below defined class 'HillCipher' implements the Hill Cipher algorithm.
|
The below defined class 'HillCipher' implements the Hill Cipher algorithm.
|
||||||
The Hill Cipher is an algorithm that implements modern linear algebra techniques
|
The Hill Cipher is an algorithm that implements modern linear algebra techniques
|
||||||
In this algortihm, you have an encryption key matrix. This is what will be used
|
In this algorithm, you have an encryption key matrix. This is what will be used
|
||||||
in encoding and decoding your text.
|
in encoding and decoding your text.
|
||||||
|
|
||||||
Algortihm:
|
Algorithm:
|
||||||
Let the order of the encryption key be N (as it is a square matrix).
|
Let the order of the encryption key be N (as it is a square matrix).
|
||||||
Your text is divided into batches of length N and converted to numerical vectors
|
Your text is divided into batches of length N and converted to numerical vectors
|
||||||
by a simple mapping starting with A=0 and so on.
|
by a simple mapping starting with A=0 and so on.
|
||||||
|
|
||||||
The key is then mulitplied with the newly created batch vector to obtain the
|
The key is then multiplied with the newly created batch vector to obtain the
|
||||||
encoded vector. After each multiplication modular 36 calculations are performed
|
encoded vector. After each multiplication modular 36 calculations are performed
|
||||||
on the vectors so as to bring the numbers between 0 and 36 and then mapped with
|
on the vectors so as to bring the numbers between 0 and 36 and then mapped with
|
||||||
their corresponding alphanumerics.
|
their corresponding alphanumerics.
|
||||||
|
|
|
@ -6,7 +6,7 @@ BYTE_SIZE = 256
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
filename = "encrypted_file.txt"
|
filename = "encrypted_file.txt"
|
||||||
response = input(r"Encrypte\Decrypt [e\d]: ")
|
response = input(r"Encrypt\Decrypt [e\d]: ")
|
||||||
|
|
||||||
if response.lower().startswith("e"):
|
if response.lower().startswith("e"):
|
||||||
mode = "encrypt"
|
mode = "encrypt"
|
||||||
|
|
|
@ -42,12 +42,12 @@ def makeKeyFiles(name, keySize):
|
||||||
|
|
||||||
publicKey, privateKey = generateKey(keySize)
|
publicKey, privateKey = generateKey(keySize)
|
||||||
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
||||||
with open("%s_pubkey.txt" % name, "w") as fo:
|
with open("%s_pubkey.txt" % name, "w") as out_file:
|
||||||
fo.write("{},{},{}".format(keySize, publicKey[0], publicKey[1]))
|
out_file.write("{},{},{}".format(keySize, publicKey[0], publicKey[1]))
|
||||||
|
|
||||||
print("Writing private key to file %s_privkey.txt..." % name)
|
print("Writing private key to file %s_privkey.txt..." % name)
|
||||||
with open("%s_privkey.txt" % name, "w") as fo:
|
with open("%s_privkey.txt" % name, "w") as out_file:
|
||||||
fo.write("{},{},{}".format(keySize, privateKey[0], privateKey[1]))
|
out_file.write("{},{},{}".format(keySize, privateKey[0], privateKey[1]))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -157,11 +157,11 @@ if __name__ == "__main__":
|
||||||
entry_msg = "Provide a string that I will generate its BWT transform: "
|
entry_msg = "Provide a string that I will generate its BWT transform: "
|
||||||
s = input(entry_msg).strip()
|
s = input(entry_msg).strip()
|
||||||
result = bwt_transform(s)
|
result = bwt_transform(s)
|
||||||
bwt_output_msg = "Burrows Wheeler tranform for string '{}' results in '{}'"
|
bwt_output_msg = "Burrows Wheeler transform for string '{}' results in '{}'"
|
||||||
print(bwt_output_msg.format(s, result["bwt_string"]))
|
print(bwt_output_msg.format(s, result["bwt_string"]))
|
||||||
original_string = reverse_bwt(result["bwt_string"], result["idx_original_string"])
|
original_string = reverse_bwt(result["bwt_string"], result["idx_original_string"])
|
||||||
fmt = (
|
fmt = (
|
||||||
"Reversing Burrows Wheeler tranform for entry '{}' we get original"
|
"Reversing Burrows Wheeler transform for entry '{}' we get original"
|
||||||
" string '{}'"
|
" string '{}'"
|
||||||
)
|
)
|
||||||
print(fmt.format(result["bwt_string"], original_string))
|
print(fmt.format(result["bwt_string"], original_string))
|
||||||
|
|
|
@ -21,7 +21,7 @@ class TreeNode:
|
||||||
def parse_file(file_path):
|
def parse_file(file_path):
|
||||||
"""
|
"""
|
||||||
Read the file and build a dict of all letters and their
|
Read the file and build a dict of all letters and their
|
||||||
frequences, then convert the dict into a list of Letters.
|
frequencies, then convert the dict into a list of Letters.
|
||||||
"""
|
"""
|
||||||
chars = {}
|
chars = {}
|
||||||
with open(file_path) as f:
|
with open(file_path) as f:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
||||||
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
|
Source: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -24,7 +24,7 @@ def decimal_to_octal(num: int) -> str:
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Print octal equivelents of decimal numbers."""
|
"""Print octal equivalents of decimal numbers."""
|
||||||
print("\n2 in octal is:")
|
print("\n2 in octal is:")
|
||||||
print(decimal_to_octal(2)) # = 2
|
print(decimal_to_octal(2)) # = 2
|
||||||
print("\n8 in octal is:")
|
print("\n8 in octal is:")
|
||||||
|
|
|
@ -15,7 +15,7 @@ class Node:
|
||||||
|
|
||||||
if self.left is None and self.right is None:
|
if self.left is None and self.right is None:
|
||||||
return str(self.value)
|
return str(self.value)
|
||||||
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1,)
|
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1)
|
||||||
|
|
||||||
|
|
||||||
class BinarySearchTree:
|
class BinarySearchTree:
|
||||||
|
|
|
@ -11,7 +11,7 @@ def swap(a, b):
|
||||||
return a, b
|
return a, b
|
||||||
|
|
||||||
|
|
||||||
# creating sparse table which saves each nodes 2^ith parent
|
# creating sparse table which saves each nodes 2^i-th parent
|
||||||
def creatSparse(max_node, parent):
|
def creatSparse(max_node, parent):
|
||||||
j = 1
|
j = 1
|
||||||
while (1 << j) < max_node:
|
while (1 << j) < max_node:
|
||||||
|
|
|
@ -41,7 +41,7 @@ def binomial_coefficient(n: int, k: int) -> int:
|
||||||
|
|
||||||
def catalan_number(node_count: int) -> int:
|
def catalan_number(node_count: int) -> int:
|
||||||
"""
|
"""
|
||||||
We can find Catalan number many ways but here we use Binomial Coefficent because it
|
We can find Catalan number many ways but here we use Binomial Coefficient because it
|
||||||
does the job in O(n)
|
does the job in O(n)
|
||||||
|
|
||||||
return the Catalan number of n using 2nCn/(n+1).
|
return the Catalan number of n using 2nCn/(n+1).
|
||||||
|
|
|
@ -12,7 +12,7 @@ class RedBlackTree:
|
||||||
less strict, so it will perform faster for writing/deleting nodes
|
less strict, so it will perform faster for writing/deleting nodes
|
||||||
and slower for reading in the average case, though, because they're
|
and slower for reading in the average case, though, because they're
|
||||||
both balanced binary search trees, both will get the same asymptotic
|
both balanced binary search trees, both will get the same asymptotic
|
||||||
perfomance.
|
performance.
|
||||||
To read more about them, https://en.wikipedia.org/wiki/Red–black_tree
|
To read more about them, https://en.wikipedia.org/wiki/Red–black_tree
|
||||||
Unless otherwise specified, all asymptotic runtimes are specified in
|
Unless otherwise specified, all asymptotic runtimes are specified in
|
||||||
terms of the size of the tree.
|
terms of the size of the tree.
|
||||||
|
@ -37,7 +37,7 @@ class RedBlackTree:
|
||||||
def rotate_left(self):
|
def rotate_left(self):
|
||||||
"""Rotate the subtree rooted at this node to the left and
|
"""Rotate the subtree rooted at this node to the left and
|
||||||
returns the new root to this subtree.
|
returns the new root to this subtree.
|
||||||
Perfoming one rotation can be done in O(1).
|
Performing one rotation can be done in O(1).
|
||||||
"""
|
"""
|
||||||
parent = self.parent
|
parent = self.parent
|
||||||
right = self.right
|
right = self.right
|
||||||
|
@ -656,7 +656,7 @@ def test_tree_traversal():
|
||||||
|
|
||||||
|
|
||||||
def test_tree_chaining():
|
def test_tree_chaining():
|
||||||
"""Tests the three different tree chaning functions."""
|
"""Tests the three different tree chaining functions."""
|
||||||
tree = RedBlackTree(0)
|
tree = RedBlackTree(0)
|
||||||
tree = tree.insert(-16).insert(16).insert(8).insert(24).insert(20).insert(22)
|
tree = tree.insert(-16).insert(16).insert(8).insert(24).insert(20).insert(22)
|
||||||
if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]:
|
if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]:
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Node:
|
||||||
return f"'{self.value}: {self.prior:.5}'"
|
return f"'{self.value}: {self.prior:.5}'"
|
||||||
else:
|
else:
|
||||||
return pformat(
|
return pformat(
|
||||||
{f"{self.value}: {self.prior:.5}": (self.left, self.right)}, indent=1,
|
{f"{self.value}: {self.prior:.5}": (self.left, self.right)}, indent=1
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -161,7 +161,7 @@ def main():
|
||||||
"""After each command, program prints treap"""
|
"""After each command, program prints treap"""
|
||||||
root = None
|
root = None
|
||||||
print(
|
print(
|
||||||
"enter numbers to creat a tree, + value to add value into treap, - value to erase all nodes with value. 'q' to quit. "
|
"enter numbers to create a tree, + value to add value into treap, - value to erase all nodes with value. 'q' to quit. "
|
||||||
)
|
)
|
||||||
|
|
||||||
args = input()
|
args = input()
|
||||||
|
|
|
@ -49,7 +49,7 @@ class BinomialHeap:
|
||||||
r"""
|
r"""
|
||||||
Min-oriented priority queue implemented with the Binomial Heap data
|
Min-oriented priority queue implemented with the Binomial Heap data
|
||||||
structure implemented with the BinomialHeap class. It supports:
|
structure implemented with the BinomialHeap class. It supports:
|
||||||
- Insert element in a heap with n elemnts: Guaranteed logn, amoratized 1
|
- Insert element in a heap with n elements: Guaranteed logn, amoratized 1
|
||||||
- Merge (meld) heaps of size m and n: O(logn + logm)
|
- Merge (meld) heaps of size m and n: O(logn + logm)
|
||||||
- Delete Min: O(logn)
|
- Delete Min: O(logn)
|
||||||
- Peek (return min without deleting it): O(1)
|
- Peek (return min without deleting it): O(1)
|
||||||
|
|
|
@ -23,6 +23,7 @@ class Heap(object):
|
||||||
[1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201]
|
[1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201]
|
||||||
>>>
|
>>>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.h = []
|
self.h = []
|
||||||
self.curr_size = 0
|
self.curr_size = 0
|
||||||
|
@ -107,28 +108,28 @@ def main():
|
||||||
[2, 5, 3, 0, 2, 3, 0, 3],
|
[2, 5, 3, 0, 2, 3, 0, 3],
|
||||||
[6, 1, 2, 7, 9, 3, 4, 5, 10, 8],
|
[6, 1, 2, 7, 9, 3, 4, 5, 10, 8],
|
||||||
[103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5],
|
[103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5],
|
||||||
[-45, -2, -5]
|
[-45, -2, -5],
|
||||||
]:
|
]:
|
||||||
print('source unsorted list: %s' % unsorted)
|
print("source unsorted list: %s" % unsorted)
|
||||||
|
|
||||||
h = Heap()
|
h = Heap()
|
||||||
h.build_heap(unsorted)
|
h.build_heap(unsorted)
|
||||||
print('after build heap: ', end=' ')
|
print("after build heap: ", end=" ")
|
||||||
h.display()
|
h.display()
|
||||||
|
|
||||||
print('max value: %s' % h.get_max())
|
print("max value: %s" % h.get_max())
|
||||||
print('delete max value: ', end=' ')
|
print("delete max value: ", end=" ")
|
||||||
h.display()
|
h.display()
|
||||||
|
|
||||||
h.insert(100)
|
h.insert(100)
|
||||||
print('after insert new value 100: ', end=' ')
|
print("after insert new value 100: ", end=" ")
|
||||||
h.display()
|
h.display()
|
||||||
|
|
||||||
h.heap_sort()
|
h.heap_sort()
|
||||||
print('heap sort: ', end=' ')
|
print("heap sort: ", end=" ")
|
||||||
h.display()
|
h.display()
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -3,7 +3,7 @@ Implementing Deque using DoublyLinkedList ...
|
||||||
Operations:
|
Operations:
|
||||||
1. insertion in the front -> O(1)
|
1. insertion in the front -> O(1)
|
||||||
2. insertion in the end -> O(1)
|
2. insertion in the end -> O(1)
|
||||||
3. remove fron the front -> O(1)
|
3. remove from the front -> O(1)
|
||||||
4. remove from the end -> O(1)
|
4. remove from the end -> O(1)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
- This is an example of a double ended, doubly linked list.
|
- This is an example of a double ended, doubly linked list.
|
||||||
- Each link references the next link and the previous one.
|
- Each link references the next link and the previous one.
|
||||||
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
|
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
|
||||||
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent"""
|
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficient"""
|
||||||
|
|
||||||
|
|
||||||
class LinkedList: # making main class named linked list
|
class LinkedList: # making main class named linked list
|
||||||
|
|
|
@ -79,7 +79,7 @@ class LinkedList:
|
||||||
# END represents end of the LinkedList
|
# END represents end of the LinkedList
|
||||||
return string_repr + "END"
|
return string_repr + "END"
|
||||||
|
|
||||||
# Indexing Support. Used to get a node at particaular position
|
# Indexing Support. Used to get a node at particular position
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
current = self.head
|
current = self.head
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class LinkedList:
|
||||||
def print_list(self):
|
def print_list(self):
|
||||||
temp = self.head
|
temp = self.head
|
||||||
while temp is not None:
|
while temp is not None:
|
||||||
print(temp.data, end=' ')
|
print(temp.data, end=" ")
|
||||||
temp = temp.next
|
temp = temp.next
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ def Solve(Postfix):
|
||||||
|
|
||||||
Stack.append(
|
Stack.append(
|
||||||
str(Opr[x](int(A), int(B)))
|
str(Opr[x](int(A), int(B)))
|
||||||
) # evaluate the 2 values poped from stack & push result to stack
|
) # evaluate the 2 values popped from stack & push result to stack
|
||||||
print(
|
print(
|
||||||
x.rjust(8),
|
x.rjust(8),
|
||||||
("push(" + A + x + B + ")").ljust(12),
|
("push(" + A + x + B + ")").ljust(12),
|
||||||
|
|
|
@ -21,7 +21,7 @@ def calculateSpan(price, S):
|
||||||
# Calculate span values for rest of the elements
|
# Calculate span values for rest of the elements
|
||||||
for i in range(1, n):
|
for i in range(1, n):
|
||||||
|
|
||||||
# Pop elements from stack whlie stack is not
|
# Pop elements from stack while stack is not
|
||||||
# empty and top of stack is smaller than price[i]
|
# empty and top of stack is smaller than price[i]
|
||||||
while len(st) > 0 and price[st[0]] <= price[i]:
|
while len(st) > 0 and price[st[0]] <= price[i]:
|
||||||
st.pop()
|
st.pop()
|
||||||
|
|
|
@ -541,7 +541,7 @@ cl = IndexCalculation()
|
||||||
# instantiating the class with the values
|
# instantiating the class with the values
|
||||||
#cl = indexCalculation(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir)
|
#cl = indexCalculation(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir)
|
||||||
|
|
||||||
# how set the values after instantiate the class cl, (for update the data or when dont
|
# how set the values after instantiate the class cl, (for update the data or when don't
|
||||||
# instantiating the class with the values)
|
# instantiating the class with the values)
|
||||||
cl.setMatrices(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir)
|
cl.setMatrices(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir)
|
||||||
|
|
||||||
|
@ -551,8 +551,8 @@ indexValue_form1 = cl.calculation("CCCI", red=red, green=green, blue=blue,
|
||||||
redEdge=redEdge, nir=nir).astype(np.float64)
|
redEdge=redEdge, nir=nir).astype(np.float64)
|
||||||
indexValue_form2 = cl.CCCI()
|
indexValue_form2 = cl.CCCI()
|
||||||
|
|
||||||
# calculating the index with the values directly -- you can set just the values preferred --
|
# calculating the index with the values directly -- you can set just the values
|
||||||
# note: the *calculation* fuction performs the function *setMatrices*
|
# preferred note: the *calculation* function performs the function *setMatrices*
|
||||||
indexValue_form3 = cl.calculation("CCCI", red=red, green=green, blue=blue,
|
indexValue_form3 = cl.calculation("CCCI", red=red, green=green, blue=blue,
|
||||||
redEdge=redEdge, nir=nir).astype(np.float64)
|
redEdge=redEdge, nir=nir).astype(np.float64)
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ def test_gen_gaussian_kernel():
|
||||||
# canny.py
|
# canny.py
|
||||||
def test_canny():
|
def test_canny():
|
||||||
canny_img = imread("digital_image_processing/image_data/lena_small.jpg", 0)
|
canny_img = imread("digital_image_processing/image_data/lena_small.jpg", 0)
|
||||||
# assert ambiguos array for all == True
|
# assert ambiguous array for all == True
|
||||||
assert canny_img.all()
|
assert canny_img.all()
|
||||||
canny_array = canny.canny(canny_img)
|
canny_array = canny.canny(canny_img)
|
||||||
# assert canny array for at least one True
|
# assert canny array for at least one True
|
||||||
|
|
|
@ -42,7 +42,7 @@ class AssignmentUsingBitmask:
|
||||||
if self.dp[mask][taskno] != -1:
|
if self.dp[mask][taskno] != -1:
|
||||||
return self.dp[mask][taskno]
|
return self.dp[mask][taskno]
|
||||||
|
|
||||||
# Number of ways when we dont this task in the arrangement
|
# Number of ways when we don't this task in the arrangement
|
||||||
total_ways_util = self.CountWaysUtil(mask, taskno + 1)
|
total_ways_util = self.CountWaysUtil(mask, taskno + 1)
|
||||||
|
|
||||||
# now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks.
|
# now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks.
|
||||||
|
|
|
@ -49,9 +49,9 @@ def knapsack_with_example_solution(W: int, wt: list, val: list):
|
||||||
|
|
||||||
W: int, the total maximum weight for the given knapsack problem.
|
W: int, the total maximum weight for the given knapsack problem.
|
||||||
wt: list, the vector of weights for all items where wt[i] is the weight
|
wt: list, the vector of weights for all items where wt[i] is the weight
|
||||||
of the ith item.
|
of the i-th item.
|
||||||
val: list, the vector of values for all items where val[i] is the value
|
val: list, the vector of values for all items where val[i] is the value
|
||||||
of te ith item
|
of the i-th item
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Auther : Yvonne
|
Author : Yvonne
|
||||||
|
|
||||||
This is a pure Python implementation of Dynamic Programming solution to the longest_sub_array problem.
|
This is a pure Python implementation of Dynamic Programming solution to the longest_sub_array problem.
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
# python program to print all subset combination of n element in given set of r element .
|
# Python program to print all subset combinations of n element in given set of r element.
|
||||||
# arr[] ---> Input Array
|
# arr[] ---> Input Array
|
||||||
# data[] ---> Temporary array to store current combination
|
# data[] ---> Temporary array to store current combination
|
||||||
# start & end ---> Staring and Ending indexes in arr[]
|
# start & end ---> Staring and Ending indexes in arr[]
|
||||||
# index ---> Current index in data[]
|
# index ---> Current index in data[]
|
||||||
# r ---> Size of a combination to be printed
|
# r ---> Size of a combination to be printed
|
||||||
def combinationUtil(arr, n, r, index, data, i):
|
def combination_util(arr, n, r, index, data, i):
|
||||||
# Current combination is ready to be printed,
|
# Current combination is ready to be printed,
|
||||||
# print it
|
# print it
|
||||||
if index == r:
|
if index == r:
|
||||||
|
@ -15,29 +15,26 @@ def combinationUtil(arr, n, r, index, data, i):
|
||||||
# When no more elements are there to put in data[]
|
# When no more elements are there to put in data[]
|
||||||
if i >= n:
|
if i >= n:
|
||||||
return
|
return
|
||||||
# current is included, put next at next
|
# current is included, put next at next location
|
||||||
# location
|
|
||||||
data[index] = arr[i]
|
data[index] = arr[i]
|
||||||
combinationUtil(arr, n, r, index + 1, data, i + 1)
|
combination_util(arr, n, r, index + 1, data, i + 1)
|
||||||
# current is excluded, replace it with
|
# current is excluded, replace it with
|
||||||
# next (Note that i+1 is passed, but
|
# next (Note that i+1 is passed, but
|
||||||
# index is not changed)
|
# index is not changed)
|
||||||
combinationUtil(arr, n, r, index, data, i + 1)
|
combination_util(arr, n, r, index, data, i + 1)
|
||||||
# The main function that prints all combinations
|
# The main function that prints all combinations
|
||||||
# of size r in arr[] of size n. This function
|
# of size r in arr[] of size n. This function
|
||||||
# mainly uses combinationUtil()
|
# mainly uses combinationUtil()
|
||||||
|
|
||||||
|
|
||||||
def printcombination(arr, n, r):
|
def print_combination(arr, n, r):
|
||||||
# A temporary array to store all combination
|
# A temporary array to store all combination one by one
|
||||||
# one by one
|
|
||||||
data = [0] * r
|
data = [0] * r
|
||||||
# Print all combination using temprary
|
# Print all combination using temporary array 'data[]'
|
||||||
# array 'data[]'
|
combination_util(arr, n, r, 0, data, 0)
|
||||||
combinationUtil(arr, n, r, 0, data, 0)
|
|
||||||
|
|
||||||
|
|
||||||
# Driver function to check for above function
|
# Driver function to check for above function
|
||||||
arr = [10, 20, 30, 40, 50]
|
arr = [10, 20, 30, 40, 50]
|
||||||
printcombination(arr, len(arr), 3)
|
print_combination(arr, len(arr), 3)
|
||||||
# This code is contributed by Ambuj sahu
|
# This code is contributed by Ambuj sahu
|
||||||
|
|
|
@ -35,7 +35,7 @@ def search(grid, init, goal, cost, heuristic):
|
||||||
|
|
||||||
closed = [
|
closed = [
|
||||||
[0 for col in range(len(grid[0]))] for row in range(len(grid))
|
[0 for col in range(len(grid[0]))] for row in range(len(grid))
|
||||||
] # the referrence grid
|
] # the reference grid
|
||||||
closed[init[0]][init[1]] = 1
|
closed[init[0]][init[1]] = 1
|
||||||
action = [
|
action = [
|
||||||
[0 for col in range(len(grid[0]))] for row in range(len(grid))
|
[0 for col in range(len(grid[0]))] for row in range(len(grid))
|
||||||
|
|
|
@ -69,7 +69,7 @@ def dfs(G, s):
|
||||||
Args : G - Dictionary of edges
|
Args : G - Dictionary of edges
|
||||||
s - Starting Node
|
s - Starting Node
|
||||||
Vars : vis - Set of visited nodes
|
Vars : vis - Set of visited nodes
|
||||||
Q - Traveral Stack
|
Q - Traversal Stack
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
|
@ -7,12 +7,12 @@ class Graph:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.vertex = {}
|
self.vertex = {}
|
||||||
|
|
||||||
# for printing the Graph vertexes
|
# for printing the Graph vertices
|
||||||
def printGraph(self):
|
def printGraph(self):
|
||||||
for i in self.vertex.keys():
|
for i in self.vertex.keys():
|
||||||
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
||||||
|
|
||||||
# for adding the edge beween two vertexes
|
# for adding the edge between two vertices
|
||||||
def addEdge(self, fromVertex, toVertex):
|
def addEdge(self, fromVertex, toVertex):
|
||||||
# check if vertex is already present,
|
# check if vertex is already present,
|
||||||
if fromVertex in self.vertex.keys():
|
if fromVertex in self.vertex.keys():
|
||||||
|
@ -22,10 +22,10 @@ class Graph:
|
||||||
self.vertex[fromVertex] = [toVertex]
|
self.vertex[fromVertex] = [toVertex]
|
||||||
|
|
||||||
def BFS(self, startVertex):
|
def BFS(self, startVertex):
|
||||||
# Take a list for stoting already visited vertexes
|
# Take a list for stoting already visited vertices
|
||||||
visited = [False] * len(self.vertex)
|
visited = [False] * len(self.vertex)
|
||||||
|
|
||||||
# create a list to store all the vertexes for BFS
|
# create a list to store all the vertices for BFS
|
||||||
queue = []
|
queue = []
|
||||||
|
|
||||||
# mark the source node as visited and enqueue it
|
# mark the source node as visited and enqueue it
|
||||||
|
|
|
@ -7,13 +7,13 @@ class Graph:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.vertex = {}
|
self.vertex = {}
|
||||||
|
|
||||||
# for printing the Graph vertexes
|
# for printing the Graph vertices
|
||||||
def printGraph(self):
|
def printGraph(self):
|
||||||
print(self.vertex)
|
print(self.vertex)
|
||||||
for i in self.vertex.keys():
|
for i in self.vertex.keys():
|
||||||
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
||||||
|
|
||||||
# for adding the edge beween two vertexes
|
# for adding the edge between two vertices
|
||||||
def addEdge(self, fromVertex, toVertex):
|
def addEdge(self, fromVertex, toVertex):
|
||||||
# check if vertex is already present,
|
# check if vertex is already present,
|
||||||
if fromVertex in self.vertex.keys():
|
if fromVertex in self.vertex.keys():
|
||||||
|
@ -37,7 +37,7 @@ class Graph:
|
||||||
|
|
||||||
print(startVertex, end=" ")
|
print(startVertex, end=" ")
|
||||||
|
|
||||||
# Recur for all the vertexes that are adjacent to this node
|
# Recur for all the vertices that are adjacent to this node
|
||||||
for i in self.vertex.keys():
|
for i in self.vertex.keys():
|
||||||
if visited[i] == False:
|
if visited[i] == False:
|
||||||
self.DFSRec(i, visited)
|
self.DFSRec(i, visited)
|
||||||
|
|
|
@ -22,7 +22,7 @@ DIJKSTRA(graph G, start vertex s, destination vertex d):
|
||||||
13 - add (total_cost,V) to H
|
13 - add (total_cost,V) to H
|
||||||
|
|
||||||
You can think at cost as a distance where Dijkstra finds the shortest distance
|
You can think at cost as a distance where Dijkstra finds the shortest distance
|
||||||
between vertexes s and v in a graph G. The use of a min heap as H guarantees
|
between vertices s and v in a graph G. The use of a min heap as H guarantees
|
||||||
that if a vertex has already been explored there will be no other path with
|
that if a vertex has already been explored there will be no other path with
|
||||||
shortest distance, that happens because heapq.heappop will always return the
|
shortest distance, that happens because heapq.heappop will always return the
|
||||||
next vertex with the shortest distance, considering that the heap stores not
|
next vertex with the shortest distance, considering that the heap stores not
|
||||||
|
@ -35,7 +35,7 @@ import heapq
|
||||||
|
|
||||||
|
|
||||||
def dijkstra(graph, start, end):
|
def dijkstra(graph, start, end):
|
||||||
"""Return the cost of the shortest path between vertexes start and end.
|
"""Return the cost of the shortest path between vertices start and end.
|
||||||
|
|
||||||
>>> dijkstra(G, "E", "C")
|
>>> dijkstra(G, "E", "C")
|
||||||
6
|
6
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# For storing the vertex set to retreive node with the lowest distance
|
# For storing the vertex set to retrieve node with the lowest distance
|
||||||
|
|
||||||
|
|
||||||
class PriorityQueue:
|
class PriorityQueue:
|
||||||
|
@ -103,9 +103,7 @@ class Graph:
|
||||||
def show_graph(self):
|
def show_graph(self):
|
||||||
# u -> v(w)
|
# u -> v(w)
|
||||||
for u in self.adjList:
|
for u in self.adjList:
|
||||||
print(
|
print(u, "->", " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]))
|
||||||
u, "->", " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]),
|
|
||||||
)
|
|
||||||
|
|
||||||
def dijkstra(self, src):
|
def dijkstra(self, src):
|
||||||
# Flush old junk values in par[]
|
# Flush old junk values in par[]
|
||||||
|
|
|
@ -3,7 +3,7 @@ import random as rand
|
||||||
import math as math
|
import math as math
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# the dfault weight is 1 if not assigend but all the implementation is weighted
|
# the dfault weight is 1 if not assigned but all the implementation is weighted
|
||||||
|
|
||||||
|
|
||||||
class DirectedGraph:
|
class DirectedGraph:
|
||||||
|
@ -12,7 +12,7 @@ class DirectedGraph:
|
||||||
|
|
||||||
# adding vertices and edges
|
# adding vertices and edges
|
||||||
# adding the weight is optional
|
# adding the weight is optional
|
||||||
# handels repetition
|
# handles repetition
|
||||||
def add_pair(self, u, v, w=1):
|
def add_pair(self, u, v, w=1):
|
||||||
if self.graph.get(u):
|
if self.graph.get(u):
|
||||||
if self.graph[u].count([w, v]) == 0:
|
if self.graph[u].count([w, v]) == 0:
|
||||||
|
@ -25,14 +25,14 @@ class DirectedGraph:
|
||||||
def all_nodes(self):
|
def all_nodes(self):
|
||||||
return list(self.graph)
|
return list(self.graph)
|
||||||
|
|
||||||
# handels if the input does not exist
|
# handles if the input does not exist
|
||||||
def remove_pair(self, u, v):
|
def remove_pair(self, u, v):
|
||||||
if self.graph.get(u):
|
if self.graph.get(u):
|
||||||
for _ in self.graph[u]:
|
for _ in self.graph[u]:
|
||||||
if _[1] == v:
|
if _[1] == v:
|
||||||
self.graph[u].remove(_)
|
self.graph[u].remove(_)
|
||||||
|
|
||||||
# if no destination is meant the defaut value is -1
|
# if no destination is meant the default value is -1
|
||||||
def dfs(self, s=-2, d=-1):
|
def dfs(self, s=-2, d=-1):
|
||||||
if s == d:
|
if s == d:
|
||||||
return []
|
return []
|
||||||
|
@ -71,7 +71,7 @@ class DirectedGraph:
|
||||||
if len(stack) == 0:
|
if len(stack) == 0:
|
||||||
return visited
|
return visited
|
||||||
|
|
||||||
# c is the count of nodes you want and if you leave it or pass -1 to the funtion the count
|
# c is the count of nodes you want and if you leave it or pass -1 to the function the count
|
||||||
# will be random from 10 to 10000
|
# will be random from 10 to 10000
|
||||||
def fill_graph_randomly(self, c=-1):
|
def fill_graph_randomly(self, c=-1):
|
||||||
if c == -1:
|
if c == -1:
|
||||||
|
@ -271,7 +271,7 @@ class Graph:
|
||||||
|
|
||||||
# adding vertices and edges
|
# adding vertices and edges
|
||||||
# adding the weight is optional
|
# adding the weight is optional
|
||||||
# handels repetition
|
# handles repetition
|
||||||
def add_pair(self, u, v, w=1):
|
def add_pair(self, u, v, w=1):
|
||||||
# check if the u exists
|
# check if the u exists
|
||||||
if self.graph.get(u):
|
if self.graph.get(u):
|
||||||
|
@ -290,7 +290,7 @@ class Graph:
|
||||||
# if u does not exist
|
# if u does not exist
|
||||||
self.graph[v] = [[w, u]]
|
self.graph[v] = [[w, u]]
|
||||||
|
|
||||||
# handels if the input does not exist
|
# handles if the input does not exist
|
||||||
def remove_pair(self, u, v):
|
def remove_pair(self, u, v):
|
||||||
if self.graph.get(u):
|
if self.graph.get(u):
|
||||||
for _ in self.graph[u]:
|
for _ in self.graph[u]:
|
||||||
|
@ -302,7 +302,7 @@ class Graph:
|
||||||
if _[1] == u:
|
if _[1] == u:
|
||||||
self.graph[v].remove(_)
|
self.graph[v].remove(_)
|
||||||
|
|
||||||
# if no destination is meant the defaut value is -1
|
# if no destination is meant the default value is -1
|
||||||
def dfs(self, s=-2, d=-1):
|
def dfs(self, s=-2, d=-1):
|
||||||
if s == d:
|
if s == d:
|
||||||
return []
|
return []
|
||||||
|
@ -341,7 +341,7 @@ class Graph:
|
||||||
if len(stack) == 0:
|
if len(stack) == 0:
|
||||||
return visited
|
return visited
|
||||||
|
|
||||||
# c is the count of nodes you want and if you leave it or pass -1 to the funtion the count
|
# c is the count of nodes you want and if you leave it or pass -1 to the function the count
|
||||||
# will be random from 10 to 10000
|
# will be random from 10 to 10000
|
||||||
def fill_graph_randomly(self, c=-1):
|
def fill_graph_randomly(self, c=-1):
|
||||||
if c == -1:
|
if c == -1:
|
||||||
|
|
|
@ -8,7 +8,7 @@ Also contains a Test class to verify that the generated Hash is same as that
|
||||||
returned by the hashlib library
|
returned by the hashlib library
|
||||||
|
|
||||||
SHA1 hash or SHA1 sum of a string is a crytpographic function which means it is easy
|
SHA1 hash or SHA1 sum of a string is a crytpographic function which means it is easy
|
||||||
to calculate forwards but extemely difficult to calculate backwards. What this means
|
to calculate forwards but extremely difficult to calculate backwards. What this means
|
||||||
is, you can easily calculate the hash of a string, but it is extremely difficult to
|
is, you can easily calculate the hash of a string, but it is extremely difficult to
|
||||||
know the original string if you have its hash. This property is useful to communicate
|
know the original string if you have its hash. This property is useful to communicate
|
||||||
securely, send encrypted messages and is very useful in payment systems, blockchain
|
securely, send encrypted messages and is very useful in payment systems, blockchain
|
||||||
|
@ -139,7 +139,7 @@ class SHA1HashTest(unittest.TestCase):
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash.
|
Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash.
|
||||||
unittest.main() has been commented because we probably dont want to run
|
unittest.main() has been commented because we probably don't want to run
|
||||||
the test each time.
|
the test each time.
|
||||||
"""
|
"""
|
||||||
# unittest.main()
|
# unittest.main()
|
||||||
|
|
|
@ -8,7 +8,7 @@ This module contains classes and functions for doing linear algebra.
|
||||||
|
|
||||||
### class Vector
|
### class Vector
|
||||||
-
|
-
|
||||||
- This class represents a vector of arbitray size and related operations.
|
- This class represents a vector of arbitrary size and related operations.
|
||||||
|
|
||||||
**Overview about the methods:**
|
**Overview about the methods:**
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ def classifier(train_data, train_target, classes, point, k=5):
|
||||||
distances.append((distance, data_point[1]))
|
distances.append((distance, data_point[1]))
|
||||||
# Choosing 'k' points with the least distances.
|
# Choosing 'k' points with the least distances.
|
||||||
votes = [i[1] for i in sorted(distances)[:k]]
|
votes = [i[1] for i in sorted(distances)[:k]]
|
||||||
# Most commonly occuring class among them
|
# Most commonly occurring class among them
|
||||||
# is the class into which the point is classified
|
# is the class into which the point is classified
|
||||||
result = Counter(votes).most_common(1)[0][0]
|
result = Counter(votes).most_common(1)[0][0]
|
||||||
return classes[result]
|
return classes[result]
|
||||||
|
|
|
@ -10,7 +10,7 @@ import numpy as np
|
||||||
even log is used.
|
even log is used.
|
||||||
|
|
||||||
Using log and roots can be perceived as tools for penalizing big
|
Using log and roots can be perceived as tools for penalizing big
|
||||||
erors. However, using appropriate metrics depends on the situations,
|
errors. However, using appropriate metrics depends on the situations,
|
||||||
and types of data
|
and types of data
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
"""
|
"""
|
||||||
Implementation of sequential minimal optimization(SMO) for support vector machines(SVM).
|
Implementation of sequential minimal optimization (SMO) for support vector machines
|
||||||
|
(SVM).
|
||||||
|
|
||||||
Sequential minimal optimization (SMO) is an algorithm for solving the quadratic programming (QP) problem
|
Sequential minimal optimization (SMO) is an algorithm for solving the quadratic
|
||||||
that arises during the training of support vector machines.
|
programming (QP) problem that arises during the training of support vector
|
||||||
|
machines.
|
||||||
It was invented by John Platt in 1998.
|
It was invented by John Platt in 1998.
|
||||||
|
|
||||||
Input:
|
Input:
|
||||||
|
@ -18,7 +20,8 @@ Usage:
|
||||||
|
|
||||||
kernel = Kernel(kernel='poly', degree=3., coef0=1., gamma=0.5)
|
kernel = Kernel(kernel='poly', degree=3., coef0=1., gamma=0.5)
|
||||||
init_alphas = np.zeros(train.shape[0])
|
init_alphas = np.zeros(train.shape[0])
|
||||||
SVM = SmoSVM(train=train, alpha_list=init_alphas, kernel_func=kernel, cost=0.4, b=0.0, tolerance=0.001)
|
SVM = SmoSVM(train=train, alpha_list=init_alphas, kernel_func=kernel, cost=0.4,
|
||||||
|
b=0.0, tolerance=0.001)
|
||||||
SVM.fit()
|
SVM.fit()
|
||||||
predict = SVM.predict(test_samples)
|
predict = SVM.predict(test_samples)
|
||||||
|
|
||||||
|
@ -72,7 +75,7 @@ class SmoSVM:
|
||||||
|
|
||||||
self.choose_alpha = self._choose_alphas()
|
self.choose_alpha = self._choose_alphas()
|
||||||
|
|
||||||
# Calculate alphas using SMO algorithsm
|
# Calculate alphas using SMO algorithm
|
||||||
def fit(self):
|
def fit(self):
|
||||||
K = self._k
|
K = self._k
|
||||||
state = None
|
state = None
|
||||||
|
@ -227,7 +230,7 @@ class SmoSVM:
|
||||||
def _choose_a1(self):
|
def _choose_a1(self):
|
||||||
"""
|
"""
|
||||||
Choose first alpha ;steps:
|
Choose first alpha ;steps:
|
||||||
1:Fisrt loop over all sample
|
1:First loop over all sample
|
||||||
2:Second loop over all non-bound samples till all non-bound samples does not voilate kkt condition.
|
2:Second loop over all non-bound samples till all non-bound samples does not voilate kkt condition.
|
||||||
3:Repeat this two process endlessly,till all samples does not voilate kkt condition samples after first loop.
|
3:Repeat this two process endlessly,till all samples does not voilate kkt condition samples after first loop.
|
||||||
"""
|
"""
|
||||||
|
@ -261,9 +264,11 @@ class SmoSVM:
|
||||||
def _choose_a2(self, i1):
|
def _choose_a2(self, i1):
|
||||||
"""
|
"""
|
||||||
Choose the second alpha by using heuristic algorithm ;steps:
|
Choose the second alpha by using heuristic algorithm ;steps:
|
||||||
1:Choosed alpha2 which get the maximum step size (|E1 - E2|).
|
1: Choose alpha2 which gets the maximum step size (|E1 - E2|).
|
||||||
2:Start in a random point,loop over all non-bound samples till alpha1 and alpha2 are optimized.
|
2: Start in a random point,loop over all non-bound samples till alpha1 and
|
||||||
3:Start in a random point,loop over all samples till alpha1 and alpha2 are optimized.
|
alpha2 are optimized.
|
||||||
|
3: Start in a random point,loop over all samples till alpha1 and alpha2 are
|
||||||
|
optimized.
|
||||||
"""
|
"""
|
||||||
self._unbound = [i for i in self._all_samples if self._is_unbound(i)]
|
self._unbound = [i for i in self._all_samples if self._is_unbound(i)]
|
||||||
|
|
||||||
|
@ -316,7 +321,7 @@ class SmoSVM:
|
||||||
# select the new alpha2 which could get the minimal objectives
|
# select the new alpha2 which could get the minimal objectives
|
||||||
if eta > 0.0:
|
if eta > 0.0:
|
||||||
a2_new_unc = a2 + (y2 * (e1 - e2)) / eta
|
a2_new_unc = a2 + (y2 * (e1 - e2)) / eta
|
||||||
# a2_new has a boundry
|
# a2_new has a boundary
|
||||||
if a2_new_unc >= H:
|
if a2_new_unc >= H:
|
||||||
a2_new = H
|
a2_new = H
|
||||||
elif a2_new_unc <= L:
|
elif a2_new_unc <= L:
|
||||||
|
@ -357,7 +362,7 @@ class SmoSVM:
|
||||||
else:
|
else:
|
||||||
a2_new = a2
|
a2_new = a2
|
||||||
|
|
||||||
# a1_new has a boundry too
|
# a1_new has a boundary too
|
||||||
a1_new = a1 + s * (a2 - a2_new)
|
a1_new = a1 + s * (a2 - a2_new)
|
||||||
if a1_new < 0:
|
if a1_new < 0:
|
||||||
a2_new += s * a1_new
|
a2_new += s * a1_new
|
||||||
|
@ -471,7 +476,7 @@ def test_cancel_data():
|
||||||
data = data.replace({"M": np.float64(1), "B": np.float64(-1)})
|
data = data.replace({"M": np.float64(1), "B": np.float64(-1)})
|
||||||
samples = np.array(data)[:, :]
|
samples = np.array(data)[:, :]
|
||||||
|
|
||||||
# 2: deviding data into train_data data and test_data data
|
# 2: dividing data into train_data data and test_data data
|
||||||
train_data, test_data = samples[:328, :], samples[328:, :]
|
train_data, test_data = samples[:328, :], samples[328:, :]
|
||||||
test_tags, test_samples = test_data[:, 0], test_data[:, 1:]
|
test_tags, test_samples = test_data[:, 0], test_data[:, 1:]
|
||||||
|
|
||||||
|
@ -568,7 +573,7 @@ def plot_partition_boundary(
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
We can not get the optimum w of our kernel svm model which is different from linear svm.
|
We can not get the optimum w of our kernel svm model which is different from linear svm.
|
||||||
For this reason, we generate randomly destributed points with high desity and prediced values of these points are
|
For this reason, we generate randomly distributed points with high desity and prediced values of these points are
|
||||||
calculated by using our tained model. Then we could use this prediced values to draw contour map.
|
calculated by using our tained model. Then we could use this prediced values to draw contour map.
|
||||||
And this contour map can represent svm's partition boundary.
|
And this contour map can represent svm's partition boundary.
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ def Linearsvc(train_x, train_y):
|
||||||
|
|
||||||
def SVC(train_x, train_y):
|
def SVC(train_x, train_y):
|
||||||
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=None)
|
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=None)
|
||||||
# various parameters like "kernal","gamma","C" can effectively tuned for a given machine learning model.
|
# various parameters like "kernel","gamma","C" can effectively tuned for a given machine learning model.
|
||||||
SVC = svm.SVC(gamma="auto")
|
SVC = svm.SVC(gamma="auto")
|
||||||
SVC.fit(train_x, train_y)
|
SVC.fit(train_x, train_y)
|
||||||
return SVC
|
return SVC
|
||||||
|
|
|
@ -12,7 +12,7 @@ https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/
|
||||||
|
|
||||||
class Matrix:
|
class Matrix:
|
||||||
def __init__(self, arg):
|
def __init__(self, arg):
|
||||||
if isinstance(arg, list): # Initialzes a matrix identical to the one provided.
|
if isinstance(arg, list): # Initializes a matrix identical to the one provided.
|
||||||
self.t = arg
|
self.t = arg
|
||||||
self.n = len(arg)
|
self.n = len(arg)
|
||||||
else: # Initializes a square matrix of the given size and set the values to zero.
|
else: # Initializes a square matrix of the given size and set the values to zero.
|
||||||
|
@ -50,7 +50,7 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
||||||
|
|
||||||
|
|
||||||
def simple_fibonacci(n, f1, f2):
|
def simple_fibonacci(n, f1, f2):
|
||||||
# Trival Cases
|
# Trivial Cases
|
||||||
if n == 1:
|
if n == 1:
|
||||||
return f1
|
return f1
|
||||||
elif n == 2:
|
elif n == 2:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function
|
References: https://en.wikipedia.org/wiki/M%C3%B6bius_function
|
||||||
References: wikipedia:square free number
|
References: wikipedia:square free number
|
||||||
python/black : True
|
python/black : True
|
||||||
flake8 : True
|
flake8 : True
|
||||||
|
|
|
@ -4,18 +4,18 @@ Sieve of Eratosthenes
|
||||||
Input : n =10
|
Input : n =10
|
||||||
Output : 2 3 5 7
|
Output : 2 3 5 7
|
||||||
|
|
||||||
Input : n = 20
|
Input : n = 20
|
||||||
Output: 2 3 5 7 11 13 17 19
|
Output: 2 3 5 7 11 13 17 19
|
||||||
|
|
||||||
you can read in detail about this at
|
you can read in detail about this at
|
||||||
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def prime_sieve_eratosthenes(num):
|
def prime_sieve_eratosthenes(num):
|
||||||
"""
|
"""
|
||||||
print the prime numbers upto n
|
print the prime numbers up to n
|
||||||
|
|
||||||
>>> prime_sieve_eratosthenes(10)
|
>>> prime_sieve_eratosthenes(10)
|
||||||
2 3 5 7
|
2 3 5 7
|
||||||
>>> prime_sieve_eratosthenes(20)
|
>>> prime_sieve_eratosthenes(20)
|
||||||
|
@ -26,7 +26,7 @@ def prime_sieve_eratosthenes(num):
|
||||||
p = 2
|
p = 2
|
||||||
|
|
||||||
while p * p <= num:
|
while p * p <= num:
|
||||||
if primes[p] == True:
|
if primes[p]:
|
||||||
for i in range(p * p, num + 1, p):
|
for i in range(p * p, num + 1, p):
|
||||||
primes[i] = False
|
primes[i] = False
|
||||||
p += 1
|
p += 1
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Numerical integration or quadrature for a smooth function f with known values at x_i
|
Numerical integration or quadrature for a smooth function f with known values at x_i
|
||||||
|
|
||||||
This method is the classical approch of suming 'Equally Spaced Abscissas'
|
This method is the classical approach of suming 'Equally Spaced Abscissas'
|
||||||
|
|
||||||
method 2:
|
method 2:
|
||||||
"Simpson Rule"
|
"Simpson Rule"
|
||||||
|
|
|
@ -22,7 +22,7 @@ def square_root_iterative(
|
||||||
a: float, max_iter: int = 9999, tolerance: float = 0.00000000000001
|
a: float, max_iter: int = 9999, tolerance: float = 0.00000000000001
|
||||||
) -> float:
|
) -> float:
|
||||||
"""
|
"""
|
||||||
Sqaure root is aproximated using Newtons method.
|
Square root is aproximated using Newtons method.
|
||||||
https://en.wikipedia.org/wiki/Newton%27s_method
|
https://en.wikipedia.org/wiki/Newton%27s_method
|
||||||
|
|
||||||
>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 for i in range(0, 500))
|
>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 for i in range(0, 500))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Numerical integration or quadrature for a smooth function f with known values at x_i
|
Numerical integration or quadrature for a smooth function f with known values at x_i
|
||||||
|
|
||||||
This method is the classical approch of suming 'Equally Spaced Abscissas'
|
This method is the classical approach of suming 'Equally Spaced Abscissas'
|
||||||
|
|
||||||
method 1:
|
method 1:
|
||||||
"extended trapezoidal rule"
|
"extended trapezoidal rule"
|
||||||
|
|
|
@ -31,17 +31,17 @@ def zeller(date_input: str) -> str:
|
||||||
...
|
...
|
||||||
ValueError: invalid literal for int() with base 10: '.4'
|
ValueError: invalid literal for int() with base 10: '.4'
|
||||||
|
|
||||||
Validate second seperator:
|
Validate second separator:
|
||||||
>>> zeller('01-31*2010')
|
>>> zeller('01-31*2010')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: Date seperator must be '-' or '/'
|
ValueError: Date separator must be '-' or '/'
|
||||||
|
|
||||||
Validate first seperator:
|
Validate first separator:
|
||||||
>>> zeller('01^31-2010')
|
>>> zeller('01^31-2010')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: Date seperator must be '-' or '/'
|
ValueError: Date separator must be '-' or '/'
|
||||||
|
|
||||||
Validate out of range year:
|
Validate out of range year:
|
||||||
>>> zeller('01-31-8999')
|
>>> zeller('01-31-8999')
|
||||||
|
@ -55,7 +55,7 @@ def zeller(date_input: str) -> str:
|
||||||
...
|
...
|
||||||
TypeError: zeller() missing 1 required positional argument: 'date_input'
|
TypeError: zeller() missing 1 required positional argument: 'date_input'
|
||||||
|
|
||||||
Test length fo date_input:
|
Test length of date_input:
|
||||||
>>> zeller('')
|
>>> zeller('')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
@ -92,7 +92,7 @@ def zeller(date_input: str) -> str:
|
||||||
sep_1: str = date_input[2]
|
sep_1: str = date_input[2]
|
||||||
# Validate
|
# Validate
|
||||||
if sep_1 not in ["-", "/"]:
|
if sep_1 not in ["-", "/"]:
|
||||||
raise ValueError("Date seperator must be '-' or '/'")
|
raise ValueError("Date separator must be '-' or '/'")
|
||||||
|
|
||||||
# Get day
|
# Get day
|
||||||
d: int = int(date_input[3] + date_input[4])
|
d: int = int(date_input[3] + date_input[4])
|
||||||
|
@ -100,11 +100,11 @@ def zeller(date_input: str) -> str:
|
||||||
if not 0 < d < 32:
|
if not 0 < d < 32:
|
||||||
raise ValueError("Date must be between 1 - 31")
|
raise ValueError("Date must be between 1 - 31")
|
||||||
|
|
||||||
# Get second seperator
|
# Get second separator
|
||||||
sep_2: str = date_input[5]
|
sep_2: str = date_input[5]
|
||||||
# Validate
|
# Validate
|
||||||
if sep_2 not in ["-", "/"]:
|
if sep_2 not in ["-", "/"]:
|
||||||
raise ValueError("Date seperator must be '-' or '/'")
|
raise ValueError("Date separator must be '-' or '/'")
|
||||||
|
|
||||||
# Get year
|
# Get year
|
||||||
y: int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9])
|
y: int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9])
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# An OOP aproach to representing and manipulating matrices
|
# An OOP approach to representing and manipulating matrices
|
||||||
|
|
||||||
|
|
||||||
class Matrix:
|
class Matrix:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
This program print the matix in spiral form.
|
This program print the matrix in spiral form.
|
||||||
This problem has been solved through recursive way.
|
This problem has been solved through recursive way.
|
||||||
|
|
||||||
Matrix must satisfy below conditions
|
Matrix must satisfy below conditions
|
||||||
|
|
|
@ -35,7 +35,7 @@ def mincut(graph, source, sink):
|
||||||
parent = [-1] * (len(graph))
|
parent = [-1] * (len(graph))
|
||||||
max_flow = 0
|
max_flow = 0
|
||||||
res = []
|
res = []
|
||||||
temp = [i[:] for i in graph] # Record orignial cut, copy.
|
temp = [i[:] for i in graph] # Record original cut, copy.
|
||||||
while BFS(graph, source, sink, parent):
|
while BFS(graph, source, sink, parent):
|
||||||
path_flow = float("Inf")
|
path_flow = float("Inf")
|
||||||
s = sink
|
s = sink
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
"""
|
"""
|
||||||
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
|
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
|
||||||
Name - - CNN - Convolution Neural Network For Photo Recognizing
|
Name - - CNN - Convolution Neural Network For Photo Recognizing
|
||||||
Goal - - Recognize Handing Writting Word Photo
|
Goal - - Recognize Handing Writing Word Photo
|
||||||
Detail:Total 5 layers neural network
|
Detail:Total 5 layers neural network
|
||||||
* Convolution layer
|
* Convolution layer
|
||||||
* Pooling layer
|
* Pooling layer
|
||||||
* Input layer layer of BP
|
* Input layer layer of BP
|
||||||
* Hiden layer of BP
|
* Hidden layer of BP
|
||||||
* Output layer of BP
|
* Output layer of BP
|
||||||
Author: Stephen Lee
|
Author: Stephen Lee
|
||||||
Github: 245885195@qq.com
|
Github: 245885195@qq.com
|
||||||
|
@ -116,7 +116,7 @@ class CNN:
|
||||||
i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
|
i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
|
||||||
]
|
]
|
||||||
data_focus.append(focus)
|
data_focus.append(focus)
|
||||||
# caculate the feature map of every single kernel, and saved as list of matrix
|
# calculate the feature map of every single kernel, and saved as list of matrix
|
||||||
data_featuremap = []
|
data_featuremap = []
|
||||||
Size_FeatureMap = int((size_data - size_conv) / conv_step + 1)
|
Size_FeatureMap = int((size_data - size_conv) / conv_step + 1)
|
||||||
for i_map in range(num_conv):
|
for i_map in range(num_conv):
|
||||||
|
@ -163,12 +163,12 @@ class CNN:
|
||||||
featuremap_pooled.append(map_pooled)
|
featuremap_pooled.append(map_pooled)
|
||||||
return featuremap_pooled
|
return featuremap_pooled
|
||||||
|
|
||||||
def _expand(self, datas):
|
def _expand(self, data):
|
||||||
# expanding three dimension data to one dimension list
|
# expanding three dimension data to one dimension list
|
||||||
data_expanded = []
|
data_expanded = []
|
||||||
for i in range(len(datas)):
|
for i in range(len(data)):
|
||||||
shapes = np.shape(datas[i])
|
shapes = np.shape(data[i])
|
||||||
data_listed = datas[i].reshape(1, shapes[0] * shapes[1])
|
data_listed = data[i].reshape(1, shapes[0] * shapes[1])
|
||||||
data_listed = data_listed.getA().tolist()[0]
|
data_listed = data_listed.getA().tolist()[0]
|
||||||
data_expanded.extend(data_listed)
|
data_expanded.extend(data_listed)
|
||||||
data_expanded = np.asarray(data_expanded)
|
data_expanded = np.asarray(data_expanded)
|
||||||
|
@ -185,7 +185,7 @@ class CNN:
|
||||||
self, out_map, pd_pool, num_map, size_map, size_pooling
|
self, out_map, pd_pool, num_map, size_map, size_pooling
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
calcluate the gradient from the data slice of pool layer
|
calculate the gradient from the data slice of pool layer
|
||||||
pd_pool: list of matrix
|
pd_pool: list of matrix
|
||||||
out_map: the shape of data slice(size_map*size_map)
|
out_map: the shape of data slice(size_map*size_map)
|
||||||
return: pd_all: list of matrix, [num, size_map, size_map]
|
return: pd_all: list of matrix, [num, size_map, size_map]
|
||||||
|
@ -217,7 +217,7 @@ class CNN:
|
||||||
all_mse = []
|
all_mse = []
|
||||||
mse = 10000
|
mse = 10000
|
||||||
while rp < n_repeat and mse >= error_accuracy:
|
while rp < n_repeat and mse >= error_accuracy:
|
||||||
alle = 0
|
error_count = 0
|
||||||
print("-------------Learning Time %d--------------" % rp)
|
print("-------------Learning Time %d--------------" % rp)
|
||||||
for p in range(len(datas_train)):
|
for p in range(len(datas_train)):
|
||||||
# print('------------Learning Image: %d--------------'%p)
|
# print('------------Learning Image: %d--------------'%p)
|
||||||
|
@ -246,7 +246,7 @@ class CNN:
|
||||||
bp_out3 = self.sig(bp_net_k)
|
bp_out3 = self.sig(bp_net_k)
|
||||||
|
|
||||||
# --------------Model Leaning ------------------------
|
# --------------Model Leaning ------------------------
|
||||||
# calcluate error and gradient---------------
|
# calculate error and gradient---------------
|
||||||
pd_k_all = np.multiply(
|
pd_k_all = np.multiply(
|
||||||
(data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3))
|
(data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3))
|
||||||
)
|
)
|
||||||
|
@ -285,11 +285,11 @@ class CNN:
|
||||||
self.thre_bp2 = self.thre_bp2 - pd_j_all * self.rate_thre
|
self.thre_bp2 = self.thre_bp2 - pd_j_all * self.rate_thre
|
||||||
# calculate the sum error of all single image
|
# calculate the sum error of all single image
|
||||||
errors = np.sum(abs(data_teach - bp_out3))
|
errors = np.sum(abs(data_teach - bp_out3))
|
||||||
alle = alle + errors
|
error_count += errors
|
||||||
# print(' ----Teach ',data_teach)
|
# print(' ----Teach ',data_teach)
|
||||||
# print(' ----BP_output ',bp_out3)
|
# print(' ----BP_output ',bp_out3)
|
||||||
rp = rp + 1
|
rp = rp + 1
|
||||||
mse = alle / patterns
|
mse = error_count / patterns
|
||||||
all_mse.append(mse)
|
all_mse.append(mse)
|
||||||
|
|
||||||
def draw_error():
|
def draw_error():
|
||||||
|
|
|
@ -76,11 +76,11 @@ class Perceptron:
|
||||||
has_misclassified = True
|
has_misclassified = True
|
||||||
# print('Epoch: \n',epoch_count)
|
# print('Epoch: \n',epoch_count)
|
||||||
epoch_count = epoch_count + 1
|
epoch_count = epoch_count + 1
|
||||||
# if you want controle the epoch or just by erro
|
# if you want control the epoch or just by error
|
||||||
if not has_misclassified:
|
if not has_misclassified:
|
||||||
print(("\nEpoch:\n", epoch_count))
|
print(("\nEpoch:\n", epoch_count))
|
||||||
print("------------------------\n")
|
print("------------------------\n")
|
||||||
# if epoch_count > self.epoch_number or not erro:
|
# if epoch_count > self.epoch_number or not error:
|
||||||
break
|
break
|
||||||
|
|
||||||
def sort(self, sample) -> None:
|
def sort(self, sample) -> None:
|
||||||
|
|
|
@ -13,7 +13,7 @@ class LinearCongruentialGenerator:
|
||||||
These parameters are saved and used when nextNumber() is called.
|
These parameters are saved and used when nextNumber() is called.
|
||||||
|
|
||||||
modulo is the largest number that can be generated (exclusive). The most
|
modulo is the largest number that can be generated (exclusive). The most
|
||||||
efficent values are powers of 2. 2^32 is a common value.
|
efficient values are powers of 2. 2^32 is a common value.
|
||||||
"""
|
"""
|
||||||
self.multiplier = multiplier
|
self.multiplier = multiplier
|
||||||
self.increment = increment
|
self.increment = increment
|
||||||
|
|
|
@ -88,7 +88,7 @@ def sieveEr(N):
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2"
|
assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2"
|
||||||
|
|
||||||
# beginList: conatins all natural numbers from 2 upt to N
|
# beginList: contains all natural numbers from 2 up to N
|
||||||
beginList = [x for x in range(2, N + 1)]
|
beginList = [x for x in range(2, N + 1)]
|
||||||
|
|
||||||
ans = [] # this list will be returns.
|
ans = [] # this list will be returns.
|
||||||
|
@ -480,8 +480,8 @@ def getPrimesBetween(pNumber1, pNumber2):
|
||||||
"""
|
"""
|
||||||
input: prime numbers 'pNumber1' and 'pNumber2'
|
input: prime numbers 'pNumber1' and 'pNumber2'
|
||||||
pNumber1 < pNumber2
|
pNumber1 < pNumber2
|
||||||
returns a list of all prime numbers between 'pNumber1' (exclusiv)
|
returns a list of all prime numbers between 'pNumber1' (exclusive)
|
||||||
and 'pNumber2' (exclusiv)
|
and 'pNumber2' (exclusive)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
|
|
|
@ -19,7 +19,7 @@ def solution(n):
|
||||||
>>> solution(40000)
|
>>> solution(40000)
|
||||||
39893
|
39893
|
||||||
"""
|
"""
|
||||||
# fetchs the next number
|
# fetches the next number
|
||||||
for number in range(n - 1, 10000, -1):
|
for number in range(n - 1, 10000, -1):
|
||||||
|
|
||||||
# converts number into string.
|
# converts number into string.
|
||||||
|
|
|
@ -30,5 +30,5 @@ def digitsum(s: str) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
count = sum(digitsum(str(i)) for i in range(1000,1000000))
|
count = sum(digitsum(str(i)) for i in range(1000, 1000000))
|
||||||
print(count) # --> 443839
|
print(count) # --> 443839
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
Sum of digits sequence
|
Sum of digits sequence
|
||||||
Problem 551
|
Problem 551
|
||||||
|
|
||||||
Let a(0), a(1),... be an interger sequence defined by:
|
Let a(0), a(1),... be an integer sequence defined by:
|
||||||
a(0) = 1
|
a(0) = 1
|
||||||
for n >= 1, a(n) is the sum of the digits of all preceding terms
|
for n >= 1, a(n) is the sum of the digits of all preceding terms
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ def next_term(a_i, k, i, n):
|
||||||
k -- k when terms are written in the from a(i) = b*10^k + c.
|
k -- k when terms are written in the from a(i) = b*10^k + c.
|
||||||
Term are calulcated until c > 10^k or the n-th term is reached.
|
Term are calulcated until c > 10^k or the n-th term is reached.
|
||||||
i -- position along the sequence
|
i -- position along the sequence
|
||||||
n -- term to caluclate up to if k is large enough
|
n -- term to calculate up to if k is large enough
|
||||||
|
|
||||||
Return: a tuple of difference between ending term and starting term, and
|
Return: a tuple of difference between ending term and starting term, and
|
||||||
the number of terms calculated. ex. if starting term is a_0=1, and
|
the number of terms calculated. ex. if starting term is a_0=1, and
|
||||||
|
|
|
@ -87,7 +87,7 @@ def hill_climbing(
|
||||||
"""
|
"""
|
||||||
implementation of the hill climbling algorithm. We start with a given state, find
|
implementation of the hill climbling algorithm. We start with a given state, find
|
||||||
all its neighbors, move towards the neighbor which provides the maximum (or
|
all its neighbors, move towards the neighbor which provides the maximum (or
|
||||||
minimum) change. We keep doing this untill we are at a state where we do not
|
minimum) change. We keep doing this until we are at a state where we do not
|
||||||
have any neighbors which can improve the solution.
|
have any neighbors which can improve the solution.
|
||||||
Args:
|
Args:
|
||||||
search_prob: The search state at the start.
|
search_prob: The search state at the start.
|
||||||
|
|
|
@ -15,7 +15,7 @@ def interpolation_search(sorted_collection, item):
|
||||||
right = len(sorted_collection) - 1
|
right = len(sorted_collection) - 1
|
||||||
|
|
||||||
while left <= right:
|
while left <= right:
|
||||||
# avoid devided by 0 during interpolation
|
# avoid divided by 0 during interpolation
|
||||||
if sorted_collection[left] == sorted_collection[right]:
|
if sorted_collection[left] == sorted_collection[right]:
|
||||||
if sorted_collection[left] == item:
|
if sorted_collection[left] == item:
|
||||||
return left
|
return left
|
||||||
|
@ -59,7 +59,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
|
||||||
:return: index of found item or None if item is not found
|
:return: index of found item or None if item is not found
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# avoid devided by 0 during interpolation
|
# avoid divided by 0 during interpolation
|
||||||
if sorted_collection[left] == sorted_collection[right]:
|
if sorted_collection[left] == sorted_collection[right]:
|
||||||
if sorted_collection[left] == item:
|
if sorted_collection[left] == item:
|
||||||
return left
|
return left
|
||||||
|
|
|
@ -20,7 +20,7 @@ def jump_search(arr, x):
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
|
if __name__ == "__main__":
|
||||||
x = 55
|
arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
|
||||||
index = jump_search(arr, x)
|
x = 55
|
||||||
print("\nNumber " + str(x) + " is at index " + str(index))
|
print(f"Number {x} is at index {jump_search(arr, x)}")
|
||||||
|
|
|
@ -62,7 +62,7 @@ def simulated_annealing(
|
||||||
continue # neighbor outside our bounds
|
continue # neighbor outside our bounds
|
||||||
|
|
||||||
if not find_max:
|
if not find_max:
|
||||||
change = change * -1 # incase we are finding minimum
|
change = change * -1 # in case we are finding minimum
|
||||||
if change > 0: # improves the solution
|
if change > 0: # improves the solution
|
||||||
next_state = picked_neighbor
|
next_state = picked_neighbor
|
||||||
else:
|
else:
|
||||||
|
@ -73,10 +73,8 @@ def simulated_annealing(
|
||||||
next_state = picked_neighbor
|
next_state = picked_neighbor
|
||||||
current_temp = current_temp - (current_temp * rate_of_decrease)
|
current_temp = current_temp - (current_temp * rate_of_decrease)
|
||||||
|
|
||||||
if (
|
if current_temp < threshold_temp or next_state is None:
|
||||||
current_temp < threshold_temp or next_state is None
|
# temperature below threshold, or could not find a suitaable neighbor
|
||||||
): # temperature below threshold, or
|
|
||||||
# couldnt find a suitaable neighbor
|
|
||||||
search_end = True
|
search_end = True
|
||||||
else:
|
else:
|
||||||
current_state = next_state
|
current_state = next_state
|
||||||
|
|
|
@ -188,7 +188,7 @@ def tabu_search(
|
||||||
and the cost (distance) for each neighbor.
|
and the cost (distance) for each neighbor.
|
||||||
:param iters: The number of iterations that Tabu search will execute.
|
:param iters: The number of iterations that Tabu search will execute.
|
||||||
:param size: The size of Tabu List.
|
:param size: The size of Tabu List.
|
||||||
:return best_solution_ever: The solution with the lowest distance that occured during the execution of Tabu search.
|
:return best_solution_ever: The solution with the lowest distance that occurred during the execution of Tabu search.
|
||||||
:return best_cost: The total distance that Travelling Salesman will travel, if he follows the path in best_solution
|
:return best_cost: The total distance that Travelling Salesman will travel, if he follows the path in best_solution
|
||||||
ever.
|
ever.
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ def bitonicMerge(a, low, cnt, dire):
|
||||||
bitonicMerge(a, low, k, dire)
|
bitonicMerge(a, low, k, dire)
|
||||||
bitonicMerge(a, low + k, k, dire)
|
bitonicMerge(a, low + k, k, dire)
|
||||||
|
|
||||||
# This funcion first produces a bitonic sequence by recursively
|
# This function first produces a bitonic sequence by recursively
|
||||||
|
|
||||||
|
|
||||||
# sorting its two halves in opposite sorting orders, and then
|
# sorting its two halves in opposite sorting orders, and then
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
def double_sort(lst):
|
def double_sort(lst):
|
||||||
"""this sorting algorithm sorts an array using the principle of bubble sort ,
|
"""this sorting algorithm sorts an array using the principle of bubble sort,
|
||||||
but does it both from left to right and right to left ,
|
but does it both from left to right and right to left,
|
||||||
hence i decided to call it "double sort"
|
hence i decided to call it "double sort"
|
||||||
:param collection: mutable ordered sequence of elements
|
:param collection: mutable ordered sequence of elements
|
||||||
:return: the same collection in ascending order
|
:return: the same collection in ascending order
|
||||||
|
@ -17,7 +17,7 @@ def double_sort(lst):
|
||||||
no_of_elements = len(lst)
|
no_of_elements = len(lst)
|
||||||
for i in range(
|
for i in range(
|
||||||
0, int(((no_of_elements - 1) / 2) + 1)
|
0, int(((no_of_elements - 1) / 2) + 1)
|
||||||
): # we dont need to traverse to end of list as
|
): # we don't need to traverse to end of list as
|
||||||
for j in range(0, no_of_elements - 1):
|
for j in range(0, no_of_elements - 1):
|
||||||
if (
|
if (
|
||||||
lst[j + 1] < lst[j]
|
lst[j + 1] < lst[j]
|
||||||
|
|
|
@ -7,7 +7,7 @@ def pigeonhole_sort(a):
|
||||||
"""
|
"""
|
||||||
>>> a = [8, 3, 2, 7, 4, 6, 8]
|
>>> a = [8, 3, 2, 7, 4, 6, 8]
|
||||||
>>> b = sorted(a) # a nondestructive sort
|
>>> b = sorted(a) # a nondestructive sort
|
||||||
>>> pigeonhole_sort(a) # a distructive sort
|
>>> pigeonhole_sort(a) # a destructive sort
|
||||||
>>> a == b
|
>>> a == b
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -69,7 +69,7 @@ def insert_next(collection: List, index: int):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
numbers = input("Enter integers seperated by spaces: ")
|
numbers = input("Enter integers separated by spaces: ")
|
||||||
numbers = [int(num) for num in numbers.split()]
|
numbers = [int(num) for num in numbers.split()]
|
||||||
rec_insertion_sort(numbers, len(numbers))
|
rec_insertion_sort(numbers, len(numbers))
|
||||||
print(numbers)
|
print(numbers)
|
||||||
|
|
|
@ -67,7 +67,7 @@ class Automaton:
|
||||||
>>> A.search_in("whatever, err ... , wherever")
|
>>> A.search_in("whatever, err ... , wherever")
|
||||||
{'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]}
|
{'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]}
|
||||||
"""
|
"""
|
||||||
result = dict() # returns a dict with keywords and list of its occurences
|
result = dict() # returns a dict with keywords and list of its occurrences
|
||||||
current_state = 0
|
current_state = 0
|
||||||
for i in range(len(string)):
|
for i in range(len(string)):
|
||||||
while (
|
while (
|
||||||
|
|
|
@ -27,7 +27,7 @@ class BoyerMooreSearch:
|
||||||
def match_in_pattern(self, char):
|
def match_in_pattern(self, char):
|
||||||
""" finds the index of char in pattern in reverse order
|
""" finds the index of char in pattern in reverse order
|
||||||
|
|
||||||
Paremeters :
|
Parameters :
|
||||||
char (chr): character to be searched
|
char (chr): character to be searched
|
||||||
|
|
||||||
Returns :
|
Returns :
|
||||||
|
@ -43,12 +43,12 @@ class BoyerMooreSearch:
|
||||||
def mismatch_in_text(self, currentPos):
|
def mismatch_in_text(self, currentPos):
|
||||||
""" finds the index of mis-matched character in text when compared with pattern from last
|
""" finds the index of mis-matched character in text when compared with pattern from last
|
||||||
|
|
||||||
Paremeters :
|
Parameters :
|
||||||
currentPos (int): current index position of text
|
currentPos (int): current index position of text
|
||||||
|
|
||||||
Returns :
|
Returns :
|
||||||
i (int): index of mismatched char from last in text
|
i (int): index of mismatched char from last in text
|
||||||
-1 (int): if there is no mis-match between pattern and text block
|
-1 (int): if there is no mismatch between pattern and text block
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for i in range(self.patLen - 1, -1, -1):
|
for i in range(self.patLen - 1, -1, -1):
|
||||||
|
|
|
@ -13,12 +13,13 @@ def palindromic_string(input_string):
|
||||||
"""
|
"""
|
||||||
Manacher’s algorithm which finds Longest Palindromic Substring in linear time.
|
Manacher’s algorithm which finds Longest Palindromic Substring in linear time.
|
||||||
|
|
||||||
1. first this conver input_string("xyx") into new_string("x|y|x") where odd positions are actual input
|
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
||||||
characters.
|
positions are actual input characters.
|
||||||
2. for each character in new_string it find corresponding length and store,
|
2. for each character in new_string it find corresponding length and store,
|
||||||
a. max_length
|
a. max_length
|
||||||
b. max_length's center
|
b. max_length's center
|
||||||
3. return output_string from center - max_length to center + max_length and remove all "|"
|
3. return output_string from center - max_length to center + max_length and remove
|
||||||
|
all "|"
|
||||||
"""
|
"""
|
||||||
max_length = 0
|
max_length = 0
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ def palindromic_string(input_string):
|
||||||
# for each character in new_string find corresponding palindromic string
|
# for each character in new_string find corresponding palindromic string
|
||||||
for i in range(len(new_input_string)):
|
for i in range(len(new_input_string)):
|
||||||
|
|
||||||
# get palindromic length from ith position
|
# get palindromic length from i-th position
|
||||||
length = palindromic_length(i, 1, new_input_string)
|
length = palindromic_length(i, 1, new_input_string)
|
||||||
|
|
||||||
# update max_length and start position
|
# update max_length and start position
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
def split(string: str, seperator: str = " ") -> list:
|
def split(string: str, separator: str = " ") -> list:
|
||||||
"""
|
"""
|
||||||
Will split the string up into all the values seperated by the seperator (defaults to spaces)
|
Will split the string up into all the values separated by the separator (defaults to spaces)
|
||||||
|
|
||||||
>>> split("apple#banana#cherry#orange",seperator='#')
|
>>> split("apple#banana#cherry#orange",separator='#')
|
||||||
['apple', 'banana', 'cherry', 'orange']
|
['apple', 'banana', 'cherry', 'orange']
|
||||||
|
|
||||||
>>> split("Hello there")
|
>>> split("Hello there")
|
||||||
['Hello', 'there']
|
['Hello', 'there']
|
||||||
|
|
||||||
>>> split("11/22/63",seperator = '/')
|
>>> split("11/22/63",separator = '/')
|
||||||
['11', '22', '63']
|
['11', '22', '63']
|
||||||
|
|
||||||
>>> split("12:43:39",seperator = ":")
|
>>> split("12:43:39",separator = ":")
|
||||||
['12', '43', '39']
|
['12', '43', '39']
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ def split(string: str, seperator: str = " ") -> list:
|
||||||
|
|
||||||
last_index = 0
|
last_index = 0
|
||||||
for index, char in enumerate(string):
|
for index, char in enumerate(string):
|
||||||
if char == seperator:
|
if char == separator:
|
||||||
split_words.append(string[last_index:index])
|
split_words.append(string[last_index:index])
|
||||||
last_index = index + 1
|
last_index = index + 1
|
||||||
elif index + 1 == len(string):
|
elif index + 1 == len(string):
|
||||||
|
|
|
@ -11,11 +11,11 @@ def word_occurence(sentence: str) -> dict:
|
||||||
... in Counter(SENTENCE.split()).items())
|
... in Counter(SENTENCE.split()).items())
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
occurence = defaultdict(int)
|
occurrence = defaultdict(int)
|
||||||
# Creating a dictionary containing count of each word
|
# Creating a dictionary containing count of each word
|
||||||
for word in sentence.split(" "):
|
for word in sentence.split(" "):
|
||||||
occurence[word] += 1
|
occurrence[word] += 1
|
||||||
return occurence
|
return occurrence
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
Loading…
Reference in New Issue
Block a user