Advertisement
Guest User

Untitled

a guest
May 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. ## Module 1 Week 3 Diagnostic
  2.  
  3. This exercise is intended to help you assess your progress
  4. with the concepts and techniques we've covered during the week.
  5.  
  6. For these questions, write a short snippet of code that meets
  7. the requirement. In cases where the question mentions a "given"
  8. data value, use the variable `given` to refer to it (instead of re-writing
  9. the information).
  10.  
  11. ##### 1. What does the following code output?
  12.  
  13. ```ruby
  14. def print_variables(x)
  15. puts "x: #{x}"
  16. puts "b: #{b}"
  17. end
  18.  
  19. def b
  20. 12
  21. end
  22.  
  23. a = 4
  24. print_variables(a)
  25. ```
  26.  
  27. ```
  28. x: 4
  29. b: 12
  30. ```
  31.  
  32. ##### 2. Working with files
  33.  
  34. Given a text file located at `"~/Documents/pizza.txt"`, write code to read the
  35. file from the filesystem and print _each line_ one at a time.
  36.  
  37. ```
  38. lines = File.readlines("~/Documents/pizza.txt")
  39.  
  40. lines.each do |line|
  41. puts line
  42. end
  43.  
  44. lines.close
  45. ```
  46.  
  47. ##### 3. Writing Files
  48.  
  49. Given a text file located at `"~/Documents/pizza.txt"`, write code to read the
  50. file from the filesystem, then write a new file at `"~/Documents/line_count.txt"`
  51. containing the number of lines in the original file.
  52.  
  53. ```
  54. lines = File.readlines("~/Documents/pizza.txt")
  55.  
  56. num_lines = 0
  57. lines.each { |line| num_lines += 1 }
  58.  
  59. lines_file = File.new("lines.txt", 'w')
  60. lines_file << "There are #{num_lines} lines in 'pizza.txt'"
  61.  
  62. lines.close
  63. lines_file.close
  64. ```
  65.  
  66. ##### 4. Corgis
  67.  
  68. Imagine a simple ruby class designed to represent a Corgi dog. Write a _test_
  69. for each of the following features:
  70.  
  71. * A Corgi can be created with no arguments
  72. * A Corgi can be assigned a name
  73. * A Corgi can be asked for its name
  74. * A Corgi can be asked for its posture, which should default to "standing"
  75. * A Corgi can be asked to lie down, which should change its posture to "laying"
  76.  
  77. ```
  78. require 'minitest/autorun'
  79. require 'minitest/pride'
  80. require './lib/corgi'
  81.  
  82. class CorgiTest < Minitest::Test
  83.  
  84. def test_it_can_be_created_with_no_arguments
  85. new_corg = Corgi.new
  86. assert_instance_of Corgi, new_corg
  87. end
  88.  
  89. def test_it_can_be_assigned_a_name
  90. new_corg = Corgi.new
  91. assert new_corg.name = "Daniel"
  92. end
  93.  
  94. def test_it_can_be_asked_for_its_name
  95. new_corg = Corgi.new
  96. new_corg.name = "Daniel"
  97. assert_equal "Daniel", new_corg.name
  98. end
  99.  
  100. def test_it_has_a_default_posture_of_standing
  101. new_corg = Corgi.new
  102. assert_equal "standing", new_corg.posture
  103. end
  104.  
  105. def test_it_can_lie_down_and_change_posture_to_laying
  106. new_corg = Corgi.new
  107. new_corg.lie_down
  108. assert_equal "laying", new_corg.posture
  109. end
  110. end
  111. ```
  112.  
  113. ##### 5. Counting Words
  114.  
  115. Given an array of words `["dog", "cat", "gerbil", "cat", "hamster", "rabbit", "rabbit"]`,
  116. create a Hash containing the individual words as keys and the number of times
  117. the word appears in the list as values. That is:
  118.  
  119. ```
  120. {"dog" => 1, "cat" => 2, "gerbil" => 1, "hamster" => 1, "rabbit" => 2}
  121. ```
  122.  
  123. ```
  124. new_hash = Hash[ given.map { |animal| [animal, given.count(animal)] } ]
  125. ```
  126.  
  127. ##### 6. Reading Files
  128.  
  129. Given a text file located at `"~/Documents/pizza.txt"`, write code to read the
  130. file from the filesystem, then process the file's lines so that:
  131.  
  132. * Even lines go into an array called `even`
  133. * Odd lines go into an array called `odd`
  134.  
  135. (Assume the first line is numbered `0`, and is thus even)
  136.  
  137. ```
  138. file = File.readlines("~/Documents/pizza/txt")
  139.  
  140. line_num = 0
  141. even = []
  142. odd = []
  143.  
  144. file.each do |line|
  145. if line_num.even?
  146. even << line
  147. else
  148. odd << line
  149. end
  150. line_num += 1
  151. end
  152. ```
  153.  
  154. ##### 7. Stacks
  155.  
  156. Given the following code, draw a simple diagram representing the stack
  157. frames that the program will generate as it is run. In order to show
  158. change in the stack over time, you may need to re-copy the lower frames
  159. into a new diagram.
  160.  
  161. ```ruby
  162. def wrap_it(x)
  163. "<<<" + x + ">>>"
  164. end
  165.  
  166. def string_it(x)
  167. x.to_s
  168. end
  169.  
  170. def churn_it(x)
  171. wrap_it(string_it(x))
  172. end
  173.  
  174.  
  175. churn_it(10)
  176. ```
  177.  
  178. ```
  179. - As the methods are defined self is main, local variables are empty, and current method is main
  180. - When it reaches churn_it(10) self remains as main, current method becomes churn_it, and the local variable becomes 10
  181. - Upon evaluating churn_it, self remains main, local variable remains 10 (passed as an arg to string_it) and our current method becomes string_it
  182. - When we move to string_it we pass the local variable remains 10, self remains, main, and current method is string_it
  183. - As string_it is evaluated, our local variable is 10, self is main, and our current method is .to_s, creating "10", which is returned
  184. - All things string_it come off the stack and we are back inside of churn_it, where our returned value "10" will be the argument passed to wrap_it. Before we enter into wrap_it, self is still main, local variables are 10 and "10", and our current method is churn_it
  185. - We pass "10" into wrap_it. Self is main, local variable is "10", and current method is wrap_it. Wrap_it evaluates to "<<<< 10 >>>>" and returns that string.
  186. - With wrap_it completed, it comes off the stack and we move back to churn_it, where our local variable remains 10, self is main, current method is churn_it, and it will return "<<<< 10 >>>>".
  187. - Churn_it returns "<<<< 10 >>>>", local variables are empty, self is main, current method is main, and everything else is off the stack.
  188. - With the program completed, main comes off and the program exits.
  189. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement