Skip to content

Commit df2fc9e

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
pull from master
2 parents 39c40e7 + 357dbd4 commit df2fc9e

25 files changed

+1479
-236
lines changed

Diff for: Travis_CI_tests_are_failing.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Travis CI test are failing
2+
### How do I find out what is wrong with my pull request?
3+
1. In your PR look for the failing test and click the `Details` link: ![Travis_CI_fail_1.png](images/Travis_CI_fail_1.png)
4+
2. On the next page, click `The build failed` link: ![Travis_CI_fail_2.png](images/Travis_CI_fail_2.png)
5+
3. Now scroll down and look for `red` text describing the error(s) in the test log.
6+
7+
Pull requests will __not__ be merged if the Travis CI tests are failing.
8+
9+
If anything is unclear, please read through [CONTRIBUTING.md](CONTRIBUTING.md) and attempt to run the failing tests on your computer before asking for assistance.

Diff for: arithmetic_analysis/secant_method.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Implementing Secant method in Python
2+
# Author: dimgrichr
3+
4+
5+
from math import exp
6+
7+
8+
def f(x):
9+
"""
10+
>>> f(5)
11+
39.98652410600183
12+
"""
13+
return 8 * x - 2 * exp(-x)
14+
15+
16+
def SecantMethod(lower_bound, upper_bound, repeats):
17+
"""
18+
>>> SecantMethod(1, 3, 2)
19+
0.2139409276214589
20+
"""
21+
x0 = lower_bound
22+
x1 = upper_bound
23+
for i in range(0, repeats):
24+
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
25+
return x1
26+
27+
28+
print(f"The solution is: {SecantMethod(1, 3, 2)}")

Diff for: ciphers/diffie.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def find_primitive(n):
2+
for r in range(1, n):
3+
li = []
4+
for x in range(n-1):
5+
val = pow(r,x,n)
6+
if val in li:
7+
break
8+
li.append(val)
9+
else:
10+
return r
11+
12+
13+
if __name__ == "__main__":
14+
q = int(input('Enter a prime number q: '))
15+
a = find_primitive(q)
16+
a_private = int(input('Enter private key of A: '))
17+
a_public = pow(a, a_private, q)
18+
b_private = int(input('Enter private key of B: '))
19+
b_public = pow(a, b_private, q)
20+
21+
a_secret = pow(b_public, a_private, q)
22+
b_secret = pow(a_public, b_private, q)
23+
24+
print('The key value generated by A is: ', a_secret)
25+
print('The key value generated by B is: ', b_secret)
26+

Diff for: ciphers/mixed_keyword_cypher.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
def mixed_keyword(key="college", pt="UNIVERSITY"):
2+
"""
3+
4+
For key:hello
5+
6+
H E L O
7+
A B C D
8+
F G I J
9+
K M N P
10+
Q R S T
11+
U V W X
12+
Y Z
13+
and map vertically
14+
15+
>>> mixed_keyword("college", "UNIVERSITY") # doctest: +NORMALIZE_WHITESPACE
16+
{'A': 'C', 'B': 'A', 'C': 'I', 'D': 'P', 'E': 'U', 'F': 'Z', 'G': 'O', 'H': 'B',
17+
'I': 'J', 'J': 'Q', 'K': 'V', 'L': 'L', 'M': 'D', 'N': 'K', 'O': 'R', 'P': 'W',
18+
'Q': 'E', 'R': 'F', 'S': 'M', 'T': 'S', 'U': 'X', 'V': 'G', 'W': 'H', 'X': 'N',
19+
'Y': 'T', 'Z': 'Y'}
20+
'XKJGUFMJST'
21+
"""
22+
key = key.upper()
23+
pt = pt.upper()
24+
temp = []
25+
for i in key:
26+
if i not in temp:
27+
temp.append(i)
28+
l = len(temp)
29+
# print(temp)
30+
alpha = []
31+
modalpha = []
32+
# modalpha.append(temp)
33+
dic = dict()
34+
c = 0
35+
for i in range(65, 91):
36+
t = chr(i)
37+
alpha.append(t)
38+
if t not in temp:
39+
temp.append(t)
40+
# print(temp)
41+
r = int(26 / 4)
42+
# print(r)
43+
k = 0
44+
for i in range(r):
45+
t = []
46+
for j in range(l):
47+
t.append(temp[k])
48+
if not (k < 25):
49+
break
50+
k += 1
51+
modalpha.append(t)
52+
# print(modalpha)
53+
d = dict()
54+
j = 0
55+
k = 0
56+
for j in range(l):
57+
for i in modalpha:
58+
if not (len(i) - 1 >= j):
59+
break
60+
d[alpha[k]] = i[j]
61+
if not k < 25:
62+
break
63+
k += 1
64+
print(d)
65+
cypher = ""
66+
for i in pt:
67+
cypher += d[i]
68+
return cypher
69+
70+
71+
print(mixed_keyword("college", "UNIVERSITY"))

Diff for: data_structures/heap/binomial_heap.py

+86-91
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
2-
Binomial Heap
3-
4-
Reference: Advanced Data Structures, Peter Brass
2+
Binomial Heap
3+
Reference: Advanced Data Structures, Peter Brass
54
"""
65

76

@@ -10,7 +9,7 @@ class Node:
109
Node in a doubly-linked binomial tree, containing:
1110
- value
1211
- size of left subtree
13-
- link to left, right and parent nodes
12+
- link to left, right and parent nodes
1413
"""
1514

1615
def __init__(self, val):
@@ -23,8 +22,8 @@ def __init__(self, val):
2322

2423
def mergeTrees(self, other):
2524
"""
26-
In-place merge of two binomial trees of equal size.
27-
Returns the root of the resulting tree
25+
In-place merge of two binomial trees of equal size.
26+
Returns the root of the resulting tree
2827
"""
2928
assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks"
3029

@@ -47,83 +46,79 @@ def mergeTrees(self, other):
4746

4847

4948
class BinomialHeap:
50-
"""
51-
Min-oriented priority queue implemented with the Binomial Heap data
52-
structure implemented with the BinomialHeap class. It supports:
53-
49+
r"""
50+
Min-oriented priority queue implemented with the Binomial Heap data
51+
structure implemented with the BinomialHeap class. It supports:
5452
- Insert element in a heap with n elemnts: Guaranteed logn, amoratized 1
5553
- Merge (meld) heaps of size m and n: O(logn + logm)
56-
- Delete Min: O(logn)
54+
- Delete Min: O(logn)
5755
- Peek (return min without deleting it): O(1)
58-
59-
Example:
60-
61-
Create a random permutation of 30 integers to be inserted and
62-
19 of them deleted
63-
>>> import numpy as np
64-
>>> permutation = np.random.permutation(list(range(30)))
65-
66-
Create a Heap and insert the 30 integers
67-
68-
__init__() test
69-
>>> first_heap = BinomialHeap()
70-
71-
30 inserts - insert() test
72-
>>> for number in permutation:
73-
... first_heap.insert(number)
74-
75-
Size test
76-
>>> print(first_heap.size)
77-
30
78-
79-
Deleting - delete() test
80-
>>> for i in range(25):
81-
... print(first_heap.deleteMin(), end=" ")
82-
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
83-
84-
Create a new Heap
85-
>>> second_heap = BinomialHeap()
86-
>>> vals = [17, 20, 31, 34]
87-
>>> for value in vals:
88-
... second_heap.insert(value)
89-
90-
91-
The heap should have the following structure:
92-
93-
17
94-
/ \
95-
# 31
96-
/ \
97-
20 34
98-
/ \ / \
99-
# # # #
100-
101-
preOrder() test
102-
>>> print(second_heap.preOrder())
103-
[(17, 0), ('#', 1), (31, 1), (20, 2), ('#', 3), ('#', 3), (34, 2), ('#', 3), ('#', 3)]
104-
105-
printing Heap - __str__() test
106-
>>> print(second_heap)
107-
17
108-
-#
109-
-31
110-
--20
111-
---#
112-
---#
113-
--34
114-
---#
115-
---#
116-
117-
mergeHeaps() test
118-
>>> merged = second_heap.mergeHeaps(first_heap)
119-
>>> merged.peek()
120-
17
121-
122-
values in merged heap; (merge is inplace)
123-
>>> while not first_heap.isEmpty():
124-
... print(first_heap.deleteMin(), end=" ")
125-
17 20 25 26 27 28 29 31 34
126-
56+
57+
Example:
58+
59+
Create a random permutation of 30 integers to be inserted and 19 of them deleted
60+
>>> import numpy as np
61+
>>> permutation = np.random.permutation(list(range(30)))
62+
63+
Create a Heap and insert the 30 integers
64+
__init__() test
65+
>>> first_heap = BinomialHeap()
66+
67+
30 inserts - insert() test
68+
>>> for number in permutation:
69+
... first_heap.insert(number)
70+
71+
Size test
72+
>>> print(first_heap.size)
73+
30
74+
75+
Deleting - delete() test
76+
>>> for i in range(25):
77+
... print(first_heap.deleteMin(), end=" ")
78+
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
79+
80+
Create a new Heap
81+
>>> second_heap = BinomialHeap()
82+
>>> vals = [17, 20, 31, 34]
83+
>>> for value in vals:
84+
... second_heap.insert(value)
85+
86+
87+
The heap should have the following structure:
88+
89+
17
90+
/ \
91+
# 31
92+
/ \
93+
20 34
94+
/ \ / \
95+
# # # #
96+
97+
preOrder() test
98+
>>> print(second_heap.preOrder())
99+
[(17, 0), ('#', 1), (31, 1), (20, 2), ('#', 3), ('#', 3), (34, 2), ('#', 3), ('#', 3)]
100+
101+
printing Heap - __str__() test
102+
>>> print(second_heap)
103+
17
104+
-#
105+
-31
106+
--20
107+
---#
108+
---#
109+
--34
110+
---#
111+
---#
112+
113+
mergeHeaps() test
114+
>>> merged = second_heap.mergeHeaps(first_heap)
115+
>>> merged.peek()
116+
17
117+
118+
values in merged heap; (merge is inplace)
119+
>>> while not first_heap.isEmpty():
120+
... print(first_heap.deleteMin(), end=" ")
121+
17 20 25 26 27 28 29 31 34
127122
"""
128123

129124
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):
133128

134129
def mergeHeaps(self, other):
135130
"""
136-
In-place merge of two binomial heaps.
137-
Both of them become the resulting merged heap
131+
In-place merge of two binomial heaps.
132+
Both of them become the resulting merged heap
138133
"""
139134

140135
# Empty heaps corner cases
@@ -209,7 +204,7 @@ def mergeHeaps(self, other):
209204

210205
def insert(self, val):
211206
"""
212-
insert a value in the heap
207+
insert a value in the heap
213208
"""
214209
if self.size == 0:
215210
self.bottom_root = Node(val)
@@ -251,7 +246,7 @@ def insert(self, val):
251246

252247
def peek(self):
253248
"""
254-
return min element without deleting it
249+
return min element without deleting it
255250
"""
256251
return self.min_node.val
257252

@@ -260,7 +255,7 @@ def isEmpty(self):
260255

261256
def deleteMin(self):
262257
"""
263-
delete min element and return it
258+
delete min element and return it
264259
"""
265260
# assert not self.isEmpty(), "Empty Heap"
266261

@@ -363,9 +358,9 @@ def deleteMin(self):
363358

364359
def preOrder(self):
365360
"""
366-
Returns the Pre-order representation of the heap including
367-
values of nodes plus their level distance from the root;
368-
Empty nodes appear as #
361+
Returns the Pre-order representation of the heap including
362+
values of nodes plus their level distance from the root;
363+
Empty nodes appear as #
369364
"""
370365
# Find top root
371366
top_root = self.bottom_root
@@ -378,7 +373,7 @@ def preOrder(self):
378373

379374
def __traversal(self, curr_node, preorder, level=0):
380375
"""
381-
Pre-order traversal of nodes
376+
Pre-order traversal of nodes
382377
"""
383378
if curr_node:
384379
preorder.append((curr_node.val, level))
@@ -389,8 +384,8 @@ def __traversal(self, curr_node, preorder, level=0):
389384

390385
def __str__(self):
391386
"""
392-
Overwriting str for a pre-order print of nodes in heap;
393-
Performance is poor, so use only for small examples
387+
Overwriting str for a pre-order print of nodes in heap;
388+
Performance is poor, so use only for small examples
394389
"""
395390
if self.isEmpty():
396391
return ""

0 commit comments

Comments
 (0)