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).
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.
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
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"]
]
21. Assign the hash from 20 to a variable. Then add to it a new key "kitten" which points to the value "cat"
22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)
23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)
24. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing each name in ALL CAPS
25. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that are shorter than 5 characters
27. Given the following list of variable names, circle those that are valid ruby local variable names
TARDIS
apples
@height
3_blind_mice
soda_or_pop
@@x
$pizza
best_teacher
28. Given the string "pizza"
, create an array containing the individual characters, capitalized (i.e. ["P", "I", "Z", "Z", "A"]
)
29. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that start with "J"
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
Create a variable called name and assign it to a string containing your name.
name = "Alisher"
Given the string "Turing", retrieve the first character
"Turing"[0]
Given the string "abcdefghijklmnopqrstuvwxyz", retrieve the last character
"abcdefghijklmnopqrstuvwxyz"[-1]
Given the string "Welcome to the Dungeon", retrieve the substring "Welcome"
"Welcome to the Dungeon"[0..6]
Find the number of characters in the string "supercalifragilisticexpialidocious"
"supercalifragilisticexpialidocious".length
Find the number of words in the string "Coding is gr8"
truth = "Coding is gr8"
word_count = truth.split.count
Given 2 strings, "key" and "board", create a new string by concatenaing them together
"key" + "board"
Create a variable called no_things and assign it to an empty array
no_things = []
Create an array of the numbers 1 to 5
numbers = [1, 2, 3, 4, 5]
Given the array ["dog", "cat", "parrot"], retrieve the second element ("cat")
animals = ["dog", "cat", "parrot"]
animals[1]
Given the array ["dog", "cat", "parrot"], find how many elements appear in the array
animals = ["dog", "cat", "parrot"]
animals.count
Create an array containing strings of the names of the 2 students sitting closest to you.
names = ["Anna", "Daniel"]
Given the array from number 12, print the name of each student.
names = ["Anna", "Daniel"]
puts names
Given the array from number 12, add the name "Jeff" to the end of the list
names << "Jeff" or names.push("Jeff")
Given the array [1,2,3,4], return a new array of only the even numbers. Then an array of only the odd numbers.
numbers = [1,2,3,4]
even_numbers = numbers.map do |number|
number.even?
end
odd_numbers = numbers.map do |number|
number.odd?
end
Given the following array, retrieve the second element of the first nested array
people = [
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
people[0][1]
17. Given the following array, sort the list alphabetically by the names (i.e. the first element of each sub-array)
people = [
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
people.sort
18. Given the array ["pizza", "casserole", "pie"], remove the last element from the array
things = ["pizza", "casserole", "pie"]
things.pop
19. Create an empty hash
dictionary = {}
20. Create a hash which associates the key "puppy" with the value "dog"
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"
animals = {"puppy" => "dog"}
animals.store("kitten", "cat")
22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)
animals.keys
23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)
animals.values
24. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing each name in ALL CAPS
associates = ["Jeff", "Horace", "Josh", "Joanne"]
associates_caps = associates.map do |associate|
associate.capitalize
end
25. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing only the names that are shorter than 5 characters
associates = ["Jeff", "Horace", "Josh", "Joanne"]
associates_caps = associates.map do |associate|
associate if associate.length < 5
end
26. Given the array [1,2,3,4], add up all the numbers in the list
numbers = [1,2,3,4]
sum = 0
numbers.each do |number|
sum += number
end
return sum
27. Given the following list of variable names, circle those that are valid ruby local variable names
TARDIS
apples => valid
@height
3_blind_mice
soda_or_pop
@@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"])
word = "pizza"
word.upcase.chars or word.upcase.split("")
29. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing only the names that start with "J"
associates = ["Jeff", "Horace", "Josh", "Joanne"]
associates_with_j = associates.map do |associate|
associate if associate.start_with?("J")
end
30. Given the following array, retrieve the name of the first instructor who teaches "Module 2"
instructors = [
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
instructors[2][0]
31. Given the string "turing students love sunlight" replace "sunlight" with "darkness"
truth = "turing students love sunlight"
deception = truth.sub("sunlight", "darkness")
32. Given the string "dogs and cats and parrots", replace all instances of the string "and" with "or"
"dogs and cats and parrots".sub("and", "or")
33. Given the array ["dogs", "cats", "parrots"], create the string "dogs and cats and parrots"
friends = "dogs and cats and parrots"
friends.join(" and ")
or
sentence = ""
friends.each do |friend|
sentence << friend + " and "
end
return sentence