Skip to content

Arrays #67

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

Open
wants to merge 6 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
14 changes: 14 additions & 0 deletions array_problems/arrays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@

module ArrayUtil
def self.max(array)
biggest= array[0]
array.each_with_index do |x, i|
if array[(i+1)] > biggest
biggest = x
binding.pry
end
end
biggest
end

def self.middle_element(array)
if array.length % 2 == 0
#array is even length
array.each_with_index do |x,i|

end

end

def self.sum_arrays(array1, array2)
Expand Down
69 changes: 68 additions & 1 deletion set1/set1.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,77 @@
module Set1
def self.swap_small(array)
def self.swap_small(array) #darrell & Jimmy
#.each_index
if array.count == 1
return array
end
smallest_number = array[0]
smallest_index = 0
array.each_with_index do |value, index|
if value < smallest_number
smallest_number = value
smallest_index = index
end
end
if smallest_number == array[0]
return array
end
array.delete_at(smallest_index)
temp = array[0]
array.delete(array[0])
array.unshift(smallest_number)
array.insert(smallest_index, temp)
array
end

def self.find_sum_2(array, sum = 0)
#Given an array of numbers, find out if there are two numbers in
#the array that add up to 0 (just return true or false). You can use
#the same number multiple times.
if array.count == 0
false
elsif array.include?(0)
true
elsif array[0] == 0
true
else
flag = false
array.each do |x|
array.each do |y|
if x + y == sum
flag = true #inefficient because it will continue to run
#even after the conditions has been satisified until
#it hits the end
end
end
end
flag
end
end

def self.find_sum_3(array)
if array.count == 0
false
elsif array.include?(0)
true
elsif array[0] == 0
true
else
flag = false
array.each do |x|
array.each do |y|
array.each do |z|
sum = x + y + z
if sum == 0
flag = true
end
end
end
end
flag
end
end
end
#WHILE WORKING THROUGH THESE TYPES OF PROBLEMS. Think about how
#we are thinking about them. Examine our own thought process
#because we will be evaluated on that during interviews.

20 changes: 10 additions & 10 deletions set1/spec/set1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@
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

xit "should return false if none of the numbers add to 0" do
it "should return false if none of the numbers add to 0" do
expect(Set1.find_sum_2([5, 6, 7, 8, -1, -2, -3, -4])).to eq(false)
end
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

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_3([5, 2, 0, -100])).to eq(true)
end

xit "should return true if 3 numbers in the array add to 0" do
it "should return true if 3 numbers in the array add to 0" do
expect(Set1.find_sum_3([10, 2, 100, -200, -102, 5])).to eq(true)
expect(Set1.find_sum_3([10, -51, 100, -201, 102, 5])).to eq(true)
expect(Set1.find_sum_3([10, 51, 100, -201, -102, 5])).to eq(true) # 51, 51, -102
end

xit "should return false if no 3 numbers in the array add to 0" do
it "should return false if no 3 numbers in the array add to 0" do
expect(Set1.find_sum_3([10, 51, 100, 201, 102, 5])).to eq(false)
end
end
Expand Down