Skip to content
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

added algorithms in python and ruby #875

Merged
merged 11 commits into from
Dec 25, 2018
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
9 changes: 9 additions & 0 deletions Bit Manipulation/EvenOrOdd/evenodd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def isEven(n) :
return (n & 1);

n = 101;
if(isEven(n) == 0) :
print ("Even");
else :
print ("Odd");

44 changes: 44 additions & 0 deletions Other Algorithms/KMP/pyhton/kmpsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Python program for KMP Algorithm
def KMPSearch(pat, txt):
M = len(pat)
N = len(txt)
lps = [0]*M
j = 0
computeLPSArray(pat, M, lps)

i = 0
while i < N:
if pat[j] == txt[i]:
i += 1
j += 1

if j == M:
print "Found pattern at index " + str(i-j)
j = lps[j-1]
elif i < N and pat[j] != txt[i]:
if j != 0:
j = lps[j-1]
else:
i += 1

def computeLPSArray(pat, M, lps):
len = 0

lps[0]
i = 1
while i < M:
if pat[i]== pat[len]:
len += 1
lps[i] = len
i += 1
else:
if len != 0:
len = lps[len-1]
else:
lps[i] = 0
i += 1

txt = "ABABDABACDABABCABAB"
pat = "ABABCABAB"
KMPSearch(pat, txt)

11 changes: 11 additions & 0 deletions Recursive Algorithms/LCSRecursive/cpp/LCSREC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def lcs(X, Y, m, n):
if m == 0 or n == 0:
return 0
elif X[m-1] == Y[n-1]:
return 1 + lcs(X, Y, m-1, n-1)
else:
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n))

X = "SDFWERTHK"
Y = "QWEDFGHTY"
print ("Length of LCS is ", lcs(X , Y, len(X), len(Y)))
19 changes: 19 additions & 0 deletions Searching/binary search/Ruby/binSearch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def binary_search(a, n, x)
index_start = 0
index_end = n - 1

while index_start <= index_end do
index_middle = (index_start + index_end) / 2
if a[index_middle] == x
return index_middle
elsif x < a[index_middle]
index_end = index_middle - 1
else
index_start = index_middle + 1
end
end
return "not found"
end

a = [1, 2, 3, 4, 5, 7]
puts binary_search(a, a.size, 3)
18 changes: 18 additions & 0 deletions Sorting/Counting Sort/Ruby/countSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Array
def counting_sort!
replace counting_sort
end

def counting_sort
min, max = minmax
count = Array.new(max - min + 1, 0)
each {|number| count[number - min] += 1}
(min..max).each_with_object([]) {|i, ary| ary.concat([i] * count[i - min])}
end
end

ary = [9,7,10,2,9,7,4,3,10,2,7,10,2,1,3,8,7,3,9,5,8,5,1,6,3,7,5,4,6,9,9,6,6,10,2,4,5,2,8,2,2,5,2,9,3,3,5,7,8,4]
p ary.counting_sort.join(",")

p ary = Array.new(20){rand(-10..10)}
p ary.counting_sort
File renamed without changes.
21 changes: 21 additions & 0 deletions Sorting/Gnome Sort/Ruby/GnomeSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Array
def gnomesort!
i, j = 1, 2
while i < length
if self[i-1] <= self[i]
i, j = j, j+1
else
self[i-1], self[i] = self[i], self[i-1]
i -= 1
if i == 0
i, j = j, j+1
end
end
end
self
end
end


ary = [7,6,5,9,8,4,3,1,2,0]
ary.gnomesort!
22 changes: 22 additions & 0 deletions Sorting/Pancake Sorting/Ruby/pancakeSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Array
def pancake_sort!
num_flips = 0
(self.size-1).downto(1) do |end_idx|
max = self[0..end_idx].max
max_idx = self[0..end_idx].index(max)
next if max_idx == end_idx

if max_idx > 0
self[0..max_idx] = self[0..max_idx].reverse
p [num_flips += 1, self] if $DEBUG
end

self[0..end_idx] = self[0..end_idx].reverse
p [num_flips += 1, self] if $DEBUG
end
self
end
end

p a = (1..9).to_a.shuffle
p a.pancake_sort!
23 changes: 23 additions & 0 deletions Sorting/Radix Sort/Ruby/radixSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Array
def radix_sort(base=10)
ary = dup
rounds = (Math.log(ary.minmax.map(&:abs).max)/Math.log(base)).floor + 1
rounds.times do |i|
buckets = Array.new(2*base){[]}
base_i = base**i
ary.each do |n|
digit = (n/base_i) % base
digit += base if 0<=n
buckets[digit] << n
end
ary = buckets.flatten
p [i, ary] if $DEBUG
end
ary
end
def radix_sort!(base=10)
replace radix_sort(base)
end
end

p [170, 45, 75, 90, 2, 24, 802, 66].radix_sort
19 changes: 19 additions & 0 deletions Sorting/Selection Sort/Ruby/selSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def selection_sort(a, n)
(0..n - 2).each do |i|
i_min = i
(i + 1..n - 1).each do |j|
if a[j] < a[i_min]
i_min = j
end
end

temp = a[i]
a[i] = a[i_min]
a[i_min] = temp
#byebug
end
a
end

a = [2,7,4,1,5,3]
selection_sort(a, a.size)
111 changes: 111 additions & 0 deletions Tree/Binary Search Tree/Ruby/bst.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class BinarySearchTree
class Node
attr_reader :key, :left, :right
def initialize( key )
@key = key
@left = nil
@right = nil
end

def insert( new_key )
if new_key <= @key
@left.nil? ? @left = Node.new( new_key ) : @left.insert( new_key )
elsif new_key > @key
@right.nil? ? @right = Node.new( new_key ) : @right.insert( new_key )
end
end
end

def initialize
@root = nil
end

def insert( key )
if @root.nil?
@root = Node.new( key )
else
@root.insert( key )
end
end

def in_order(node=@root, &block)
return if node.nil?
in_order(node.left, &block)
yield node
in_order(node.right, &block)
end

def pre_order(node=@root, &block)
return if node.nil?
yield node
in_order(node.left, &block)
in_order(node.right, &block)
end

def post_order(node=@root, &block)
return if node.nil?
in_order(node.left, &block)
in_order(node.right, &block)
yield node
end

def search( key, node=@root )
return nil if node.nil?
if key < node.key
search( key, node.left )
elsif key > node.key
search( key, node.right )
else
return node
end
end

def check_height(node)
return 0 if node.nil?

leftHeight = check_height(node.left)
return -1 if leftHeight == -1

rightHeight = check_height(node.right)
return -1 if rightHeight == -1

diff = leftHeight - rightHeight
if diff.abs > 1
-1
else
[leftHeight, rightHeight].max + 1
end
end

def is_balanced?(node=@root)
check_height(node) == -1 ? false : true
end

end

tree = BinarySearchTree.new
tree.insert(50)
tree.insert(25)
tree.insert(75)
tree.insert(12)
tree.insert(37)
tree.insert(87)
tree.insert(63)
puts tree.inspect
puts "tree.is_balanced?"
puts tree.is_balanced?

puts "pre_order"
tree.pre_order do |node|
puts node.key
end

puts "in_order"
tree.in_order do |node|
puts node.key
end

puts "post_order"
tree.post_order do |node|
puts node.key
end