Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
'math',
'searching',
'sorting',
'string',
'strings',
'pathfinding'
'geometry',
'greedy_algorithm'
Expand Down
4 changes: 3 additions & 1 deletion pygorithm/binary/ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

Author: Ian Doarn
"""
from pygorithm.binary.binary_utils import pad
from pygorithm.binary.base10 import to_base2 as b10_to_b2
from pygorithm.binary.base2 import to_base16 as b2_to_b16, \
to_ascii as b2_to_ascii
Expand Down Expand Up @@ -47,7 +48,8 @@ def to_base2(string, visualize=False, as_string=False):
x, str(ord(x)),
str(b10_to_b2(ord(x)))
))
_list.append(str(b10_to_b2(ord(x))))
value = pad(str(b10_to_b2(ord(x))))
_list.append(value)

if as_string:
return ' '.join(_list)
Expand Down
2 changes: 1 addition & 1 deletion pygorithm/binary/base10.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def to_base16(n, visualize=False):
print("{} % 16 = {} -> hex = {}".format(
str(n), str(n % 16), HEX_VALUES[n % 16]
))
_list.append(HEX_VALUES[n % 16])
_list.append(HEX_VALUES[n % 16])
n = int(n / 16)

if visualize:
Expand Down
3 changes: 2 additions & 1 deletion pygorithm/binary/base16.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Author: Ian Doarn
"""
from pygorithm.binary.base2 import to_ascii as b2_to_ascii
from pygorithm.binary.binary_utils import pad
from math import pow

HEX_BINARY_VALUES = {
Expand Down Expand Up @@ -45,7 +46,7 @@ def to_base2(h, visualize=False):
print("{} -> {}".format(
value, HEX_BINARY_VALUES[value]
))
_list.append(HEX_BINARY_VALUES[value])
_list.append(pad(HEX_BINARY_VALUES[value]))

return int(''.join(_list))

Expand Down
16 changes: 16 additions & 0 deletions pygorithm/binary/binary_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Helper methods for binary package
Author: Ian Doarn
"""


def pad(value: str, return_type=str) -> """Pad binary value with zeros""":
if len(value) % 4 != 0:
pad_amount = 4 - (len(value) % 4)
return return_type(('0' * pad_amount) + value)
else:
return return_type(value)


def to_string(binary_array: list, delimiter=' ') -> """Convert binary array to string""":
return delimiter.join(binary_array)
2 changes: 1 addition & 1 deletion pygorithm/dynamic_programming/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

__all__ = [
'binary_knapsack',
'lis',
'lis'
]
25 changes: 13 additions & 12 deletions pygorithm/dynamic_programming/binary_knapsack.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'''
"""
Author: Omkar Pathak
Created At: 25th August 2017
'''

"""
import inspect
# TODO: Explain how this works / Explain what a knapsack is


def knapsack(W, value, weight):
'''
:param W: maximum weight capacity
:param value: an array of values of items in the knapsack
:param weight: an array of weights of items in the knapsack
'''
def knapsack(w, value, weight):
"""
:param w: maximum weight capacity
:param value: an array of values of items in the knapsack
:param weight: an array of weights of items in the knapsack
"""
if type(value) is not list:
raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(value))))
if type(weight) is not list:
Expand All @@ -22,13 +23,13 @@ def knapsack(W, value, weight):
# n = number of items
n = len(value)

knap_sack = [[0 for x in range(W+1)] for x in range(n+1)]
knap_sack = [[0 for _ in range(w+1)] for _ in range(n+1)]

for j in range(W + 1):
for j in range(w + 1):
knap_sack[0][j] = 0

for i in range(n + 1):
for w in range(W + 1):
for w in range(w + 1):
if weight[i - 1] <= w:
knap_sack[i][w] = max(value[i - 1] + knap_sack[i - 1][w - weight[i - 1]], knap_sack[i - 1][w])
else:
Expand Down
20 changes: 11 additions & 9 deletions pygorithm/dynamic_programming/lis.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
'''
"""
Author: Omkar Pathak
Created At: 25th August 2017
'''
"""


def longest_increasing_subsequence(_list):
'''
"""
The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a
given sequence such that all elements of the subsequence are sorted in increasing order. For example,
the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.
'''
:param _list:
:return:
"""
# Initialize list with some value
lis = [1] * len(_list)
# list for storing the elements in an lis
elements = [0] * len(_list)

# Compute optimized LIS values in bottom up manner
for i in range (1 , len(_list)):
for j in range(0 , i):
if _list[i] > _list[j] and lis[i]< lis[j] + 1:
for i in range(1, len(_list)):
for j in range(0, i):
if _list[i] > _list[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j]+1
elements[i] = j

idx = 0

# find the maximum of the whole list and get its index in idx
maximum = max(lis)
idx = lis.index(maximum)
Expand All @@ -35,6 +36,7 @@ def longest_increasing_subsequence(_list):

return (maximum, seq[::-1])


def get_code():
"""
returns the code for the longest_increasing_subsequence function
Expand Down
2 changes: 1 addition & 1 deletion pygorithm/greedy_algorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

__all__ = [
'fractional_knapsack',
'activity_selection',
'activity_selection'
]
4 changes: 3 additions & 1 deletion pygorithm/greedy_algorithm/activity_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Author: OMKAR PATHAK
Created On: 26th August 2017
"""

import inspect
# TODO: Explain what this is / how it works


def activity_selection(start_times, finish_times):
"""
Expand Down Expand Up @@ -37,6 +38,7 @@ def activity_selection(start_times, finish_times):

return activity


def get_code():
"""
returns the code for the activity_selection function
Expand Down
11 changes: 5 additions & 6 deletions pygorithm/greedy_algorithm/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
Author: SHARAD BHAT
Created On: 22nd August 2017
"""

import inspect
# TODO: Explain how this works / Explain what a knapsack is


def knapsack(W, item_values, item_weights):
def knapsack(w, item_values, item_weights):
"""
:param W: maximum weight capacity
:param w: maximum weight capacity
:param item_values: a list of values of items in the knapsack
:param item_weights: a list of weights of items in the knapsack
"""
Expand Down Expand Up @@ -38,21 +39,19 @@ def knapsack(W, item_values, item_weights):
item_weights[i], item_weights[maximum] = item_weights[maximum], item_weights[i]

# Placing items in knapsack
remaining_space = W
remaining_space = w
profit = 0
for i in range(0, n):
if remaining_space > item_weights[i]:
profit += item_values[i]
remaining_space -= item_weights[i]
else:
profit += fractional_weights[i] * remaining_space
remaining_space = 0
break

return profit



def get_code():
"""
returns the code for the knapsack function
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions tests/test_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import unittest

from pygorithm.binary import (
ascii,
base2,
base10,
base16
)


class TestBase2(unittest.TestCase):
def test_base2_to_ascii(self):
array = ['01010100', '01101000', '01100101', '00100000', '01010001', '01110101', '01101001',
'01100011',
'01101011', '00100000', '01000010', '01110010', '01101111', '01110111', '01101110',
'00100000',
'01000110', '01101111', '01111000', '00100000', '01001010', '01110101', '01101101',
'01110000',
'01110011', '00100000', '01001111', '01110110', '01100101', '01110010', '00100000',
'01110100',
'01101000', '01100101', '00100000', '01001100', '01100001', '01111010', '01111001',
'00100000',
'01000100', '01101111', '01100111']

self.assertEqual(base2.to_ascii(array), "The Quick Brown Fox Jumps Over the Lazy Dog")

def test_base2_to_base10(self):
self.assertEqual(base2.to_base10(1101001000101001), 53801)
self.assertEqual(base2.to_base10(101111101011110000011111111), 99999999)
self.assertEqual(base2.to_base10(10011110110001100001010001100101000000110001), 10910848929841)

def test_base2_to_base16(self):
self.assertEqual(base2.to_base16(1101001000101001), 'D229')
self.assertEqual(base2.to_base16(101111101011110000011111111), '5F5E0FF')
self.assertEqual(base2.to_base16(10011110110001100001010001100101000000110001), '9EC61465031')


class TestBase10(unittest.TestCase):
def test_base10_to_base2(self):
self.assertEqual(base10.to_base2(10), 1010)
self.assertEqual(base10.to_base2(99999999), 101111101011110000011111111)
self.assertEqual(base10.to_base2(1234567890), 1001001100101100000001011010010)

def test_base10_to_base16(self):
self.assertEqual(base10.to_base16(99999999), '5F5E0FF')
self.assertEqual(base10.to_base16(1111111111111111111), 'F6B75AB2BC47207')
self.assertEqual(base10.to_base16(34875439754935739457), '1E3FE73ADDA7B2001')
self.assertEqual(base10.to_base16(3735928559), 'DEADBEEF')


class TestBase16(unittest.TestCase):
def test_base16_to_base2(self):
self.assertEqual(base16.to_base2('DEADBEEF'), 11011110101011011011111011101111)
self.assertEqual(base16.to_base2('FFFFFFFFFFFFFFF'),
111111111111111111111111111111111111111111111111111111111111)
self.assertEqual(base16.to_base2('23F235E865A45C'), 100011111100100011010111101000011001011010010001011100)

def test_base16_to_base10(self):
self.assertEqual(base16.to_base10('DEADBEEF'), 3735928559)
self.assertEqual(base16.to_base10('FFFFFFFFFFFFFFF'), 1152921504606846976)
self.assertEqual(base16.to_base10('23F235E865A45C'), 10117937531036764)

def test_base16_to_ascii(self):
array = ['54', '68', '65', '20', '51', '75', '69', '63', '6B', '20', '42', '72', '6F', '77', '6E', '20', '46',
'6F', '78', '20', '4A', '75', '6D', '70', '73', '20', '4F', '76', '65', '72', '20', '74', '68', '65',
'20', '4C', '61', '7A', '79', '20', '44', '6F', '67']

array_2 = ['77', '48', '40', '74', '20', '5F', '54', '2D', '68', '33', '20', '2F', '2F', '2D', '46', '3D', '7E',
'21', '63', '6B']

self.assertEqual(base16.to_ascii(array), "The Quick Brown Fox Jumps Over the Lazy Dog")
self.assertEqual(base16.to_ascii(array_2), "wH@t _T-h3 //-F=~!ck")


class TestASCII(unittest.TestCase):
def test_ascii_to_base16(self):
array = ['54', '68', '65', '20', '51', '75', '69', '63', '6B', '20', '42', '72', '6F', '77', '6E', '20', '46',
'6F', '78', '20', '4A', '75', '6D', '70', '73', '20', '4F', '76', '65', '72', '20', '74', '68', '65',
'20', '4C', '61', '7A', '79', '20', '44', '6F', '67']

array_2 = ['77', '48', '40', '74', '20', '5F', '54', '2D', '68', '33', '20', '2F', '2F', '2D', '46', '3D', '7E',
'21', '63', '6B']

self.assertEqual(ascii.to_base16("The Quick Brown Fox Jumps Over the Lazy Dog"), array)
self.assertEqual(ascii.to_base16("wH@t _T-h3 //-F=~!ck"), array_2)

def test_ascii_to_base2(self):
array = ['01010100', '01101000', '01100101', '00100000', '01010001', '01110101', '01101001',
'01100011',
'01101011', '00100000', '01000010', '01110010', '01101111', '01110111', '01101110',
'00100000',
'01000110', '01101111', '01111000', '00100000', '01001010', '01110101', '01101101',
'01110000',
'01110011', '00100000', '01001111', '01110110', '01100101', '01110010', '00100000',
'01110100',
'01101000', '01100101', '00100000', '01001100', '01100001', '01111010', '01111001',
'00100000',
'01000100', '01101111', '01100111']

array_2 = ['01110111', '01001000', '01000000', '01110100', '00100000', '01011111', '01010100', '00101101',
'01101000',
'00110011', '00100000', '00101111', '00101111', '00101101', '01000110', '00111101', '01111110',
'00100001',
'01100011', '01101011']

self.assertEqual(ascii.to_base2("wH@t _T-h3 //-F=~!ck"), array_2)
self.assertEqual(ascii.to_base2("The Quick Brown Fox Jumps Over the Lazy Dog"), array)

if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions tests/test_dynamic_programming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def test_binary_knapsack(self):
value = [60, 100, 120]
weight = [10, 20, 30]
W = 50
n = len(value)
self.assertEqual(binary_knapsack.knapsack(W, value, weight, n), 220)
self.assertEqual(binary_knapsack.knapsack(W, value, weight), 220)

class TestLongestIncreasingSubsequence(unittest.TestCase):
def test_lis(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from pygorithm.string import (
from pygorithm.strings import (
anagram,
isogram,
pangram,
Expand Down