Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. # Week 2 Diagnostic
  2.  
  3. This exercise is intended to help you assess your progress with the concepts and techniques we've covered during the week.
  4. For these questions, write a short snippet of code that meets the requirement. In cases where the question mentions a "given" data value, use the variable `given` to refer to it (instead of re-writing the information).
  5. Use single (\`) and triple backticks (\`\`\`) to container code snippets.
  6.  
  7. 1. Define a class called `PizzaOven` which has a method `cook_pizza` which returns the string `"mmm 'za"`.
  8. class PizzaOven
  9. def cook_pizza
  10. "mmm 'za"
  11. end
  12. end
  13.  
  14. 2. Define a class called `Student` which is instantiated with a "name" value and which has a method `name` that returns this value
  15. class Student
  16. attr_reader :name
  17. def initialize(name)
  18. @name = name
  19. end
  20. end
  21.  
  22. 3. Given an array of the numbers `[1,2,3,4,5]`, how would you create a new array of all elements doubled? How would you return an array of all odd elements?
  23. doubled = []
  24. array.each do |num|
  25. doubled << num * 2
  26. end
  27.  
  28. array.find_all do |num|
  29. num.odd?
  30. end
  31.  
  32. 4. Give the command to create a new Git repository in a directory on your machine
  33. git push origin -b url -or something along those lines. I still go into github to create the repo and push from command line
  34.  
  35. ### Pizza
  36.  
  37. 5. Given a hypothetical `Pizza` class which has an instance method `is_tasty?` that always returns true, write a simple Minitest test that tests this behavior.
  38. def test_its_tasty
  39. pizza = Pizza.new
  40. assert pizza.is_tasty?
  41. end
  42.  
  43.  
  44. 6. Suppose the `Pizza` class also has a method `style` which randomly returns one of: `"supreme"`, `"mediterranean"`, or `"cheese"`. Write a test that confirms that the returned pizza style is within this list.
  45. def test_style_choices
  46. pizza = Pizza.new
  47. assert_equal "supreme" || "mediterranean" || "cheese", pizza.style
  48. end
  49.  
  50. 6. Give the Git commands needed to *stage* and then *commit* a set of changes to a file
  51. git add .
  52. git commit -m "changes"
  53.  
  54. ### Student
  55.  
  56. 7. Define a `Student` class which, when created, has an `attitude` attribute.
  57. `attitude` should start out with the value "cheerful", and the `Student` class should provide a "reader" method that allows us to access the value of its `attitude`.
  58. class Student
  59. attr_reader :attitude
  60. def initialize(attitude = cheerful)
  61. @attitude = attitude
  62. end
  63. end
  64.  
  65. 8. Additionally, add an `assign_homework` method to `Student`. When `assigned_homework` is invoked, if the student's `attitude` is `"cheerful"`, it should become `"dubious"`. If the value is currently `"dubious"` it should become `"perturbed"`. If the value is currently `"perturbed"`, it should become `"dazed"`. Assigning homework to a `"dazed"` student has no effect.
  66. def assign_homework
  67. attitude = "dazed" if attitude = "perturbed"
  68. attitude = "perturbed" if attitude = "dubious"
  69. attitude = "dubious" if attitude = "cheerful"
  70. end
  71.  
  72. 9. Building on the `Student` class from the previous example, update the `assign_homework` method to accept an argument. The argument will be a `String` containing a short description of the assignment. For example we might use it like this:
  73. def assign_homework(description)
  74. attitude = "dazed" if attitude = "perturbed"
  75. attitude = "perturbed" if attitude = "dubious"
  76. attitude = "dubious" if attitude = "cheerful"
  77. end
  78. ```ruby
  79. s = Student.new
  80. s.assign_homework("Write a linked list")
  81. ```
  82.  
  83. Then, add an `assignments` method to `Student`. `assignments` should return a list of all the assignments that have been given, separated by a comma and a space. For example:
  84. assignments = ""
  85. def assignments
  86. assignments += description + ", "
  87. end
  88. ```ruby
  89. s = Student.new
  90. s.attitude
  91. => "cheerful"
  92. s.assign_homework("write a linked list")
  93. s.attitude
  94. => "dubious"
  95. s.assign_homework("write a BST")
  96. s.attitude
  97. => "perturbed"
  98. s.assignments
  99. => "write a linked list, write a BST"
  100. ```
  101.  
  102. 10. Given an array of 3 `Student` instances, generate a new string of *all* of their assignments
  103.  
  104. For example:
  105.  
  106. ```
  107. s1 = Student.new
  108. s2 = Student.new
  109. s3 = Student.new
  110.  
  111. s1.assign_homework("linked list")
  112. s1.assign_homework("sorting algos")
  113.  
  114. s2.assign_homework("write a c compiler")
  115. s2.assign_homework("write a pacman game")
  116.  
  117. s3.assign_homework("headcount")
  118. s3.assign_homework("sales engine")
  119.  
  120. students = [s1,s2,s3]
  121.  
  122. all_assignments = ""
  123. students.each do |hw|
  124. all_assignments += students[hw].assignments + " "
  125. end
  126.  
  127. => "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
  128. ```
  129.  
  130. 11. What does the following code output?
  131.  
  132. ```ruby
  133. def print_variables(x)
  134. puts "x: #{x}"
  135. puts "b: #{b}"
  136. end
  137.  
  138. def b
  139. 12
  140. end
  141.  
  142. a = 4
  143. print_variables(a)
  144. ```
  145. "a: 4"
  146. "b: 12"
  147.  
  148. 12. Working with files: given a text file located at `"~/Documents/pizza.txt"`, write code to read the
  149. file from the filesystem and print _each line_ one at a time.
  150.  
  151. new_file = File.open(ARGV[0], "r")
  152. text = new_file.read
  153. print text
  154.  
  155.  
  156.  
  157. 13. Writing Files: given a text file located at `"~/Documents/pizza.txt"`, write code to read the file from the filesystem, then write a new file at `"~/Documents/line_count.txt"` containing the number of lines in the original file.
  158.  
  159. new_file = File.open(ARGV[0], "r")
  160. text = new_file.read
  161. print text
  162. writer = File.open(ARGV[1], "w")
  163. writer.write(text)
  164.  
  165.  
  166. 14. Imagine a simple ruby class designed to represent a Corgi dog. Write a _test_ for each of the following features:
  167.  
  168. * A Corgi can be created with no arguments
  169. def test_it_exisits
  170. dog = Corgi.new
  171. assert_instance_of Corgi
  172. end
  173. * A Corgi can be assigned a name
  174. def test_it_has_name
  175. dog = Corgi.new("Bob")
  176. assert_equal "Bob", dog.name
  177. end
  178. * A Corgi can be asked for its name
  179. def test_provides_name
  180. dog = Corgi.new("Ted")
  181. assert_equal "Ted", dog.name
  182. end
  183. * A Corgi can be asked for its posture, which should default to "standing"
  184. def test_posture
  185. dog = Corgi.new("Ted", posture = "standing")
  186. assert_equal "standing", dog.posture
  187. end
  188. * A Corgi can be asked to lie down, which should change its posture to "laying"
  189. def test_dog_lies_down
  190. dog = Corgi.new("Ted", posture = "standing")
  191. assert_equal "laying", dog.lie_down
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement