Skip to content

fellow Python's master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 14, 2019
72 changes: 72 additions & 0 deletions Graphs/pagerank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'''
Author: https://github.com/bhushan-borole
'''
'''
The input graph for the algorithm is:

A B C
A 0 1 1
B 0 0 1
C 1 0 0

'''

graph = [[0, 1, 1],
[0, 0, 1],
[1, 0, 0]]


class Node:
def __init__(self, name):
self.name = name
self.inbound = []
self.outbound = []

def add_inbound(self, node):
self.inbound.append(node)

def add_outbound(self, node):
self.outbound.append(node)

def __repr__(self):
return 'Node {}: Inbound: {} ; Outbound: {}'.format(self.name,
self.inbound,
self.outbound)


def page_rank(nodes, limit=3, d=0.85):
ranks = {}
for node in nodes:
ranks[node.name] = 1

outbounds = {}
for node in nodes:
outbounds[node.name] = len(node.outbound)

for i in range(limit):
print("======= Iteration {} =======".format(i+1))
for j, node in enumerate(nodes):
ranks[node.name] = (1 - d) + d * sum([ ranks[ib]/outbounds[ib] for ib in node.inbound ])
print(ranks)


def main():
names = list(input('Enter Names of the Nodes: ').split())

nodes = [Node(name) for name in names]

for ri, row in enumerate(graph):
for ci, col in enumerate(row):
if col == 1:
nodes[ci].add_inbound(names[ri])
nodes[ri].add_outbound(names[ci])

print("======= Nodes =======")
for node in nodes:
print(node)

page_rank(nodes)


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# The Algorithms - Python <!-- [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python) -->
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/TheAlgorithms/100)


### All algorithms implemented in Python (for education)

Expand Down
87 changes: 87 additions & 0 deletions compression/huffman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import sys

class Letter:
def __init__(self, letter, freq):
self.letter = letter
self.freq = freq
self.bitstring = ""

def __repr__(self):
return f'{self.letter}:{self.freq}'


class TreeNode:
def __init__(self, freq, left, right):
self.freq = freq
self.left = left
self.right = right


def parse_file(file_path):
"""
Read the file and build a dict of all letters and their
frequences, then convert the dict into a list of Letters.
"""
chars = {}
with open(file_path) as f:
while True:
c = f.read(1)
if not c:
break
chars[c] = chars[c] + 1 if c in chars.keys() else 1
letters = []
for char, freq in chars.items():
letter = Letter(char, freq)
letters.append(letter)
letters.sort(key=lambda l: l.freq)
return letters

def build_tree(letters):
"""
Run through the list of Letters and build the min heap
for the Huffman Tree.
"""
while len(letters) > 1:
left = letters.pop(0)
right = letters.pop(0)
total_freq = left.freq + right.freq
node = TreeNode(total_freq, left, right)
letters.append(node)
letters.sort(key=lambda l: l.freq)
return letters[0]

def traverse_tree(root, bitstring):
"""
Recursively traverse the Huffman Tree to set each
Letter's bitstring, and return the list of Letters
"""
if type(root) is Letter:
root.bitstring = bitstring
return [root]
letters = []
letters += traverse_tree(root.left, bitstring + "0")
letters += traverse_tree(root.right, bitstring + "1")
return letters

def huffman(file_path):
"""
Parse the file, build the tree, then run through the file
again, using the list of Letters to find and print out the
bitstring for each letter.
"""
letters_list = parse_file(file_path)
root = build_tree(letters_list)
letters = traverse_tree(root, "")
print(f'Huffman Coding of {file_path}: ')
with open(file_path) as f:
while True:
c = f.read(1)
if not c:
break
le = list(filter(lambda l: l.letter == c, letters))[0]
print(le.bitstring, end=" ")
print()

if __name__ == "__main__":
# pass the file path to the huffman function
huffman(sys.argv[1])
4 changes: 4 additions & 0 deletions graphs/Directed and Undirected (Weighted) Graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def cycle_nodes(self):
parent = -2
indirect_parents = []
ss = s
on_the_way_back = False
anticipating_nodes = set()

while True:
Expand Down Expand Up @@ -199,6 +200,7 @@ def has_cycle(self):
parent = -2
indirect_parents = []
ss = s
on_the_way_back = False
anticipating_nodes = set()

while True:
Expand Down Expand Up @@ -367,6 +369,7 @@ def cycle_nodes(self):
parent = -2
indirect_parents = []
ss = s
on_the_way_back = False
anticipating_nodes = set()

while True:
Expand Down Expand Up @@ -414,6 +417,7 @@ def has_cycle(self):
parent = -2
indirect_parents = []
ss = s
on_the_way_back = False
anticipating_nodes = set()

while True:
Expand Down
25 changes: 25 additions & 0 deletions maths/BinaryExponentiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#Author : Junth Basnet
#Time Complexity : O(logn)

def binary_exponentiation(a, n):

if (n == 0):
return 1

elif (n % 2 == 1):
return binary_exponentiation(a, n - 1) * a

else:
b = binary_exponentiation(a, n / 2)
return b * b


try:
base = int(input('Enter Base : '))
power = int(input("Enter Power : "))
except ValueError:
print ("Invalid literal for integer")

result = binary_exponentiation(base, power)
print("{}^({}) : {}".format(base, power, result))

55 changes: 48 additions & 7 deletions maths/PrimeCheck.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
import math
import unittest


def primeCheck(number):
if number % 2 == 0 and number > 2:
"""
A number is prime if it has exactly two dividers: 1 and itself.
"""
if number < 2:
# Negatives, 0 and 1 are not primes
return False
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
if number < 4:
# 2 and 3 are primes
return True
if number % 2 == 0:
# Even values are not primes
return False

# Except 2, all primes are odd. If any odd value divide
# the number, then that number is not prime.
odd_numbers = range(3, int(math.sqrt(number)) + 1, 2)
return not any(number % i == 0 for i in odd_numbers)


class Test(unittest.TestCase):
def test_primes(self):
self.assertTrue(primeCheck(2))
self.assertTrue(primeCheck(3))
self.assertTrue(primeCheck(5))
self.assertTrue(primeCheck(7))
self.assertTrue(primeCheck(11))
self.assertTrue(primeCheck(13))
self.assertTrue(primeCheck(17))
self.assertTrue(primeCheck(19))
self.assertTrue(primeCheck(23))
self.assertTrue(primeCheck(29))

def test_not_primes(self):
self.assertFalse(primeCheck(-19),
"Negative numbers are not prime.")
self.assertFalse(primeCheck(0),
"Zero doesn't have any divider, primes must have two")
self.assertFalse(primeCheck(1),
"One just have 1 divider, primes must have two.")
self.assertFalse(primeCheck(2 * 2))
self.assertFalse(primeCheck(2 * 3))
self.assertFalse(primeCheck(3 * 3))
self.assertFalse(primeCheck(3 * 5))
self.assertFalse(primeCheck(3 * 5 * 7))

def main():
print(primeCheck(37))
print(primeCheck(100))
print(primeCheck(77))

if __name__ == '__main__':
main()
unittest.main()

4 changes: 2 additions & 2 deletions other/sierpinski_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Usage:
- $python sierpinski_triangle.py <int:depth_for_fractal>

Credits: This code was written by editing the code from http://www.lpb-riannetrujillo.com/blog/python-fractal/
Credits: This code was written by editing the code from http://www.riannetrujillo.com/blog/python-fractal/

'''
import turtle
Expand Down Expand Up @@ -64,4 +64,4 @@ def triangle(points,depth):
depth-1)


triangle(points,int(sys.argv[1]))
triangle(points,int(sys.argv[1]))
13 changes: 13 additions & 0 deletions project_euler/problem_02/sol4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import math
from decimal import *

getcontext().prec = 100
phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2)

n = Decimal(int(input()) - 1)

index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2
num = round(phi ** Decimal(index + 1)) / (phi + 2)
sum = num // 2

print(int(sum))
24 changes: 12 additions & 12 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
def binary_search(sorted_collection, item):
"""Pure implementation of binary search algorithm in Python

Be careful collection must be sorted, otherwise result will be
Be careful collection must be ascending sorted, otherwise result will be
unpredictable

:param sorted_collection: some sorted collection with comparable items
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of found item or None if item is not found

Expand Down Expand Up @@ -60,10 +60,10 @@ def binary_search(sorted_collection, item):
def binary_search_std_lib(sorted_collection, item):
"""Pure implementation of binary search algorithm in Python using stdlib

Be careful collection must be sorted, otherwise result will be
Be careful collection must be ascending sorted, otherwise result will be
unpredictable

:param sorted_collection: some sorted collection with comparable items
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of found item or None if item is not found

Expand All @@ -89,11 +89,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):

"""Pure implementation of binary search algorithm in Python by recursion

Be careful collection must be sorted, otherwise result will be
Be careful collection must be ascending sorted, otherwise result will be
unpredictable
First recursion should be started with left=0 and right=(len(sorted_collection)-1)

:param sorted_collection: some sorted collection with comparable items
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of found item or None if item is not found

Expand Down Expand Up @@ -123,11 +123,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
return binary_search_by_recursion(sorted_collection, item, midpoint+1, right)

def __assert_sorted(collection):
"""Check if collection is sorted, if not - raises :py:class:`ValueError`
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`

:param collection: collection
:return: True if collection is sorted
:raise: :py:class:`ValueError` if collection is not sorted
:return: True if collection is ascending sorted
:raise: :py:class:`ValueError` if collection is not ascending sorted

Examples:
>>> __assert_sorted([0, 1, 2, 4])
Expand All @@ -136,10 +136,10 @@ def __assert_sorted(collection):
>>> __assert_sorted([10, -1, 5])
Traceback (most recent call last):
...
ValueError: Collection must be sorted
ValueError: Collection must be ascending sorted
"""
if collection != sorted(collection):
raise ValueError('Collection must be sorted')
raise ValueError('Collection must be ascending sorted')
return True


Expand All @@ -150,7 +150,7 @@ def __assert_sorted(collection):
try:
__assert_sorted(collection)
except ValueError:
sys.exit('Sequence must be sorted to apply binary search')
sys.exit('Sequence must be ascending sorted to apply binary search')

target_input = raw_input('Enter a single number to be found in the list:\n')
target = int(target_input)
Expand Down
Loading