This exercise is intended to help you assess your progress with the concepts and techniques we've covered during the week.
For these questions, write a short snippet of code that meets
the requirement. Fill in your answers on a second sheet of paper or in your
notebook. In cases where the question mentions a "given"
data value, use the variable given
to refer to it (instead of re-writing
the information).
name = "Erin"
given.first
given.last
given.find("Welcome")
given.length
given.
new_string = given_1 + given_2
no_things = []
array = [1, 2, 3, 4, 5]
given[1]
given.length
students = ["Alisher", "Dave"]
students.each do |name|
print name
end
students << "Jeff"
15. Given the array [1,2,3,4]
, return a new array of only the even numbers. Then an array of only the odd numbers.
`given.each do |number|`
`number.even?`
`end`
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
given[0][1]
17. Given the following array, sort the list alphabetically by the names (i.e. the first element of each sub-array)
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
`given.sort`
given.pop
hash = {}
new_hash = {"puppy" => "dog"}
21. Assign the hash from 20 to a variable. Then add to it a new key "kitten" which points to the value "cat"
animals = new_hash
animals.new("kitten", "cat")
22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)
animal_array = [animals.key]
23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)
animal_array = [animals.values]
24. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing each name in ALL CAPS
new = given.map do |name|
name.upcase
end
25. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that are shorter than 5 characters
new = given.each do |name|
name.length < 5
end
total = 0
given.each do |i|
total = total + i
end
27. Given the following list of variable names, circle those that are valid ruby local variable names
TARDIS
- validapples
- valid@height
- valid3_blind_mice
soda_or_pop
-valid@@x
$pizza
best_teacher
-valid
28. Given the string "pizza"
, create an array containing the individual characters, capitalized (i.e. ["P", "I", "Z", "Z", "A"]
)
given.slice.upcase
29. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that start with "J"
new = given.map do |name|
name.start_with("J")
end
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
given[2][0]
given.gsub("sunlight", "darkness")
32. Given the string "dogs and cats and parrots"
, replace all instances of the string "and"
with "or"
given.gsub("and", "or")
"#{given[0]} + " and " + #{given[1]} + " and " + #{given[2]}"