Skip to content

Instantly share code, notes, and snippets.

@lsaville
Forked from mikedao/1608-b-week1.markdown
Last active August 22, 2016 15:41
Show Gist options
  • Save lsaville/84bf0455165e04ad4e6139f39bb829e4 to your computer and use it in GitHub Desktop.
Save lsaville/84bf0455165e04ad4e6139f39bb829e4 to your computer and use it in GitHub Desktop.

Module 1 Week 1 Diagnostic

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).

1. Create a variable called name and assign it to a string containing your name.

name = "Lee Saville"




2. Given the string "Turing", retrieve the first character

"Turing"[0]




3. Given the string "abcdefghijklmnopqrstuvwxyz", retrieve the last character

"abcdefghijklmnopqrstuvwxyz".last




4. Given the string "Welcome to the Dungeon", retrieve the substring "Welcome"

"Welcome to the Dungeon"[0,7]




5. Find the number of characters in the string "supercalifragilisticexpialidocious"

"supercalifragilisticexpialidocious".length




6. Find the number of words in the string "Coding is gr8"

"Coding is gr8".split.count




7. Given 2 strings, "key" and "board", create a new string by concatening them together

"key" + "board"




8. Create a variable called no_things and assign it to an empty array

no_things = []




9. Create an array of the numbers 1 to 5

(1..5).to_a




10. Given the array ["dog", "cat", "parrot"], retrieve the second element ("cat")

array[1]




11. Given the array ["dog", "cat", "parrot"], find how many elements appear in the array

array.count




12. Create an array containing strings of the names of the 2 students sitting closest to you.

names = ["Jon", "Tessa"]




13. Given the array from number 12, print the name of each student.

names.each { |name| print name }






14. Given the array from number 12, add the name "Jeff" to the end of the list

names << "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.
numbers = [1,2,3,4]
evens = numbers.select { |num| num.even? }
odds = numbers.select { |num| num.odd? }







16. Given the following array, retrieve the second element of the first nested array
names = [
  ["Horace", "Module 1"],
  ["Jeff", "Module 1"],
  ["Rachel", "Module 2"],
  ["Steve", "Module 4"]
]
names[0][1]



17. Given the following array, sort the list alphabetically by the names (i.e. the first element of each sub-array)
names = [
  ["Horace", "Module 1"],
  ["Jeff", "Module 1"],
  ["Rachel", "Module 2"],
  ["Steve", "Module 4"]
]
names.sort_by { |x| x[0] }





18. Given the array ["pizza", "casserole", "pie"], remove the last element from the array

array.pop




19. Create an empty hash

cat = Hash.new




20. Create a hash which associates the key "puppy" with the value "dog"

{"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"

subset = {"puppy" => "dog", "kitten" => "cat"}





22. Given the hash from 21, create an array of the strings "puppy" and "kitten" (don't worry about the order)

subset.keys





23. Given the hash from 21, create an array of the strings "dog" and "cat" (don't worry about the order)

subset.values





24. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing each name in ALL CAPS

names.each { |name| name.upcase! }






25. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing only the names that are shorter than 5 characters

names.select { |name| name.length < 5 }






26. Given the array [1,2,3,4], add up all the numbers in the list
total = 0
numbers.each do |num|
  total += num
end







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"])
food = "pizza"
individual_capitalized = food.upcase.chars





29. Given the array ["Jeff", "Horace", "Josh", "Joanne"], create a new array containing only the names that start with "J"

j_names = names.select { |name| name[0] == "J" }






30. Given the following array, retrieve the name of the first instructor who teaches "Module 2"
[
  ["Horace", "Module 1"],
  ["Jeff", "Module 1"],
  ["Rachel", "Module 2"],
  ["Steve", "Module 4"]
]

mod_2_teacher = names.find { |name| name[1] == "Module 2" }





31. Given the string "turing students love sunlight" replace "sunlight" with "darkness"

"turing students love sunlight".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".gsub("and", "or")




33. Given the array ["dogs", "cats", "parrots"], create the string "dogs and cats and parrots"

["dogs", "cats", "parrots"].join(' and ')




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment