Created
November 1, 2016 01:23
-
-
Save jonatanklosko/1e552a3e5c220e7e83f979111c2723dc to your computer and use it in GitHub Desktop.
Check if all the zeros of an array are at the end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def all_zeros_at_end(array) | |
array.drop_while(&:nonzero?).all?(&:zero?) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./all_zeros_at_end" | |
describe "all_zeros_at_end" do | |
it "returns true for an empty array" do | |
expect(all_zeros_at_end([])).to eq true | |
end | |
it "returns true for an array without zeros" do | |
expect(all_zeros_at_end([1, 2, 3])).to eq true | |
end | |
it "returns true for an array with one zero at the end" do | |
expect(all_zeros_at_end([1, 2, 3, 0])).to eq true | |
end | |
it "returns true for an array with many zeros at the end" do | |
expect(all_zeros_at_end([1, 2, 3, 0, 0])).to eq true | |
end | |
it "returns true for an array with zeros only" do | |
expect(all_zeros_at_end([0, 0, 0])).to eq true | |
end | |
it "returns false for an array with a zero in the middle" do | |
expect(all_zeros_at_end([1, 0, 2])).to eq false | |
end | |
it "returns false for an array with a zero at the beginning" do | |
expect(all_zeros_at_end([0, 1, 2])).to eq false | |
end | |
it "returns false for an array with a zero in the middle even if it ends with another zero" do | |
expect(all_zeros_at_end([1, 0, 2, 0, 0])).to eq false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment