Merge pull request #47 from Doppon/fix-ruby-syntax

Fix syntax of ruby code, and Unify the naming convention with snake case
This commit is contained in:
Stepfen Shawn 2020-05-14 23:00:00 +08:00 committed by GitHub
commit 26a7610238
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 101 additions and 105 deletions

View file

@ -1,16 +1,16 @@
class Array
def sorted?
### goes thru array and checks if all elements are in order
for i in 1...self.length
return false if self[i-1] > self[i]
end
def sorted?
### goes thru array and checks if all elements are in order
for i in 1...self.length
return false if self[i-1] > self[i]
end
return true
end
def bogosort
### randomly shuffles until sorted
self.shuffle! until self.sorted?
return self #return sorted array
end
def bogosort
### randomly shuffles until sorted
self.shuffle! until self.sorted?
return self #return sorted array
end
end
puts "Enter a list of numbers seprated by space"

View file

@ -17,6 +17,6 @@ def bubble_sort(array)
end
puts "Enter a list of numbers seprated by space"
list = gets
list = gets
bubble_sort(list)
print list

View file

@ -2,32 +2,31 @@
Algorithm: Heap-Sort
Time-Complexity: O(nlogn)
"""
def heap_sort(array)
array_size = array.size
adjusted_array = [nil] + array
(array_size / 2).downto(1) do |i|
adjusted_down(adjusted_array, i, array_size)
end
while array_size > 1
adjusted_array[1], adjusted_array[array_size] = adjusted_array[array_size], adjusted_array[1]
array_size -= 1
adjusted_down(adjusted_array, 1, array_size)
end
adjusted_array.drop(1)
end
def heap_sort(array)
array_size = array.size
adjusted_array = [nil] + array
(array_size / 2).downto(1) do |i|
adjusted_down(adjusted_array, i, array_size)
end
while array_size > 1
adjusted_array[1], adjusted_array[array_size] = adjusted_array[array_size], adjusted_array[1]
array_size -= 1
adjusted_down(adjusted_array, 1, array_size)
end
adjusted_array.drop(1)
end
#Method to adjust heap in downward manner
def adjusted_down(adjusted_array, parent, limit)
top = adjusted_array[parent]
while (child = 2 * parent) <= limit
child += 1 if child < limit and adjusted_array[child] < adjusted_array[child + 1]
break if top >= adjusted_array[child]
adjusted_array[parent] = adjusted_array[child]
parent = child
end
adjusted_array[parent] = top
end
def adjusted_down(adjusted_array, parent, limit)
top = adjusted_array[parent]
while (child = 2 * parent) <= limit
child += 1 if child < limit and adjusted_array[child] < adjusted_array[child + 1]
break if top >= adjusted_array[child]
adjusted_array[parent] = adjusted_array[child]
parent = child
end
adjusted_array[parent] = top
end
#Code for testing heapsort
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].shuffle

View file

@ -12,6 +12,6 @@ def insertion_sort(array)
end
puts "Enter a list of numbers seprated by space"
list = gets
list = gets
insertion_sort(list)
print list

View file

@ -76,5 +76,4 @@ class SingleList
def isEmpty()
return (@head==nil)
end
end

View file

@ -1,23 +1,23 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Integer[]}
def inorder_traversal(root)
ans = []
def traverse(node, ans)
if node != nil
traverse(node.left, ans)
ans.push(node.val)
traverse(node.right,ans)
end
ans = []
def traverse(node, ans)
if node != nil
traverse(node.left, ans)
ans.push(node.val)
traverse(node.right, ans)
end
traverse(root,ans)
return ans
end
traverse(root, ans)
return ans
end

View file

@ -1,20 +1,20 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {TreeNode}
def invert_tree(root)
if root == nil
return nil
end
temp = root.left
root.left = invert_tree(root.right)
root.right = invert_tree(temp)
return root
if root == nil
return nil
end
temp = root.left
root.left = invert_tree(root.right)
root.right = invert_tree(temp)
return root
end

View file

@ -1,23 +1,23 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Integer[]}
def postorder_traversal(root)
ans = []
def traverse(node, ans)
if node != nil
traverse(node.left, ans)
traverse(node.right,ans)
ans.push(node.val)
end
ans = []
def traverse(node, ans)
if node != nil
traverse(node.left, ans)
traverse(node.right, ans)
ans.push(node.val)
end
traverse(root,ans)
return ans
end
traverse(root, ans)
return ans
end

View file

@ -1,23 +1,23 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Integer[]}
def preorder_traversal(root)
ans = []
def traverse(node, ans)
if node != nil
ans.push(node.val)
traverse(node.left, ans)
traverse(node.right,ans)
end
ans = []
def traverse(node, ans)
if node != nil
ans.push(node.val)
traverse(node.left, ans)
traverse(node.right, ans)
end
traverse(root,ans)
return ans
end
traverse(root, ans)
return ans
end

View file

@ -1,16 +1,14 @@
#https://en.wikipedia.org/wiki/Euclidean_algorithm
def euclidean_gcd(a, b)
while b != 0
t = b
b = a % b
a = t
end
return a
while b != 0
t = b
b = a % b
a = t
end
return a
end
puts "GCD(3, 5) = " + euclidean_gcd(3, 5).to_s
puts "GCD(3, 6) = " + euclidean_gcd(3, 6).to_s
puts "GCD(6, 3) = " + euclidean_gcd(6, 3).to_s

View file

@ -1,11 +1,11 @@
# Fisher and Yates Shuffle is one of the simplest and most popular shuffling algorithm
def fisher_yates_shuffle(array)
n = array.length
while n > 0
i = rand(n-=1)
array[i], array[n] = array[n], array[i]
end
return array
n = array.length
while n > 0
i = rand(n-=1)
array[i], array[n] = array[n], array[i]
end
return array
end
arr = [1, 2, 40, 30, 20, 15, 323, 12, 3, 4]