Guest User

Untitled

a guest
Nov 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. ## Create a method new_cat which takes as its input two strings. It should concatenate the strings and return the concatenated value, without modifying either of the input strings. Call this method and send your first and last name as arguments.
  2.  
  3. def new_cat(string1, string2)
  4. "#{string1}#{string2}"
  5. end
  6.  
  7. puts new_cat("michael", "kaiser-nyman")
  8.  
  9.  
  10. ## Write a method called sum which takes as its input an array of integers and returns their sum.
  11.  
  12. def sum(numbers)
  13. numbers.inject { |memo, number| memo + number }
  14. end
  15.  
  16. puts sum([1,2,3,4])
  17.  
  18.  
  19. ## Consider the following array:
  20. ##
  21. ## ["ruby", "is", "the", "best", "programming" ,"language", "ever"]
  22. ##
  23. ## What value is stored at index 3? What is the index of the word "ruby?" What is the length of the array?
  24.  
  25. # "best", 0, 7
  26.  
  27.  
  28. ## Create a new instance method on the String class called fun_string! which is called on a String object and permanently modifies it by capitalizing the even characters (counting from 1) and then reversing the string. See if you can do it in a single line.
  29.  
  30. class String
  31. def fun_string!
  32. self.split("").each_with_index { |character, index| index.odd? ? character.upcase! : character }.join.reverse!
  33. end
  34. end
  35.  
  36. puts s = "apples".fun_string!
  37. puts s
  38.  
  39.  
  40. ## Write a method called fizzblam that prints the numbers from 1 to 100. But for multiples of 5 print "Fizz" instead of the number and for the multiples of 7 print "Blam". For numbers which are multiples of both 5 and 7 print "FizzBlam."
  41.  
  42. def fizzblam
  43. (1..100).each do |number|
  44. if number % 35 == 0
  45. puts "FizzBlam"
  46. elsif number % 7 == 0
  47. puts "Blam"
  48. elsif number % 5 == 0
  49. puts "Fizz"
  50. else
  51. puts number
  52. end
  53. end
  54. end
  55.  
  56. fizzblam
  57.  
  58.  
  59. ## Create a GuessingGame class which is initialized with an integer called "answer."
  60.  
  61. ## Define an instance method GuessingGame#guess which takes an integer called "guess" as its input. guess should return the symbol :high if the guess is larger than the answer, :correct if the guess is equal to the answer, and :low if the guess is lower than the answer.
  62.  
  63. ## Define an instance method GuessingGame#solved? which returns false until a correct guess has been given.
  64.  
  65. class GuessingGame
  66. def initialize(answer)
  67. @answer = answer
  68. @solved = false
  69. end
  70.  
  71. def guess(guess)
  72. if guess > @answer
  73. :high
  74. elsif guess < @answer
  75. :low
  76. else
  77. @solved = true
  78. :correct
  79. end
  80. end
  81.  
  82. def solved?
  83. @solved
  84. end
  85. end
  86.  
  87. puts game = GuessingGame.new(6)
  88. puts game.solved?
  89. puts game.guess(5)
  90. puts game.guess(7)
  91. puts game.guess(6)
  92. puts game.solved?
  93.  
  94.  
  95. ## Explain the four levels of variable scope in Ruby and give a brief description of how you would use each.
  96.  
  97. # local variable: available only within the method or block in which it was defined. use unless you have a good reason not to.
  98. # instance variable: available within the entire instance of the class, and available to all methods and blocks within the class. use if the variable needs to be accessed in multiple methods.
  99. # class variable: available to all instances of the class and the class itself. use to track class-wide information (only example i can think of is how many times a particular method has been run, but surely there are more uses).
  100. # global variable: available to everything, everywhere, every time. don't use unless you are shaquille o'neal.
  101.  
  102.  
  103. ## Using proper terminology, explain the difference between Array#each, Array#map, and Array#select. Write clearly, using examples if necessary, as if you were explaining it to a friend.
  104.  
  105. # i don't have friends. but here goes nothing. each lets you run through each element in an array and do stuff with it. when it's done, it returns the original array. map is like each, but instead of returning the orginal array, it returns an array with the results of each pass through in an array. select runs through each element of an array and lets you do stuff with it. if the result of what you do is true, then the element gets added to an array that is returned at the end. damn.
  106.  
  107.  
  108. ## Consider the following code examples.... For each example, explain in a one or two plain-English sentences what the given method does when passed an array of positive integers. Choose one method (not the first!) and refactor it into something shorter. Give the working code of the refactored method.
  109.  
  110. # 1. it displays each number in the array multiplied by 100.
  111. # 2. it returns the largest number in the array.
  112. # 3. it returns an array like the original one but with 80 added to each number.
  113. # 4. it returns an array containing only the even elements of the first array.
  114.  
  115. def method_4(array)
  116. array.select { |number| number % 2 == 0}
  117. end
  118.  
  119. p method_4([1,2,3,4,5,6])
  120.  
  121.  
  122. ## Explain why the following code examples raise an exception, and how you’d need to change the code to fix the error. Offer multiple solutions if you know of any.
  123.  
  124. # 1. the initialize method requires 1 argument, and line 7 calls the new method (which calls initialize) with no arguments. give it an argument, yo!
  125. # 2. there is no num= method for the Foo class. create one, or use attr_writer :num.
  126. # 3. uh, still no num= method. change attr_reader :num to attr_accessor :num. c'mon, guys.
  127.  
  128.  
  129. ## Consider the following method.... What will the following code print to the console and why?
  130.  
  131. # hash = {:dog => 'woof', 'cat' => 'meow', 'duck' => 'quack'}
  132. #
  133. # print_value(hash, :dog) -> "woof" because it is accessing the hash with the :dog key, which has value 'woof'.
  134. # print_value(hash, 'dog') -> nil because it is accessing the hash with the 'dog' key, which is not in the hash. the :dog symbol is, but not the 'dog' string.
  135. # print_value(hash, 'cat') -> 'meow' because it is accessing the hash with the 'cat' key, which has value 'meow'.
  136. # print_value(hash, 'quack') -> nil because it is accessing the hash with the 'quack' key, which is not in the hash. 'quack' is in the has as a value, but not a key.
  137. # print_value(hash, :duck) -> nil because it is accessing the hash with the :duck key, which is not in the hash. the 'duck' string is, but not the :duck symbol.
  138.  
  139.  
  140. ## Explain in plain English the difference between return and puts. You’re free to use code examples to illustrate your point.
  141.  
  142. # when Ruby runs a line or a block of code, it returns the results of its calculations and musings (what we call "evaluation") to whatever called it. return lets you tell Ruby exactly what you want it to return. by default, it returns the last thing it evaluates. puts lets us tell Ruby to display something on the screen. Ruby doesn't ever display something on the screen unless we tell it to.
  143.  
  144. # for example, [1,2,3].each { |number| puts number ** 2 } will display the square of each number on the screen. however, the return will be [1,2,3] because the each method returns the array on which it was called.
  145.  
  146.  
  147. # How long does fib_slow(40) take to execute? How long does fib_fast(40) take to execute? Why is fib_fast so much faster than fib_slow? Hint: set t1=Time.now then run the code then set t2=Time.now , and find t2-t1 for the time.
  148.  
  149.  
  150. def fib_slow(n)
  151. return 0 if n == 0
  152. return 1 if n == 1
  153. return fib_slow(n-1) + fib_slow(n-2)
  154. end
  155.  
  156. def fib_fast(n)
  157. fib = [0,1]
  158.  
  159. 2.upto(n).each do |i|
  160. last = fib[1]
  161. fib_n = fib[1] + fib[0]
  162. fib = [last, fib_n]
  163. end
  164.  
  165. return fib[1]
  166. end
  167.  
  168. t1 = Time.now
  169. fib_slow(30)
  170. t2 = Time.now
  171. puts running_time = t2 - t1
  172.  
  173. # running_time = 0.335455
  174.  
  175. t1 = Time.now
  176. fib_fast(30)
  177. t2 = Time.now
  178. puts running_time = t2 - t1
  179.  
  180. # running_time = 2.7e-05
  181.  
  182. # fib_slow calls itself recursively twice each time it runs. and each time it runs, it loads itself into memory. so for fib_slow(n), it loads itself into memory on the magnitude of 2 ** n. fib_fast isn't recursive and so only loads itself once. boo-ya.
Add Comment
Please, Sign In to add comment