Guest User

Untitled

a guest
Dec 14th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. # EASY
  2.  
  3. # Return the middle character of a string. Return the middle two characters if the word is of even length,
  4. # e.g., middle_substring("middle") => "dd",
  5. # middle_substring("mid") => "i"
  6.  
  7. def middle_substring(str)
  8. if str.length.even?
  9. return str[(str.length)/2-1 .. (str.length)/2]
  10. else
  11. return str[(str.length/2).floor]
  12. end
  13. end
  14.  
  15. # Return the number of vowels in a string.
  16. def num_vowels(str)
  17. vowels = ["a","e","i","o","u"]
  18. index = 0
  19. count = 0
  20. while index < str.length
  21. ch = str[index]
  22. if vowels.include?(ch.downcase)
  23. count = count + 1
  24. end
  25. index = index + 1
  26. end
  27. count
  28. end
  29.  
  30.  
  31. # MEDIUM
  32.  
  33. # Return the argument with all its lowercase characters removed.
  34. def destructive_uppercase(str)
  35. new_str = ""
  36. index = 0
  37. while index < str.length
  38. if str[index]==str[index].upcase
  39. new_str << str[index]
  40. end
  41. index=index+1
  42. end
  43. new_str
  44. end
  45.  
  46. # Write a method that returns an array containing
  47. # all the elements of the argument array in reverse order.
  48. def my_reverse(arr)
  49. arr.reverse
  50. end
  51.  
  52.  
  53. # HARD
  54.  
  55. # Write your own version of the join method.
  56. # Assume this method will always receive a
  57. # separator as the second argument.
  58. def my_join(arr, separator)
  59. new_string = ""
  60. index = 0
  61. while index < arr.length
  62. new_string = new_string + arr[index]
  63. if index != arr.length - 1
  64. new_string = new_string + separator
  65. end
  66. index=index+1
  67. end
  68. new_string
  69. end
  70.  
  71. # Return an array of integers from 1 to 30 (inclusive), except for each multiple of 3 replace the integer with "fizz", for each multiple of 5 replace the integer with "buzz", and for each multiple of both 3 and 5, replace the integer with "fizzbuzz".
  72. def fizzbuzz
  73. array = []
  74. index = 1
  75.  
  76. while index < 31
  77. if index % 3 == 0 && index % 5 == 0
  78. array << "fizzbuzz"
  79. elsif index % 5 == 0
  80. array << "buzz"
  81. elsif index % 3 == 0
  82. array << "fizz"
  83. else array << index
  84. end
  85. index = index + 1
  86. end
  87. array
  88. end
  89.  
  90. # DO NOT MODIFY CODE BELOW
  91.  
  92. $success_count = 0
  93. $failure_count = 0
  94.  
  95. def deep_dup(arr)
  96. arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el }
  97. end
  98.  
  99. def note_success(returned, invocation, expectation)
  100. puts "success: #{invocation} => #{expectation}"
  101. $success_count += 1
  102. end
  103.  
  104. def note_failure(returned, invocation, expectation)
  105. puts "failure: #{invocation}: expected #{expectation}, returned #{returned}"
  106. $failure_count += 1
  107. end
  108.  
  109. def format_args(args)
  110. o_args = deep_dup(args)
  111. o_args.map! do |arg|
  112. arg = prettify(arg)
  113. arg.class == Array ? arg.to_s : arg
  114. end
  115. o_args.join(', ')
  116. end
  117.  
  118. def prettify(statement)
  119. case statement
  120. when Float
  121. statement.round(5)
  122. when String
  123. "\"#{statement}\""
  124. when NilClass
  125. "nil"
  126. else
  127. statement
  128. end
  129. end
  130.  
  131. def equality_test(returned, invocation, expectation)
  132. if returned == expectation && returned.class == expectation.class
  133. note_success(returned, invocation, expectation)
  134. else
  135. note_failure(returned, invocation, expectation)
  136. end
  137. end
  138.  
  139. def identity_test(returned, invocation, expectation, args)
  140. if returned.__id__ == args[0].__id__
  141. equality_test(returned, invocation, expectation)
  142. else
  143. puts "failure: #{invocation}: You did not mutate the original array!"
  144. $failure_count += 1
  145. end
  146. end
  147.  
  148. def method_missing(method_name, *args)
  149. method_name = method_name.to_s
  150. expectation = args[-1]
  151. args = args[0...-1]
  152. if method_name.start_with?("test_")
  153. tested_method = method_name[5..-1]
  154. print_test(tested_method, args, expectation)
  155. else
  156. method_name = method_name.to_sym
  157. super
  158. end
  159. end
  160.  
  161. def print_test(method_name, args, expectation)
  162. returned = self.send(method_name, *args)
  163. returned = prettify(returned)
  164. expectation = prettify(expectation)
  165. args_statement = format_args(args)
  166. invocation = "#{method_name}(#{args_statement})"
  167. method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation)
  168. rescue Exception => e
  169. puts "failure: #{invocation} threw #{e}"
  170. puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")}
  171. $failure_count += 1
  172. end
  173.  
  174. fizzbuzzed = [1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz",
  175. 13, 14, "fizzbuzz", 16, 17, "fizz", 19, "buzz", "fizz", 22, 23, "fizz", "buzz",
  176. 26, "fizz", 28, 29, "fizzbuzz"]
  177.  
  178. puts "
  179. middle_substring:
  180. " + "*" * 15 + "
  181. "
  182. test_middle_substring("middle", "dd")
  183. test_middle_substring("mid", "i")
  184.  
  185. puts "
  186. num_vowels:
  187. " + "*" * 15 + "
  188. "
  189. test_num_vowels("Aruba", 3)
  190. test_num_vowels("bcd", 0)
  191. puts "
  192. destructive_uppercase:
  193. " + "*" * 15 + "
  194. "
  195. test_destructive_uppercase("doGgIE", "GIE")
  196. test_destructive_uppercase("HI", "HI")
  197. puts "
  198. my_reverse:
  199. " + "*" * 15 + "
  200. "
  201. test_my_reverse(["a", "b", "c"], ["c", "b", "a"])
  202. test_my_reverse(["a"], ["a"])
  203. puts "
  204. my_join:
  205. " + "*" * 15 + "
  206. "
  207. test_my_join(["k", "i", "t", "t", "y"], "", "kitty")
  208. test_my_join(["dolla", "dolla"], "$", "dolla$dolla")
  209. puts "
  210. fizzbuzz:
  211. " + "*" * 15 + "
  212. "
  213. test_fizzbuzz(fizzbuzzed)
  214. puts "
  215. "
  216. puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}"
  217. puts "TOTAL FAILURES: #{$failure_count}"
  218. $success_count = 0
  219. $failure_count = 0
Add Comment
Please, Sign In to add comment