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

Arrays #65

Open
wants to merge 4 commits into
base: arrays
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions array_problems/arrays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@

module ArrayUtil
def self.max(array)
# if array.empty? dont' need
# return nil
# else
max = array.first
array.each do |x|
if x >= max
max = x
end
end
end
return max
end

def self.middle_element(array)
return nil if array.empty?
mid = array.length / 2
if array.length.even?
return (array[mid] + array[mid - 1]) / 2.0
else
return array[mid]
end
end
end

def self.sum_arrays(array1, array2)
solution = [array1.each_index { |x| array1[x] + array2[x]}]
solution
end
end
1 change: 1 addition & 0 deletions array_problems/spec/arrays_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
expect(ArrayUtil.max([-1, -5, -10, -2])).to eq(-1)
end
end
end

describe ".middle_element" do
it "should return nil for an empty array" do
Expand Down
61 changes: 61 additions & 0 deletions set1/set1.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
module Set1
def self.swap_small(array)
if array.length == 1
return array
else
min_num = array.first
array.each do |x|
if x <= min_num
min_num = x
end
end
array[array.index(min_num)] = array.first
array[0] = min_num
array
end
end

# def self.find_sum_2(array, sum = 0)
# if array.length == 0
# return false
# elsif array.include?(0)
# return true
# elsif array.each { |x| array.include?(x) && array.include?(-x) }
# return true
# else # Can not get this one to work!!!
# array.each { |x| sum += x }
# if sum.equ?(0)
# return false
# else
# return true
# end
# end
# end

def self.find_sum_2(array, sum = 0)
array.each do |elem1|
array.each do |elem2|
if elem1 + elem2 == sum
return true
end
end
end
false
end

def self.fin_sum_2(array, sum = 0)
hash = {}
array.each do |elem1|
hash[elem1] = true
end
array.each do |elem1|
if hash[sum - elem1]
return true
end
end
false
end

def self.find_sum_3(array)
array.each do |elem1|
array.each do |elem2|
array.each do |elem3|
if elem1 + elem2 + elem3 == 0
return true
end
end
end
end
false
end
end
12 changes: 6 additions & 6 deletions set1/spec/set1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
end

describe ".find_sum_2" do
xit "should return false for an empty array" do
it "should return false for an empty array" do
expect(Set1.find_sum_2([])).to eq(false)
end

xit "should return true for an array with just the number 0" do
it "should return true for an array with just the number 0" do
expect(Set1.find_sum_2([0])).to eq(true)
end

xit "should return true for an array with the number 0 in it" do
it "should return true for an array with the number 0 in it" do
expect(Set1.find_sum_2([5, 2, 0, -100])).to eq(true)
end

xit "should return true if a number and it's negative are in the arrray" do
it "should return true if a number and it's negative are in the arrray" do
expect(Set1.find_sum_2([5, 20, -5, 100])).to eq(true)
expect(Set1.find_sum_2([5, 20, -3, 100, -20, 2])).to eq(true)
end
Expand All @@ -47,11 +47,11 @@
end

describe ".find_sum_3" do
xit "should return false for an empty array" do
it "should return false for an empty array" do
expect(Set1.find_sum_3([])).to eq(false)
end

xit "should return true for an array with just the number 0" do
it "should return true for an array with just the number 0" do
expect(Set1.find_sum_3([0])).to eq(true)
end

Expand Down