Skip to content

Commit 0bc69d4

Browse files
committed
Merge branch 'master' of github.com:TheAlgorithms/Python
2 parents 36ecbc7 + dd62f1b commit 0bc69d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1042
-555
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ ENV/
8989
# Rope project settings
9090
.ropeproject
9191
.idea
92-
.DS_Store
92+
.DS_Store
93+
.try

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ install:
1616
- pip install flake8 # pytest # add another testing frameworks later
1717
before_script:
1818
# stop the build if there are Python syntax errors or undefined names
19-
- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
19+
- flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
2020
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
2121
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
2222
script:

Graphs/prim.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Prim's Algorithm.
3+
4+
Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm
5+
6+
Create a list to store x the vertices.
7+
G = [vertex(n) for n in range(x)]
8+
9+
For each vertex in G, add the neighbors:
10+
G[x].addNeighbor(G[y])
11+
G[y].addNeighbor(G[x])
12+
13+
For each vertex in G, add the edges:
14+
G[x].addEdge(G[y], w)
15+
G[y].addEdge(G[x], w)
16+
17+
To solve run:
18+
MST = prim(G, G[0])
19+
"""
20+
21+
import math
22+
23+
24+
class vertex():
25+
"""Class Vertex."""
26+
27+
def __init__(self, id):
28+
"""
29+
Arguments:
30+
id - input an id to identify the vertex
31+
32+
Attributes:
33+
neighbors - a list of the vertices it is linked to
34+
edges - a dict to store the edges's weight
35+
"""
36+
self.id = str(id)
37+
self.key = None
38+
self.pi = None
39+
self.neighbors = []
40+
self.edges = {} # [vertex:distance]
41+
42+
def __lt__(self, other):
43+
"""Comparison rule to < operator."""
44+
return (self.key < other.key)
45+
46+
def __repr__(self):
47+
"""Return the vertex id."""
48+
return self.id
49+
50+
def addNeighbor(self, vertex):
51+
"""Add a pointer to a vertex at neighbor's list."""
52+
self.neighbors.append(vertex)
53+
54+
def addEdge(self, vertex, weight):
55+
"""Destination vertex and weight."""
56+
self.edges[vertex.id] = weight
57+
58+
59+
def prim(graph, root):
60+
"""
61+
Prim's Algorithm.
62+
63+
Return a list with the edges of a Minimum Spanning Tree
64+
65+
prim(graph, graph[0])
66+
"""
67+
A = []
68+
for u in graph:
69+
u.key = math.inf
70+
u.pi = None
71+
root.key = 0
72+
Q = graph[:]
73+
while Q:
74+
u = min(Q)
75+
Q.remove(u)
76+
for v in u.neighbors:
77+
if (v in Q) and (u.edges[v.id] < v.key):
78+
v.pi = u
79+
v.key = u.edges[v.id]
80+
for i in range(1, len(graph)):
81+
A.append([graph[i].id, graph[i].pi.id])
82+
return(A)

License

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2016 The Algorithms
3+
Copyright (c) 2019 The Algorithms
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Maths/lucasSeries.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Lucas Sequence Using Recursion
2+
3+
def recur_luc(n):
4+
if n == 1:
5+
return n
6+
if n == 0:
7+
return 2
8+
return (recur_luc(n-1) + recur_luc(n-2))
9+
10+
limit = int(input("How many terms to include in Lucas series:"))
11+
print("Lucas series:")
12+
for i in range(limit):
13+
print(recur_luc(i))

Project Euler/Problem 01/sol5.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
a=3
2+
result=0
3+
while a=<1000:
4+
if(a%3==0 and a%5==0):
5+
result+=a
6+
elif(a%15==0):
7+
result-=a
8+
print(result)

README.md

Lines changed: 11 additions & 346 deletions
Large diffs are not rendered by default.

ciphers/Atbash.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def Atbash():
2+
inp=raw_input("Enter the sentence to be encrypted ")
3+
output=""
4+
for i in inp:
5+
extract=ord(i)
6+
if extract>=65 and extract<=90:
7+
output+=(unichr(155-extract))
8+
elif extract>=97 and extract<=122:
9+
output+=(unichr(219-extract))
10+
else:
11+
output+=i
12+
print (output)
13+
14+
Atbash() ;

ciphers/morse_Code_implementation.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Python program to implement Morse Code Translator
2+
3+
4+
# Dictionary representing the morse code chart
5+
MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
6+
'C':'-.-.', 'D':'-..', 'E':'.',
7+
'F':'..-.', 'G':'--.', 'H':'....',
8+
'I':'..', 'J':'.---', 'K':'-.-',
9+
'L':'.-..', 'M':'--', 'N':'-.',
10+
'O':'---', 'P':'.--.', 'Q':'--.-',
11+
'R':'.-.', 'S':'...', 'T':'-',
12+
'U':'..-', 'V':'...-', 'W':'.--',
13+
'X':'-..-', 'Y':'-.--', 'Z':'--..',
14+
'1':'.----', '2':'..---', '3':'...--',
15+
'4':'....-', '5':'.....', '6':'-....',
16+
'7':'--...', '8':'---..', '9':'----.',
17+
'0':'-----', ', ':'--..--', '.':'.-.-.-',
18+
'?':'..--..', '/':'-..-.', '-':'-....-',
19+
'(':'-.--.', ')':'-.--.-'}
20+
21+
22+
def encrypt(message):
23+
cipher = ''
24+
for letter in message:
25+
if letter != ' ':
26+
27+
28+
cipher += MORSE_CODE_DICT[letter] + ' '
29+
else:
30+
31+
cipher += ' '
32+
33+
return cipher
34+
35+
36+
def decrypt(message):
37+
38+
message += ' '
39+
40+
decipher = ''
41+
citext = ''
42+
for letter in message:
43+
44+
if (letter != ' '):
45+
46+
47+
i = 0
48+
49+
50+
citext += letter
51+
52+
else:
53+
54+
i += 1
55+
56+
57+
if i == 2 :
58+
59+
60+
decipher += ' '
61+
else:
62+
63+
64+
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
65+
.values()).index(citext)]
66+
citext = ''
67+
68+
return decipher
69+
70+
71+
def main():
72+
message = "Morse code here"
73+
result = encrypt(message.upper())
74+
print (result)
75+
76+
message = result
77+
result = decrypt(message)
78+
print (result)
79+
80+
81+
if __name__ == '__main__':
82+
main()

compression/huffman.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, freq, left, right):
1818

1919

2020
def parse_file(file_path):
21-
"""
21+
"""
2222
Read the file and build a dict of all letters and their
2323
frequences, then convert the dict into a list of Letters.
2424
"""
@@ -29,15 +29,10 @@ def parse_file(file_path):
2929
if not c:
3030
break
3131
chars[c] = chars[c] + 1 if c in chars.keys() else 1
32-
letters = []
33-
for char, freq in chars.items():
34-
letter = Letter(char, freq)
35-
letters.append(letter)
36-
letters.sort(key=lambda l: l.freq)
37-
return letters
32+
return sorted([Letter(c, f) for c, f in chars.items()], key=lambda l: l.freq)
3833

3934
def build_tree(letters):
40-
"""
35+
"""
4136
Run through the list of Letters and build the min heap
4237
for the Huffman Tree.
4338
"""
@@ -51,7 +46,7 @@ def build_tree(letters):
5146
return letters[0]
5247

5348
def traverse_tree(root, bitstring):
54-
"""
49+
"""
5550
Recursively traverse the Huffman Tree to set each
5651
Letter's bitstring, and return the list of Letters
5752
"""
@@ -64,9 +59,9 @@ def traverse_tree(root, bitstring):
6459
return letters
6560

6661
def huffman(file_path):
67-
"""
62+
"""
6863
Parse the file, build the tree, then run through the file
69-
again, using the list of Letters to find and print out the
64+
again, using the list of Letters to find and print out the
7065
bitstring for each letter.
7166
"""
7267
letters_list = parse_file(file_path)

0 commit comments

Comments
 (0)