Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. ## Practice Exercises for Assessment 109
  2.  
  3. ### Problem Set #1
  4.  
  5. 1. Reverse an array without using the built-in reverse method.
  6.  
  7. 2. Select the element out of the array if its index is a fibonacci number.
  8.  
  9. **BONUS**: Write a method that finds the nth fibonacci number where `n` is the number passed to the method.
  10.  
  11. 3. Write a method to determine if a word is a palindrome, without using the reverse method.
  12.  
  13.  
  14. ### Problem Set #2
  15.  
  16. 1. Reverse a string.
  17.  
  18. 2. Write a method that takes two arguments: the first is the starting number, and the second is the ending number. Print out all numbers between the two numbers, except if a number is divisible by 3, print "Fizz", if a number is divisible by 5, print "Buzz", and finally if a number is divisible by 3 and 5 print "FizBuzz".
  19.  
  20. 3. Implement the search function within this code:
  21. ```ruby
  22. PRODUCTS = [
  23. { name: "Thinkpad x210", price: 220 },
  24. { name: "Thinkpad x220", price: 250 },
  25. { name: "Thinkpad x250", price: 979 },
  26. { name: "Thinkpad x230", price: 300 },
  27. { name: "Thinkpad x230", price: 330 },
  28. { name: "Thinkpad x230", price: 350 },
  29. { name: "Thinkpad x240", price: 700 },
  30. { name: "Macbook Leopard", price: 300 },
  31. { name: "Macbook Air", price: 700 },
  32. { name: "Macbook Pro", price: 600 },
  33. { name: "Macbook", price: 1449 },
  34. { name: "Dell Latitude", price: 200 },
  35. { name: "Dell Latitude", price: 650 },
  36. { name: "Dell Inspiron", price: 300 },
  37. { name: "Dell Inspiron", price: 450 },
  38. ]
  39.  
  40. query = {
  41. price_min: 240,
  42. price_max: 280,
  43. q: "thinkpad"
  44. }
  45.  
  46. query2 = {
  47. price_min: 300,
  48. price_max: 450,
  49. q: "dell"
  50. }
  51.  
  52. def search(query)
  53. # implement
  54. end
  55.  
  56. search(query)
  57. # => [ { name: "Thinkpad x220", price: 250 }]
  58. search(query2)
  59. # => [ { name: "Dell Inspiron", price: 300 }, { name: "Dell Inspiron", price: 450 }]
  60. ```
  61.  
  62. 4. Write a method that takes an array of strings, and returns an array of the same string values, except with the vowels removed.
  63.  
  64. 5. Write a method that takes a string, and returns a boolean indicating whether this string has a balanced set of parentheses.
  65.  
  66. 6. Write a method that takes two numbers. It should print out all primes between the two numbers. Don't use Ruby's `prime` class.
  67.  
  68. a. Write a method that will take an array of numbers and only return those that are prime.
  69.  
  70. b. Write a method that will take an array of numbers and return the number of primes in the array.
  71.  
  72. 7. Write a program that asks the user to enter an integer greater than 0, then asks if the user wants to determine the sum or product of all numbers between 1 and the entered integer.
  73. ```
  74. >> Please enter an integer greater than 0:
  75. 5
  76. >> Enter 's' to compute the sum, 'p' to compute the product.
  77. s
  78. The sum of the integers between 1 and 5 is 15.
  79.  
  80. >> Please enter an integer greater than 0:
  81. 6
  82. >> Enter 's' to compute the sum, 'p' to compute the product.
  83. p
  84. The product of the integers between 1 and 6 is 120.
  85. ```
  86.  
  87. ### Problem Set #3
  88.  
  89. 1. Write a method that returns one UUID when called with no parameters.
  90.  
  91. 2. Determine if an input string is an IP address representing dot-separated numbers. e.g. "10.4.5.11".
  92.  
  93. 3. Create a hash that expresses the frequency with which each letter occurs in the string: "The Flintstones Rock".
  94.  
  95. 4. This will fail if you call this with an input of 0 or a negative number. Refactor.
  96. ```ruby
  97. def factors(number)
  98. dividend = number
  99. divisors = []
  100. begin
  101. divisors << number / dividend if number % dividend == 0
  102. dividend -= 1
  103. end until dividend == 0
  104. divisors
  105. end
  106. ```
  107.  
  108. 8. Given the munsters hash below. Modify the hash such that each member of the Munster family has an additional "age_group" key that has one of three values describing the age group the family member is in (kid, adult, or senior).
  109. ```ruby
  110. munsters = {
  111. "Herman" => { "age" => 32, "gender" => "male" },
  112. "Lily" => { "age" => 30, "gender" => "female" },
  113. "Grandpa" => { "age" => 402, "gender" => "male" },
  114. "Eddie" => { "age" => 10, "gender" => "male" },
  115. "Marilyn" => { "age" => 23, "gender" => "female"}
  116. }
  117.  
  118. ## Your solution should produce the hash below
  119.  
  120. { "Herman" => { "age" => 32, "gender" => "male", "age_group" => "adult" },
  121. "Lily" => {"age" => 30, "gender" => "female", "age_group" => "adult" },
  122. "Grandpa" => { "age" => 402, "gender" => "male", "age_group" => "senior" },
  123. "Eddie" => { "age" => 10, "gender" => "male", "age_group" => "kid" },
  124. "Marilyn" => { "age" => 23, "gender" => "female", "age_group" => "adult" } }
  125. ```
  126.  
  127. 9. Figure out the total age of just the male members of the family.
  128. ```ruby
  129. munsters = {
  130. "Herman" => { "age" => 32, "gender" => "male" },
  131. "Lily" => { "age" => 30, "gender" => "female" },
  132. "Grandpa" => { "age" => 402, "gender" => "male" },
  133. "Eddie" => { "age" => 10, "gender" => "male" }
  134. }
  135. ```
  136.  
  137. 12. Reverse this sentence:
  138.  
  139. "Humpty Dumpty sat on a wall."
  140.  
  141. It should look like this:
  142.  
  143. "wall a on sat Dumpty Humpty."
  144.  
  145. 13. Write a method that rotates an array by moving the first element to the end of the array. The original array should not be modified.
  146.  
  147. Do not use the method Array#rotate or Array#rotate! for you implementation.
  148.  
  149. Example:
  150. ```ruby
  151. rotate_array([7, 3, 5, 2, 9, 1]) == [3, 5, 2, 9, 1, 7]
  152. rotate_array(['a', 'b', 'c']) == ['b', 'c', 'a']
  153. rotate_array(['a']) == ['a']
  154.  
  155. x = [1, 2, 3, 4]
  156. rotate_array(x) == [2, 3, 4, 1] # => true
  157. x == [1, 2, 3, 4] # => true
  158. ```
  159.  
  160. 14. Write a method that can rotate the last n digits of a number. For example:
  161. ```ruby
  162. rotate_rightmost_digits(735291, 1) == 735291
  163. rotate_rightmost_digits(735291, 2) == 735219
  164. rotate_rightmost_digits(735291, 3) == 735912
  165. rotate_rightmost_digits(735291, 4) == 732915
  166. rotate_rightmost_digits(735291, 5) == 752913
  167. rotate_rightmost_digits(735291, 6) == 352917
  168. ```
  169. Note that rotating just 1 digit leaves the number results in the original number being returned.
  170.  
  171. You may use the rotate_array method from the previous exercise if you want.
  172.  
  173. 15. If you take a number like 735291, and rotate it to the left, you get 352917. If you now keep the first digit fixed in place, and rotate the remaining digits, you get 329175. Keep the first 2 digits fixed in place and rotate again to 321759. Keep the first 3 digits fixed in place and rotate again to get 321597. Finally, keep the first 4 digits fixed in place and rotate the final 2 digits to get 321579. The resulting number is called the maximum rotation of the original number.
  174.  
  175. Write a method that takes an integer as argument, and returns the maximum rotation of that argument. You can (and probably should) use the rotate_rightmost_digits method from the previous exercise.
  176.  
  177. Example:
  178. ```ruby
  179. max_rotation(735291) == 321579
  180. max_rotation(3) == 3
  181. max_rotation(35) == 53
  182. max_rotation(105) == 15 # the leading zero gets dropped
  183. max_rotation(8_703_529_146) == 7_321_609_845
  184. ```
  185.  
  186. 14. There is a row switches before you numbered from 1 to 1000. Each switch is connected to exactly one light that is initially off. You walk down the row of switches, and turn every one of them on. Then, you go back to the beginning and toggle switches 2, 4, 6, and so on. Repeat for every 3rd, 4th, 5th, 6th and so on, and keep going until you have been through 1000 repetitions of this process.
  187.  
  188. Write a program that determines how many lights are on at the end, and which lights are on.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement