Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 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.  
  5. 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).
  6.  
  7. Use single (\`) and triple backticks (\`\`\`) to container code snippets.
  8.  
  9. 1. Define a class called `PizzaOven` which has a method `cook_pizza` which returns the string `"mmm 'za"`.
  10.  
  11. `class PizzaOven
  12. ```def cook_pizza
  13. ```return "mmm za'
  14. ```end
  15. `end
  16.  
  17. 2. Define a class called `Student` which is instantiated with a "name" value and which has a method `name` that returns this value
  18. `class Student
  19. ```def initialize(name)
  20. ```@name = name
  21. ```end
  22. ```
  23. ```def student_name
  24. ```@name
  25. ```end
  26. `end
  27.  
  28. 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?
  29. `double = []
  30. `given.each do |num|
  31. `double << num*2
  32. `end
  33. `double
  34.  
  35. `odd = []
  36. `given.each do |num|
  37. ```if num.odd?
  38. ```odd << num
  39. ```end
  40. `end
  41. `odd
  42.  
  43. 4. Give the command to create a new Git repository in a directory on your machine
  44.  
  45. git init
  46.  
  47. ### Pizza
  48.  
  49. 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.
  50.  
  51. `def test_if_tasty_work
  52. ```pizza = Pizza.new
  53. ```assert_equal true, pizza.is_tasty
  54. `end
  55.  
  56. 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.
  57.  
  58. `def test_style_include_given
  59. ```pizza=Pizza.new
  60. ```assert_equal true, pizza.style.include?"supreme"
  61. ```assert_equal true, pizza.style.include?"mediterranean"
  62. ```assert_equal true, pizza.style.include?"cheese"
  63. `end
  64.  
  65. 6. Give the Git commands needed to *stage* and then *commit* a set of changes to a file
  66. git add file_name
  67. git commit -m "some sentence"
  68. git push file_name
  69.  
  70. ### Student
  71.  
  72. 7. Define a `Student` class which, when created, has an `attitude` attribute.
  73. `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`.
  74.  
  75. `class Student
  76. `attr_reader :attitude
  77. ```def initialize(attitude = "cheerful")
  78. ```@attitude =attitude
  79. ```end
  80. `end
  81.  
  82. 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.
  83.  
  84. `class Student
  85. `attr_reader :attitude
  86. ```def initialize(attitude = "cheerful")
  87. ```@attitude =attitude
  88. ```end
  89. ```
  90. ```def assign_homework
  91. ```if attitude == "cheerful"
  92. ```@attitude = "dubious"
  93. ```elsif attitude == "dubious"
  94. ```@attitude = "perturbed"
  95. ```elsif attitude == "perturbed"
  96. ```@attitude = "dazed
  97. ```else
  98. ```attitude
  99. ```end
  100. ```
  101. `end
  102.  
  103.  
  104. 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:
  105.  
  106. ```ruby
  107. s = Student.new
  108. s.assign_homework("Write a linked list")
  109. ```
  110.  
  111. 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:
  112.  
  113. ```ruby
  114. s = Student.new
  115. s.attitude
  116. => "cheerful"
  117. s.assign_homework("write a linked list")
  118. s.attitude
  119. => "dubious"
  120. s.assign_homework("write a BST")
  121. s.attitude
  122. => "perturbed"
  123. s.assignments
  124. => "write a linked list, write a BST"
  125. ```
  126.  
  127. 10. Given an array of 3 `Student` instances, generate a new string of *all* of their assignments
  128.  
  129. For example:
  130.  
  131. ```
  132. s1 = Student.new
  133. s2 = Student.new
  134. s3 = Student.new
  135.  
  136. s1.assign_homework("linked list")
  137. s1.assign_homework("sorting algos")
  138.  
  139. s2.assign_homework("write a c compiler")
  140. s2.assign_homework("write a pacman game")
  141.  
  142. s3.assign_homework("headcount")
  143. s3.assign_homework("sales engine")
  144.  
  145. students = [s1,s2,s3]
  146.  
  147. # YOUR CODE HERE
  148.  
  149. => "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
  150. ```
  151.  
  152. 11. What does the following code output?
  153.  
  154. ```ruby
  155. def print_variables(x)
  156. puts "x: #{x}"
  157. puts "b: #{b}"
  158. end
  159.  
  160. def b
  161. 12
  162. end
  163.  
  164. a = 4
  165. print_variables(a)
  166. ```
  167.  
  168. 12. Working with files: given a text file located at `"~/Documents/pizza.txt"`, write code to read the
  169. file from the filesystem and print _each line_ one at a time.
  170.  
  171. 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.
  172.  
  173. 14. Imagine a simple ruby class designed to represent a Corgi dog. Write a _test_ for each of the following features:
  174.  
  175. * A Corgi can be created with no arguments
  176. * A Corgi can be assigned a name
  177. * A Corgi can be asked for its name
  178. * A Corgi can be asked for its posture, which should default to "standing"
  179. * A Corgi can be asked to lie down, which should change its posture to "laying"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement