Skip to content

Commit

Permalink
arrays exercises: completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay committed Jan 10, 2024
1 parent 94b3203 commit 505be75
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
17 changes: 16 additions & 1 deletion ruby_basics/6_arrays/exercises/array_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
def nil_array(number)
Array.new(number)
# return an array containing `nil` the given number of times
end

def first_element(array)
array.first
# return the first element of the array
end

def third_element(array)
array[2]
# return the third element of the array
end

def last_three_elements(array)
array.last(3)
# return the last 3 elements of the array
end

def add_element(array)
array.push(20)
# add an element (of any value) to the array
end

def remove_last_element(array)
x = array.pop
# Step 1: remove the last element from the array

# Step 2: return the array (because Step 1 returns the value of the element removed)
Expand All @@ -27,35 +33,44 @@ def remove_last_element(array)

def remove_first_three_elements(array)
# Step 1: remove the first three elements

array.shift
array.shift
array.shift
# Step 2: return the array (because Step 1 returns the values of the elements removed)
array
end

def array_concatenation(original, additional)
original.concat(additional)
# return an array adding the original and additional array together
end

def array_difference(original, comparison)
original - comparison
# return an array of elements from the original array that are not in the comparison array
end

def empty_array?(array)
array.empty?
# return true if the array is empty
end

def reverse(array)
array.reverse
# return the reverse of the array
end

def array_length(array)
array.length
# return the length of the array
end

def include?(array, value)
array.include?(value)
# return true if the array includes the value
end

def join(array, separator)
array.join(separator)
# return the result of joining the array with the separator
end
56 changes: 28 additions & 28 deletions ruby_basics/6_arrays/spec/array_exercises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,152 +9,152 @@
end

# remove the 'x' from the line below to unskip the test
xit 'returns an array containing 2 nil values' do
it 'returns an array containing 2 nil values' do
expect(nil_array(2)).to eq([nil, nil])
end
end

describe 'first element exercise' do

xit 'returns the first element of an array of numbers' do
it 'returns the first element of an array of numbers' do
expect(first_element([2, 4, 6, 8, 10])).to eq(2)
end

xit 'returns the first element of an array of strings' do
it 'returns the first element of an array of strings' do
expect(first_element(['foo', 'bar'])).to eq('foo')
end
end

describe 'third element exercise' do

xit 'returns the third element of an array of numbers' do
it 'returns the third element of an array of numbers' do
expect(third_element([2, 4, 6, 8, 10])).to eq(6)
end

xit 'returns nil if the array does not have a third element' do
it 'returns nil if the array does not have a third element' do
expect(third_element(['foo', 'bar'])).to eq(nil)
end
end

describe 'last three elements exercise' do

xit 'returns an array of the last three elements' do
it 'returns an array of the last three elements' do
expect(last_three_elements([2, 4, 6, 8, 10])).to eq([6, 8, 10])
end
end

xit 'returns all of the elements when there are less than 3 elements' do
it 'returns all of the elements when there are less than 3 elements' do
expect(last_three_elements(['foo', 'bar'])).to eq(['foo', 'bar'])
end
end

describe 'add element exercise' do

xit 'increases the length of an array by 1' do
it 'increases the length of an array by 1' do
numbers = [1, 2, 3, 4]
expect { add_element(numbers) }.to change { numbers.length }.by(1)
end

xit 'increases the length of an empty array by 1' do
it 'increases the length of an empty array by 1' do
data = []
expect { add_element(data) }.to change { data.length }.by(1)
end
end

describe 'remove last element exercise' do

xit 'returns the array without the last element' do
it 'returns the array without the last element' do
expect(remove_last_element([1, 3, 5])).to eq([1, 3])
end

xit 'returns an empty array when the array only has one element' do
it 'returns an empty array when the array only has one element' do
expect(remove_last_element(['foo'])).to eq([])
end
end

describe 'remove first three elements exercise' do

xit 'returns the array without the first three elements' do
it 'returns the array without the first three elements' do
expect(remove_first_three_elements([1, 3, 5, 7, 9])).to eq([7, 9])
end

xit 'returns an empty array when the array has less than 3 elements' do
it 'returns an empty array when the array has less than 3 elements' do
expect(remove_first_three_elements(['foo', 'bar'])).to eq([])
end
end

describe 'array concatenation exercise' do

xit 'returns an array adding two arrays of numbers together' do
it 'returns an array adding two arrays of numbers together' do
expect(array_concatenation([1, 3, 5], [2, 4, 6])).to eq([1, 3, 5, 2, 4, 6])
end

xit 'returns an array adding arrays of strings and numbers together' do
it 'returns an array adding arrays of strings and numbers together' do
expect(array_concatenation(['a', 'b', 'c'], [1, 2, 3])).to eq(['a', 'b', 'c', 1, 2, 3])
end
end

describe 'array difference exercise' do

xit 'returns an array subtracting two arrays of numbers' do
it 'returns an array subtracting two arrays of numbers' do
expect(array_difference([0, 1, 1, 2, 3, 5], [0, 1, 2])).to eq([3, 5])
end

xit 'returns an array subtracting two arrays of strings' do
it 'returns an array subtracting two arrays of strings' do
expect(array_difference(['foo', 'bar', 'baz'], ['bar','hello'])).to eq(['foo', 'baz'])
end
end

describe 'empty array exercise' do

xit 'returns true when the array is empty' do
it 'returns true when the array is empty' do
expect(empty_array?([])).to be true
end

xit 'returns false when the array is not empty' do
it 'returns false when the array is not empty' do
expect(empty_array?([1, 2, 3])).to be false
end
end

describe 'reverse exercise' do

xit 'returns an array containing the elements in reverse order' do
it 'returns an array containing the elements in reverse order' do
expect(reverse([0, 1, 1, 2, 3, 5])).to eq([5, 3, 2, 1, 1, 0])
end

xit 'returns an array containing the element when there is only one' do
it 'returns an array containing the element when there is only one' do
expect(reverse(['foo'])).to eq(['foo'])
end
end

describe 'array length exercise' do

xit 'returns the length of the array' do
it 'returns the length of the array' do
expect(array_length([0, 1, 1, 2, 3, 5])).to eq(6)
end

xit 'returns zero when the array is empty' do
it 'returns zero when the array is empty' do
expect(array_length([])).to eq(0)
end
end

describe 'include exercise' do

xit 'returns true when the array contains the specified value' do
it 'returns true when the array contains the specified value' do
expect(include?([0, 1, 1, 2, 3, 5], 3)).to be true
end

xit 'returns false when the array does not contain the specified value' do
it 'returns false when the array does not contain the specified value' do
expect(include?([0, 1, 1, 2, 3, 5], 8)).to be false
end
end

describe 'join exercise' do

xit 'returns a string joining an array of numbers with " + "' do
it 'returns a string joining an array of numbers with " + "' do
expect(join([0, 1, 1, 2, 3, 5], ' + ')).to eq('0 + 1 + 1 + 2 + 3 + 5')
end

xit 'returns a string joining an array of strings with " "' do
it 'returns a string joining an array of strings with " "' do
expect(join(['foo', 'bar', 'baz'], ' ')).to eq('foo bar baz')
end
end
Expand Down

0 comments on commit 505be75

Please sign in to comment.