Ruby basic coding - Arrays, Hashes and enumerables Here is the link for the inst
ID: 3875103 • Letter: R
Question
Ruby basic coding - Arrays, Hashes and enumerables
Here is the link for the instructions
https://cloudspace.idrsolutions.com:8181/HTML_Page_Extraction/output/69c5d1eb-d26c-4e50-8029-4bf0736b65d4/RubyBasics1/index.html?page=1
---------------------------------------------------------------------------
here are the files
insert the codes where it says "your code here"
rubybasics1.rb
# Lab 1
# Part I
def sum arr
#YOUR CODE HERE
end
# Part II
def max_2_sum arr
# YOUR CODE HERE
end
# Part III
def sum_to_n? arr, n
# YOUR CODE HERE
end
---------------------------------------------------------------------------------------------
here is the file which shows the data -- Do not write anything here
require 'rubybasics1.rb'
RSpec.configure do |config|
config.filter_run_excluding :disabled => true
end
describe 'Ruby Basics 1 Part I' do
describe "#sum" do
it "should be defined" do
expect { sum([1,3,4]) }.not_to raise_error
end
it "returns correct sum [20 points]" , points: 20 do
expect(sum([1,2,3,4,5])).to be_a_kind_of Fixnum
expect(sum([1,2,3,4,5])).to eq(15)
expect(sum([1,2,3,4,-5])).to eq(5)
expect(sum([1,2,3,4,-5,5,-100])).to eq(-90)
end
it "works on the empty array [10 points]" , points: 10 do
expect { sum([]) }.not_to raise_error
expect(sum([])).to be_zero
end
end
end
describe 'Ruby Basics 1 Part II' do
describe "#max_2_sum", :disabled => true do
it "should be defined" do
expect { max_2_sum([1,2,3]) }.not_to raise_error
end
it "returns the correct sum [7 points]" , points: 7 do
expect(max_2_sum([1,2,3,4,5])).to be_a_kind_of Fixnum
expect(max_2_sum([1,-2,-3,-4,-5])).to eq(-1)
end
it 'works even if 2 largest values are the same [3 points]' , points: 3 do
expect(max_2_sum([1,2,3,3])).to eq(6)
end
it "returns zero if array is empty [10 points]" , points: 10 do
expect(max_2_sum([])).to be_zero
end
it "returns value of the element if just one element [10 points]" , points: 10 do
expect(max_2_sum([3])).to eq(3)
end
end
end
describe 'Ruby Basics 1 Part III' do
describe "#sum_to_n", :disabled => true do
it "should be defined" do
expect { sum_to_n?([1,2,3],4) }.not_to raise_error
end
it "returns true when any two elements sum to the second argument [30 points]" , points: 30 do
expect(sum_to_n?([1,2,3,4,5], 5)).to be true # 2 + 3 = 5
expect(sum_to_n?([3,0,5], 5)).to be true # 0 + 5 = 5
expect(sum_to_n?([-1,-2,3,4,5,-8], -3)).to be true # handles negative sum
expect(sum_to_n?([-1,-2,3,4,5,-8], 12)).to be false # 3 + 4 + 5 = 12 (not 3 elements)
expect(sum_to_n?([-1,-2,3,4,6,-8], 12)).to be false # no two elements that sum
end
it "returns false for any single element array [5 points]" , points: 5 do
expect(sum_to_n?([0], 0)).to be false
expect(sum_to_n?([1], 1)).to be false
expect(sum_to_n?([-1], -1)).to be false
expect(sum_to_n?([-3], 0)).to be false
end
it "returns false for an empty array [5 points]" , points: 5 do
expect(sum_to_n?([], 0)).to be false
expect(sum_to_n?([], 7)).to be false
end
end
end
---------------------------------------------------------------------------------
Here are the quiz questions
1.) How many methods are there in the rubybasics1.rb file?
2.) How many describe blocks are in the RSpec definition file?
3.)
How many test cases are defined in the #sum collection?
How many test cases are defined in the #max_2_sum collection?
How many test cases are defined in the #sum_to_n collection?
4.)
Select ALL that apply. Which collections will be excluded when we run the rspec tests?
5.)
You can easily find the rspec tests that failed as the output lists both the line number and the file name.
False
6.) Upload a screenshot of your passing rspec tests for all methods up to and including the sum_to_n? method.
#sumExplanation / Answer
For the first part of the question where you ask to write the code.
Programs are very easy to write in ruby because of Ruby's Enumerable methods.
The Enumerable mixin provides collection classes with several traversals and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection.
The program you require-
# Lab 1
# Part I
def sum arr
end
# Part II
def max_2_sum arr
return(arr.sort.last(2).sum) #rails method
end
# Part III
def sum_to_n? arr, n
end
1) there are 3 methods in the rubybasics1.rb, namely sum arr, max_2_sum, sum_2_n?
2) there are 4 describe blocks 1 for the rubybasics1 file and 3 for the methods
3)test cases in #sum collection-6
test cases in the #max_2_sum collection-5
test cases in the #sum_to_n collection-11
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.