Last active
August 29, 2015 14:18
-
-
Save PamBWillenz/9acbf288141bd14a3c9a to your computer and use it in GitHub Desktop.
Arrays - Code Checkpoint
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 new_array(a,b,c,d) | |
# return an array consisting of the arguments here | |
end | |
SPECS | |
describe "new_array" do | |
it "creates an array of numbers" do | |
expect( new_array(1,2,3,4) ).to eq([1,2,3,4]) | |
end | |
it "creates an array of strings" do | |
expect( new_array("a", "b", "c", "d") ).to eq(["a", "b", "c", "d"]) | |
end | |
it "creates an array of non-sequential objects" do | |
expect( new_array(1,4,2,3) ).to eq([1,4,2,3]) | |
end | |
end | |
CODE | |
def new_array(a, b, c, d) | |
new_array = [a,b,c,d] | |
end | |
numbers = [1,2,3,4] | |
strings = ["a", "b", "c", "d"] | |
objects = [1,4,2,3] | |
OUTPUT | |
new_array creates an array of numbers | |
new_array creates an array of strings | |
new_array creates an array of non-sequential objects | |
def first_and_last(a) | |
# return an array with only the first and last elements of the argument | |
end | |
SPEC | |
describe "first_and_last" do | |
it "creates new array with numbers" do | |
expect( first_and_last([1,2,3]) ).to eq([1,3]) | |
end | |
it "creates new array with strings" do | |
expect( first_and_last(["a", "b", "c", "d"]) ).to eq(["a", "d"]) | |
end | |
end | |
CODE | |
def first_and_last(a) | |
first_and_last = [a.first, a.last] | |
end | |
numbers = [1, 2, 3] | |
letters = ["a", "b", "c", "d"] | |
OUTPUT | |
first_and_last creates new array with numbers | |
first_and_last creates new array with strings |
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
reverse_plus_one! | |
def reverse_plus_one!(a) | |
a << a.first | |
# return the array, reversed | |
end | |
SPECS | |
describe "reverse_plus_one!" do | |
it "returns an altered array" do | |
ary = [1,2] | |
expect( reverse_plus_one!(ary) ).to eq([1,2,1]) | |
expect( ary ).to eq([1,2,1]) | |
end | |
it "works on longer arrays" do | |
ary = [1,2,3,4] | |
expect( reverse_plus_one!(ary) ).to eq([1,4,3,2,1]) | |
expect( ary ).to eq([1,4,3,2,1]) | |
end | |
end | |
CODE | |
def reverse_plus_one!(a) | |
a << a.first | |
a.reverse! | |
end | |
ary = [1,2] | |
ary = [1,2,3,4] | |
pluses_everywhere! | |
[ "a", "b", "c" ].join | |
#=> "abc" | |
[ "a", "b", "c" ].join("-") | |
#=> "a-b-c" | |
def pluses_everywhere(a) | |
# convert the array argument to a string with a "+" between each element | |
end | |
SPECS | |
describe "pluses_everywhere" do | |
it "returns a single plus for a short array" do | |
expect( pluses_everywhere([1,2]) ).to eq("1+2") | |
end | |
it "returns pluses for longer arrays" do | |
expect( pluses_everywhere([1,2,3,4,5]) ).to eq("1+2+3+4+5") | |
end | |
end | |
CODE | |
def pluses_everywhere(a) | |
a.join("+") | |
end | |
pluses_everywhere = [1,2] | |
pluses_everywhere = [1,2,3,4,5] | |
array_quantity_plus_one | |
[ 1, 2, 3, 4, 5 ].length | |
#=> 5 | |
def array_quantity_plus_one(a) | |
# return the number of elements in the array argument, plus 1. | |
end | |
SPECS | |
describe "array_quantity_plus_one" do | |
it "returns the number of items for a short array plus one" do | |
expect( array_quantity_plus_one([1,2,3]) ).to eq(4) | |
end | |
it "returns the number of items for a long array plus one" do | |
expect( array_quantity_plus_one([1,532,23,35,52,39]) ).to eq(7) | |
end | |
end | |
CODE | |
def array_quantity_plus_one(a) | |
a.length + 1 | |
end | |
array_quantity_plus_one = [1,2,3] | |
array_quantity_plus_one = [1,532,23,35,52,39] |
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
ADD_ITEM! | |
SPEC | |
describe "add_item!" do | |
# it "adds an item to the end of the list" do | |
# expect( add_item!("banana", ["orange"]) ).to eq(["orange", "banana"]) | |
# end | |
# it "doesn't add an item if the list already has it" do | |
# expect( add_item!("orange", ["orange"]) ).to eq(["orange"]) | |
# end | |
# end | |
CODE | |
def add_item!(item, list) | |
if !(list.include?(item)) | |
list << item | |
else | |
list | |
end | |
end | |
add_item!("banana", ["orange"]) | |
add_item!("orange", ["orange"]) | |
REMOVE_ITEM! | |
SPEC | |
describe "remove_item!" do | |
# it "removes an item if it's in the list" do | |
# expect( remove_item!("orange", ["banana", "orange"]) ).to eq(["banana"]) | |
# end | |
# it "removes an item if it's in the list" do | |
# expect( remove_item!("banana", ["banana", "orange"]) ).to eq(["orange"]) | |
# end | |
# it "doesn't remove an item if it's not in the list" do | |
# expect( remove_item!("apple", ["banana", "orange"]) ).to eq(["banana", "orange"]) | |
# end | |
# end | |
CODE | |
def remove_item!(item, list) | |
list.delete(item) | |
list | |
end | |
remove_item!("orange", ["banana", "orange"]) | |
remove_item!("banana", ["banana", "orange"]) | |
remove_item!("apple", ["banana", "orange"]) | |
SHOW_LIST | |
SPEC | |
describe "show_list" do | |
# it "returns a list sorted" do | |
# list = %w{3 1 2} | |
# list_sorted = %w{1 2 3} | |
# expect( show_list(list) ).to eq(list_sorted) | |
# end | |
# it "doesn't modify the original list" do | |
# list = %w{banana orange apple} | |
# duplicate = list.dup | |
# list_sorted = %w{apple banana orange} | |
# expect( show_list(list) ).to eq(list_sorted) | |
# expect( list ).to eq( duplicate ) | |
# end | |
# it "returns a list sorted without duplicates" do | |
# list = %w{banana orange apple orange} | |
# list_sorted = %w{apple banana orange} | |
# expect( show_list(list) ).to eq(list_sorted) | |
# end | |
# end | |
CODE | |
def show_list(list) | |
list.uniq.sort | |
end | |
list = ["orange", "banana", "apple"] | |
list = [3,1,2] | |
list = ["apple", "apple", "banana"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment