Skip to content

Instantly share code, notes, and snippets.

@voleinikov
Created January 10, 2013 09:30
Show Gist options
  • Save voleinikov/4500748 to your computer and use it in GitHub Desktop.
Save voleinikov/4500748 to your computer and use it in GitHub Desktop.
w1d3 Code Review
class Array
# In methods, my_map, my_inject, and my_select, you should have just used your my_each method instead of using the while loop. Would make slightly cleaner results, plus you made it so you might as well use it!
def my_map(&proc)
i = 0
new_array = []
while i < self.length
new_array << proc.call(self[i])
i += 1
end
new_array
end
# Was my_map_yield added later? We don't have this one in our code
def my_map_yield
i = 0
new_array = []
while i < self.length
new_array << yield(self[i])
i += 1
end
new_array
end
def my_each(&proc)
i = 0
while i < self.length
proc.call(self[i])
i+=1
end
self
end
def my_inject(result, &proc)
i = 0
while i < self.length
result = proc.call(result, self[i])
i += 1
end
result
end
def my_select
i = 0
new_array = []
while i < self.length
new_array << self[i] if yield(self[i])
i+=1
end
new_array
end
def my_sort
self.size.times do |variable|
self.each_with_index do |element, index|
unless self[index+1].nil?
result = yield(element, self[index+1])
if result==1 || result == true # "result == 1" to accomodate <=> operator
temp = self[index+1]
self[index+1] = element
self[index] = temp
end
end
end
end
self
end
end
#------------- BLOCKS --- AND --- PROCS ----------------
def my_block(*arg, &proc)
if proc.nil?
puts "NO BLOCK GIVEN!"
else
proc.call(arg)
end
end
def my_block2(str1, str2, str3, &proc)
if proc.nil?
puts "NO BLOCK GIVEN!"
else
proc.call(str1, str2, str3)
end
end
#------------------------------------------------------
=begin
**********************
* Various Test Cases *
**********************
test = [1, 2, 3, 4, 5]
test2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
test3 = [9, 5, 7, 3, 10, 2, 4, 1]
#p test.my_map {|x| x * 2}
#puts "real version"
#p test.each {|x| puts x}
#puts "our version"
#p test.my_each {|x| puts x}
# puts "real version"
# p test.inject(1) {|result, element| result + element}
# p test.inject(2) {|result, element| result - element}
# p test.inject(3) {|result, element| result * element}
# puts "our version"
# p test.my_inject(1) {|result, element| result + element}
# p test.my_inject(2) {|result, element| result - element}
# p test.my_inject(3) {|result, element| result * element}
# puts "real version"
# p test.select {|x| x % 2 == 1}
# puts "our version"
# p test.my_select {|x| x % 2 == 1}
puts "real version"
p test2.sort
puts "our version"
p test2.my_sort {|x, y| x > y}
p test3.my_sort {|x, y| x <=> y}
p test3.my_sort {|x, y| y <=> x}
------------------------------------------------------
p my_block("cookies", "ice cream", "cash") {|str1, str2, str3| puts "I like #{str1}, #{str2}, and #{str3}."}
p my_block("cookies", "ice cream") {|str1, str2, str3| puts "I like #{str1}, #{str2}, and #{str3}."}
=end
# Not too many suggestions to make, except that recursion being hard to follow as it is, maybe some more comments would have been helpful (especially in your merge sort implementation)
def fib_recursive(size)
if size == 1
return 1
elsif size == 2
return 1
else
return fib_recursive(size-1) + fib_recursive(size-2)
end
end
def print_fibonacci(size)
puts "Which type? (Recursive = 1, Iterative = 2)"
type = gets.chomp
fib_array = []
(1..size).each do |s|
if (type == "1")
fib_array << fib_recursive(s)
else
fib_array << fib_iterative(s)
end
end
p fib_array
end
def fib_iterative(size)
if size == 1
return 1 # I thought fib started with 0,1 ?
elsif size == 2
return 1
end
x = 1
y = 1
result = 0
(3..size).each do
result = x + y
x = y
y = result
end
result
end
def array_sum_iter(input_array)
result = 0
input_array.each do |element|
result+=element
end
result
end
def array_sum_recursive(input_array)
if input_array.size == 1
return input_array[0]
else
return input_array[0] + array_sum_recursive(input_array[1..-1])
end
end
def binary_search_recursive(input_array, target, start_point = 0, end_point = input_array.size-1)
mid_point = (start_point + end_point) / 2
if(start_point==end_point && input_array[mid_point] != target)
raise "Target not found in input array."
end
if(input_array[mid_point] == target)
return mid_point
elsif(input_array[mid_point] > target)
return binary_search_recursive(input_array, target, start_point, mid_point)
else
return binary_search_recursive(input_array, target, mid_point+1, end_point)
end
end
def binary_search_iterative(input_array, target)
start_point = 0
end_point = input_array.length - 1
mid_point = (start_point + end_point) / 2
counter = 0
while input_array[mid_point] != target
if counter > input_array.size
raise "Target not found in input array."
end
if input_array[mid_point] > target
end_point = mid_point
mid_point = (start_point + mid_point) / 2
else
start_point = mid_point
mid_point = (mid_point+1 + end_point) / 2
end
counter += 1
end
mid_point
end
def exp1(base, exponent)
if exponent == 0
return 1
else
return base * exp1(base, exponent -1)
end
end
def exp2(base, exponent)
if exponent == 0
return 1
elsif exponent == 1
return base
else
return exp2(base, (exponent/2.0).floor) * exp2(base, (exponent/2.0).ceil)
end
end
class Array
def deep_dup
new_array = []
self.each do |element|
if (element.is_a?(Array))
new_array << element.deep_dup
else
new_array << element
end
end
new_array
end
end
def merge_sort(input_array)
if input_array.size == 1
return input_array
else
mid_point = input_array.size / 2
first_half = merge_sort(input_array[0...mid_point])
second_half = merge_sort(input_array[mid_point..-1])
return merge(first_half,second_half)
end
end
def merge(input_array1, input_array2)
sorted_array = []
(input_array1.size + input_array2.size).times do
if(input_array1.empty?)
sorted_array << input_array2.shift
elsif(input_array2.empty?)
sorted_array << input_array1.shift
elsif(input_array1[0] < input_array2[0])
sorted_array << input_array1.shift
else
sorted_array << input_array2.shift
end
end
sorted_array
end
=begin
**********************
* Various Test Cases *
**********************
p exp2(2, 2)
p exp2(3, 3)
p exp2(2, 5)
p exp2(2, 6)
def test(input_array)
input_array.each do |element|
p binary_search_recursive(input_array, element)
end
p binary_search_recursive(input_array, 1000)
end
test_odd = [1, 2, 3, 4, 5]
test_even1 = [1, 2]
test_even2 = [1, 2, 3, 4]
test_even3 = [1, 2, 3, 4, 5, 6]
test_even4 = [1, 2, 3, 4, 5, 6, 7, 8]
test(test_odd)
test(test_even1)
test(test_even2)
test(test_even3)
test(test_even4)
# p fib_iterative(1)
p fib_iterative(2)
p fib_iterative(3)
p fib_iterative(4)
p fib_iterative(5)
print_fibonacci(5)
test = [1, 2, 3, 4, 5]
p array_sum_iter(test)
p array_sum_recursive(test)
p binary_search_recursive(test_odd, 4)
p binary_search_recursive(test_odd, 2)
p binary_search_recursive(test_even1, 2)
p binary_search_recursive(test_even1, 1)
puts
p binary_search_recursive(test_even2, 4)
p binary_search_recursive(test_even2, 3)
p binary_search_recursive(test_even2, 2)
p binary_search_recursive(test_even2, 1)
puts
p binary_search_recursive(test_even3, 6)
p binary_search_recursive(test_even3, 5)
p binary_search_recursive(test_even3, 4)
p binary_search_recursive(test_even3, 3)
p binary_search_recursive(test_even3, 2)
p binary_search_recursive(test_even3, 1)
puts
p binary_search_recursive(test_even4, 8)
p binary_search_recursive(test_even4, 7)
p binary_search_recursive(test_even4, 6)
p binary_search_recursive(test_even4, 5)
p binary_search_recursive(test_even4, 4)
p binary_search_recursive(test_even4, 3)
p binary_search_recursive(test_even4, 2)
p binary_search_recursive(test_even4, 1)
p binary_search_recursive(test2, 8)
p binary_search_recursive(test2, 7)
p binary_search_recursive(test2, 6)
p binary_search_recursive(test2, 5)
p binary_search_recursive(test2, 4)
p binary_search_recursive(test2, 3)
p binary_search_recursive(test2, 2)
p binary_search_recursive(test2, 1)
p binary_search_recursive(test3, 4)
p binary_search_recursive(test3, 3)
p binary_search_recursive(test3, 2)
p binary_search_recursive(test3, 1)
p binary_search_recursive(test3, 5)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment