Guest User

Untitled

a guest
Dec 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. # Define a method that returns the sum of all the non-negative integers up to and including its argument.
  2. # sum_to(3) => 6
  3. def sum_to(int)
  4. index = 0
  5. sum = 0
  6. while index <= int
  7. sum = sum + index
  8. index=index+1
  9. end
  10. sum
  11. end
  12.  
  13. # Define a method, #e_words(str), that accepts a string as an argument. Your method return the number of words in the string that end with the letter "e".
  14. # e_words("tree") => 1
  15. # e_words("Let be be finale of seem.") => 3
  16. def e_words(str)
  17. words = str.split
  18. count = 0
  19. word_index = 0
  20. while word_index < words.length
  21. if words[word_index][-1]=="e"
  22. count=count+1
  23. end
  24. word_index = word_index + 1
  25. end
  26. count
  27. end
  28.  
  29. # A magic number is a number whose digits, when added together, sum to 7, e.g., 34. Define a method that returns an array of the first n magic numbers. You may wish to write a helper method (magic_number?) that returns a boolean indicating whether a number is magic. This problem is harder than anything you'll receive on the coding assessment.
  30. # magic_numbers(3) => [7, 16, 25]
  31. def magic_number?(n)
  32. stringified = n.to_s.split("")
  33. index = 0
  34. sum = 0
  35. while index < stringified.length
  36. sum = sum + stringified[index].to_i
  37. index = index + 1
  38. end
  39. sum==7
  40. end
  41.  
  42. def magic_numbers(n)
  43. magic_array = []
  44. current_num = 1
  45. while magic_array.length < n
  46. if magic_number?(current_num) == true
  47. magic_array << current_num
  48. end
  49. current_num = current_num + 1
  50. end
  51. magic_array
  52. end
  53.  
  54. # DO NOT MODIFY CODE BELOW
  55.  
  56. $success_count = 0
  57. $failure_count = 0
  58.  
  59. def deep_dup(arr)
  60. arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el }
  61. end
  62.  
  63. def note_success(returned, invocation, expectation)
  64. puts "success: #{invocation} => #{expectation}"
  65. $success_count += 1
  66. end
  67.  
  68. def note_failure(returned, invocation, expectation)
  69. puts "failure: #{invocation}: expected #{expectation}, returned #{returned}"
  70. $failure_count += 1
  71. end
  72.  
  73. def format_args(args)
  74. o_args = deep_dup(args)
  75. o_args.map! do |arg|
  76. arg = prettify(arg)
  77. arg.class == Array ? arg.to_s : arg
  78. end
  79. o_args.join(', ')
  80. end
  81.  
  82. def prettify(statement)
  83. case statement
  84. when Float
  85. statement.round(5)
  86. when String
  87. "\"#{statement}\""
  88. when NilClass
  89. "nil"
  90. else
  91. statement
  92. end
  93. end
  94.  
  95. def equality_test(returned, invocation, expectation)
  96. if returned == expectation && returned.class == expectation.class
  97. note_success(returned, invocation, expectation)
  98. else
  99. note_failure(returned, invocation, expectation)
  100. end
  101. end
  102.  
  103. def identity_test(returned, invocation, expectation, args)
  104. if returned.__id__ == args[0].__id__
  105. equality_test(returned, invocation, expectation)
  106. else
  107. puts "failure: #{invocation}: You did not mutate the original array!"
  108. $failure_count += 1
  109. end
  110. end
  111.  
  112. def method_missing(method_name, *args)
  113. method_name = method_name.to_s
  114. expectation = args[-1]
  115. args = args[0...-1]
  116. if method_name.start_with?("test_")
  117. tested_method = method_name[5..-1]
  118. print_test(tested_method, args, expectation)
  119. else
  120. method_name = method_name.to_sym
  121. super
  122. end
  123. end
  124.  
  125. def print_test(method_name, args, expectation)
  126. returned = self.send(method_name, *args)
  127. returned = prettify(returned)
  128. expectation = prettify(expectation)
  129. args_statement = format_args(args)
  130. invocation = "#{method_name}(#{args_statement})"
  131. method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation)
  132. rescue Exception => e
  133. puts "failure: #{invocation} threw #{e}"
  134. puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")}
  135. $failure_count += 1
  136. end
  137.  
  138. puts "
  139. sum_to:
  140. " + "*" * 15 + "
  141. "
  142. test_sum_to(3, 6)
  143. test_sum_to(0, 0)
  144. puts "
  145. e_words:
  146. " + "*" * 15 + "
  147. "
  148. test_e_words("loom", 0)
  149. test_e_words("To be or not to be", 2)
  150. puts "
  151. magic_numbers:
  152. " + "*" * 15 + "
  153. "
  154. test_magic_numbers(3, [7, 16, 25])
  155. test_magic_numbers(0, [])
  156. puts
  157. puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}"
  158. puts "TOTAL FAILURES: #{$failure_count}"
  159. $success_count = 0
  160. $failure_count = 0
Add Comment
Please, Sign In to add comment