From 627be8a44a2baafb321089e7e5ae5a2a21ddc4b0 Mon Sep 17 00:00:00 2001 From: Himanshu Jain Date: Mon, 28 Oct 2019 12:58:20 +0530 Subject: [PATCH 1/9] Factors of a number --- maths/factors.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 maths/factors.py diff --git a/maths/factors.py b/maths/factors.py new file mode 100644 index 000000000000..58e18711aac3 --- /dev/null +++ b/maths/factors.py @@ -0,0 +1,10 @@ +#factors of a number +num=int(input("Enter a number to find factors=")) +b=[] +c=0 +for i in range(1,num+1): + if num%i==0: + b.append(i) + c+=1 +print("The factors of",num,"are=",b) +print('Number of factors of',num,'are=',c) From 2c4153a255dc9bb35956174809e4d20983570595 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 22:57:56 +0100 Subject: [PATCH 2/9] Update factors.py --- maths/factors.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/maths/factors.py b/maths/factors.py index 58e18711aac3..e2fdc4063a13 100644 --- a/maths/factors.py +++ b/maths/factors.py @@ -1,10 +1,18 @@ -#factors of a number -num=int(input("Enter a number to find factors=")) -b=[] -c=0 -for i in range(1,num+1): - if num%i==0: - b.append(i) - c+=1 -print("The factors of",num,"are=",b) -print('Number of factors of',num,'are=',c) +def factors_of_a_number(num: int) -> list: + """ + >>> factors_of_a_number(1) + [1] + >>> factors_of_a_number(5) + [1, 5] + >>> factors_of_a_number(24) + [1, 2, 3, 4, 6, 8, 12, 24] + >>> factors_of_a_number(-24) + [] + """ + return [i for i in range(1, num + 1) if num % i == 0] + + +if __name__ == "__main__": + num = int(input("Enter a number to find its factors: ")) + factors = factors_of_a_number(num) + print(f"{num} has {len(factors)} factors: {', '.join(str(f) for f in factors)}") From 1968a6b70b58fa5a3290cd0f8e9128fd80a5b02f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:03:00 +0100 Subject: [PATCH 3/9] Fix mypy issue in basic_maths.py --- maths/basic_maths.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 34ffd1031527..31c3c153014a 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -64,10 +64,8 @@ def sum_of_divisors(n): def euler_phi(n): """Calculte Euler's Phi Function.""" - l = prime_factors(n) - l = set(l) s = n - for x in l: + for x in set(prime_factors(n)): s *= (x - 1) / x return s From 4e4c4b8efc5f5cb52f9b1e329a76cd7cc5df64b4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:13:13 +0100 Subject: [PATCH 4/9] Fix mypy error in perceptron.py --- neural_network/perceptron.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 26d48506254b..3610dd2ab227 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -95,8 +95,7 @@ def sort(self, sample) -> None: ... >>> perceptron.sort([-0.6508, 0.1097, 4.0009]) # doctest: +ELLIPSIS ('Sample: ', ...) - classification: P1 - + classification: P... """ if len(self.sample) == 0: raise AttributeError("Sample data can not be empty") From 72df574a9c6b714ead49cf64f5e1833f6ee08043 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:19:21 +0100 Subject: [PATCH 5/9] def primes(max: int) -> List[int]: --- maths/prime_numbers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/prime_numbers.py b/maths/prime_numbers.py index aafbebe07be9..a29a95ea2280 100644 --- a/maths/prime_numbers.py +++ b/maths/prime_numbers.py @@ -1,9 +1,9 @@ -"""Prime numbers calculation.""" +from typing import List -def primes(max: int) -> int: +def primes(max: int) -> List[int]: """ - Return a list of all primes up to max. + Return a list of all primes numbers up to max. >>> primes(10) [2, 3, 5, 7] >>> primes(11) From 422ebc2d0ff61df09eb36f8127281c1e6ff3af5b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:37:22 +0100 Subject: [PATCH 6/9] Update binomial_heap.py --- data_structures/heap/binomial_heap.py | 177 +++++++++++++------------- 1 file changed, 86 insertions(+), 91 deletions(-) diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index e1a005487e34..1588f4529120 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -1,7 +1,6 @@ """ - Binomial Heap - - Reference: Advanced Data Structures, Peter Brass +Binomial Heap +Reference: Advanced Data Structures, Peter Brass """ @@ -10,7 +9,7 @@ class Node: Node in a doubly-linked binomial tree, containing: - value - size of left subtree - - link to left, right and parent nodes + - link to left, right and parent nodes """ def __init__(self, val): @@ -23,8 +22,8 @@ def __init__(self, val): def mergeTrees(self, other): """ - In-place merge of two binomial trees of equal size. - Returns the root of the resulting tree + In-place merge of two binomial trees of equal size. + Returns the root of the resulting tree """ assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks" @@ -47,83 +46,79 @@ def mergeTrees(self, other): class BinomialHeap: - """ - Min-oriented priority queue implemented with the Binomial Heap data - structure implemented with the BinomialHeap class. It supports: - + r""" + Min-oriented priority queue implemented with the Binomial Heap data + structure implemented with the BinomialHeap class. It supports: - Insert element in a heap with n elemnts: Guaranteed logn, amoratized 1 - 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) - - Example: - - Create a random permutation of 30 integers to be inserted and - 19 of them deleted - >>> import numpy as np - >>> permutation = np.random.permutation(list(range(30))) - - Create a Heap and insert the 30 integers - - __init__() test - >>> first_heap = BinomialHeap() - - 30 inserts - insert() test - >>> for number in permutation: - ... first_heap.insert(number) - - Size test - >>> print(first_heap.size) - 30 - - Deleting - delete() test - >>> for i in range(25): - ... print(first_heap.deleteMin(), end=" ") - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 - - Create a new Heap - >>> second_heap = BinomialHeap() - >>> vals = [17, 20, 31, 34] - >>> for value in vals: - ... second_heap.insert(value) - - - The heap should have the following structure: - - 17 - / \ - # 31 - / \ - 20 34 - / \ / \ - # # # # - - preOrder() test - >>> print(second_heap.preOrder()) - [(17, 0), ('#', 1), (31, 1), (20, 2), ('#', 3), ('#', 3), (34, 2), ('#', 3), ('#', 3)] - - printing Heap - __str__() test - >>> print(second_heap) - 17 - -# - -31 - --20 - ---# - ---# - --34 - ---# - ---# - - mergeHeaps() test - >>> merged = second_heap.mergeHeaps(first_heap) - >>> merged.peek() - 17 - - values in merged heap; (merge is inplace) - >>> while not first_heap.isEmpty(): - ... print(first_heap.deleteMin(), end=" ") - 17 20 25 26 27 28 29 31 34 - + + Example: + + Create a random permutation of 30 integers to be inserted and 19 of them deleted + >>> import numpy as np + >>> permutation = np.random.permutation(list(range(30))) + + Create a Heap and insert the 30 integers + __init__() test + >>> first_heap = BinomialHeap() + + 30 inserts - insert() test + >>> for number in permutation: + ... first_heap.insert(number) + + Size test + >>> print(first_heap.size) + 30 + + Deleting - delete() test + >>> for i in range(25): + ... print(first_heap.deleteMin(), end=" ") + 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 + + Create a new Heap + >>> second_heap = BinomialHeap() + >>> vals = [17, 20, 31, 34] + >>> for value in vals: + ... second_heap.insert(value) + + + The heap should have the following structure: + + 17 + / \ + # 31 + / \ + 20 34 + / \ / \ + # # # # + + preOrder() test + >>> print(second_heap.preOrder()) + [(17, 0), ('#', 1), (31, 1), (20, 2), ('#', 3), ('#', 3), (34, 2), ('#', 3), ('#', 3)] + + printing Heap - __str__() test + >>> print(second_heap) + 17 + -# + -31 + --20 + ---# + ---# + --34 + ---# + ---# + + mergeHeaps() test + >>> merged = second_heap.mergeHeaps(first_heap) + >>> merged.peek() + 17 + + values in merged heap; (merge is inplace) + >>> while not first_heap.isEmpty(): + ... print(first_heap.deleteMin(), end=" ") + 17 20 25 26 27 28 29 31 34 """ def __init__(self, bottom_root=None, min_node=None, heap_size=0): @@ -133,8 +128,8 @@ def __init__(self, bottom_root=None, min_node=None, heap_size=0): def mergeHeaps(self, other): """ - In-place merge of two binomial heaps. - Both of them become the resulting merged heap + In-place merge of two binomial heaps. + Both of them become the resulting merged heap """ # Empty heaps corner cases @@ -209,7 +204,7 @@ def mergeHeaps(self, other): def insert(self, val): """ - insert a value in the heap + insert a value in the heap """ if self.size == 0: self.bottom_root = Node(val) @@ -251,7 +246,7 @@ def insert(self, val): def peek(self): """ - return min element without deleting it + return min element without deleting it """ return self.min_node.val @@ -260,7 +255,7 @@ def isEmpty(self): def deleteMin(self): """ - delete min element and return it + delete min element and return it """ # assert not self.isEmpty(), "Empty Heap" @@ -363,9 +358,9 @@ def deleteMin(self): def preOrder(self): """ - Returns the Pre-order representation of the heap including - values of nodes plus their level distance from the root; - Empty nodes appear as # + Returns the Pre-order representation of the heap including + values of nodes plus their level distance from the root; + Empty nodes appear as # """ # Find top root top_root = self.bottom_root @@ -378,7 +373,7 @@ def preOrder(self): def __traversal(self, curr_node, preorder, level=0): """ - Pre-order traversal of nodes + Pre-order traversal of nodes """ if curr_node: preorder.append((curr_node.val, level)) @@ -389,8 +384,8 @@ def __traversal(self, curr_node, preorder, level=0): def __str__(self): """ - Overwriting str for a pre-order print of nodes in heap; - Performance is poor, so use only for small examples + Overwriting str for a pre-order print of nodes in heap; + Performance is poor, so use only for small examples """ if self.isEmpty(): return "" From adc9a0cf432cd58d0f178e8f508e125f4b23cef2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:42:27 +0100 Subject: [PATCH 7/9] Add a space --- data_structures/heap/binomial_heap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index 1588f4529120..7cc233366b38 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -75,7 +75,7 @@ class BinomialHeap: Deleting - delete() test >>> for i in range(25): ... print(first_heap.deleteMin(), end=" ") - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 + 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Create a new Heap >>> second_heap = BinomialHeap() From 9483c70f341ed0fcf5d2b8e62732c51ea2590153 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:46:01 +0100 Subject: [PATCH 8/9] Remove a space --- data_structures/heap/binomial_heap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index 7cc233366b38..50bea2494319 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -106,7 +106,7 @@ class BinomialHeap: --20 ---# ---# - --34 + --34 ---# ---# From 13d0e782a6d8d968518a25e9acfe80c8594a997d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 29 Oct 2019 23:50:05 +0100 Subject: [PATCH 9/9] Add a space --- data_structures/heap/binomial_heap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index 50bea2494319..7f570f1c755b 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -118,7 +118,7 @@ class BinomialHeap: values in merged heap; (merge is inplace) >>> while not first_heap.isEmpty(): ... print(first_heap.deleteMin(), end=" ") - 17 20 25 26 27 28 29 31 34 + 17 20 25 26 27 28 29 31 34 """ def __init__(self, bottom_root=None, min_node=None, heap_size=0):