Fork this gist. Edit it with your answers, and submit the link here
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 = "Kinan Whyte"
given = "Turing"
given[0]
given = "abcdefghijklmnopqrstuvwxyz"
given[-1]
given = "Welcome to the Dungeon"
given.split[0]
"supercalifragilisticexpialidocious".length
"Coding is gr8".split.length
"key" + "board"
no_things = []
array = [1,2,3,4,5]
given = ["dog", "cat", "parrot"]
given[1]
given = ["dog", "cat", "parrot"]
given.length
student_names = ["Gabby", "David"]
puts student_names
student_names.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.
given = [1,2,3,4]
even_numbers = []
odd_numbers = []
given.map do |num|
even_num << num if num.even?
end
puts even_numbers
given.map do |num|
new_array << num if num.odd?
end
puts odd_numbers
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
given = [
["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 = [
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
#These are already in alphabetical order but...otherwise I would use:
given.sort
given = ["pizza", "casserole", "pie"]
given.pop
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"
pets = {:puppy => "dog"}
pets.merge!(:kitten => "cat")
22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)
pets.keys
23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)
pets.values
24. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing each name in ALL CAPS
given = ["Jeff", "Horace", "Josh", "Joanne"]
given_allcaps = []
given.each do |name|
given_allcaps << name = name.upcase
end
puts given_allcaps
25. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that are shorter than 5 characters
given = ["Jeff", "Horace", "Josh", "Joanne"]
given_new = []
given.each do |name|
given_new << name if name.length < 5
end
puts given_new
given = [1,2,3,4]
sum = 0
given.each do |num|
sum = sum + num
end
puts
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"]
)
"pizza".upcase.chars
29. Given the array ["Jeff", "Horace", "Josh", "Joanne"]
, create a new array containing only the names that start with "J"
given = ["Jeff", "Horace", "Josh", "Joanne"]
given_new = []
given.each do |name|
given_new << name if name[0] == "J"
end
[
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
given = [
["Horace", "Module 1"],
["Jeff", "Module 1"],
["Rachel", "Module 2"],
["Steve", "Module 4"]
]
puts given[2][0]
puts "turing students love sunlight".gsub("sunlight", "darkness")
32. Given the string "dogs and cats and parrots"
, replace all instances of the string "and"
with "or"
puts "dogs and cats and parrots".gsub("and", "or")
given = ["dogs", "cats", "parrots"]
given.join(" and ")