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 = "Jason Conrad"
string = "Turing"
string[0]
string = "abcdefghijklmnopqrstuvwxyz"
string[-1]
string = "Welcome to the Dungeon"
string[0...7]
string = "supercalifragilisticexpialidocious"
string.length
string_1 = "key"
string_2 = "board"
new_string = string_1 + string_2
variable = "no_things"
array = [1, 2, 3, 4, 5]
array = ["dog", "cat", "parrot"]
array[1]
array = ["dog", "cat", "parrot"]
array.length
array = ["Morgan", "Lee"]
array.index
array.push ("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.
array = [1, 2, 3, 4]
array.even?
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
array[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"]
]
array.pop
hash = {}
animals = {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"
my_variable = animals
hash = {kitten: "cat"}
hash.push(animals)
22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)
array = hash[0][0] + hash[1][0]
23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)
array = hash[0][1] + hash[1][1]
24. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing each name in ALL CAPS
array.upcase
25. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that are shorter than 5 characters
variable_1 = array[0]
variable_2 = array[1]
variable_3 = array[2]
variable_4 = array[3]
variable_1 + variable_2 + variable_3 + variable_4
27. Given the following list of variable names, circle those that are valid ruby local variable names
TARDIS
noapples
yes@height
no3_blind_mice
yessoda_or_pop
yes@@x
no$pizza
nobest_teacher
yes
28. Given the string "pizza"
, create an array containing the individual characters, capitalized (i.e. ["P", "I", "Z", "Z", "A"]
)
string.split
29. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that start with "J"
new_array = []
new_array = array.each do |arr|
new_array = arr if array[0] = "J"
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
string.replace "turing students love darkness"
32. Given the string "dogs and cats and parrots"
, replace all instances of the string "and"
with "or"
string.replace "dogs or cats or parrots"
array.to_s