Advertisement
solidsnake

11/17

Nov 16th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.18 KB | None | 0 0
  1. 1.
  2. def diff21(n)
  3. # difference between n and 21
  4. temp = n - 21
  5. # get absolute value of result in 1
  6. new_temp = temp.abs
  7. # if n > 21 then return 2 * result
  8. if n > 21
  9.     return 2 * new_temp
  10.     OR
  11.     ans = 2 * new_temp
  12. end
  13. ans
  14. end
  15.  
  16. SHORT VERSION
  17. def diff21(n)
  18.     n > 21 ? (21 - n).abs * 2 : (21 - n)
  19. end
  20.  
  21. 2.
  22. def monkey_trouble(a, b)
  23.     # if a and b are true or a and b are false
  24.     # then return true
  25.     # if a is opposite to b
  26.     # then return false
  27.     if (a == true and b == true) || (a == false and b == false) #can also use or instead of ||
  28.         true
  29.     else
  30.         false
  31.     end
  32. end
  33.  
  34. 6.
  35. nums = [1, 2, 7, 1]
  36. def has267(num)
  37.     #contains a 2, 7, 1 pattern
  38.     #comparing an array with another array
  39.     #if array size < 3
  40.     if (nums.size < 3)
  41.         return false
  42.     else
  43.         list = nums*"" # asterisk converts array into string
  44.         if list.include?("271")
  45.             return true
  46.         end
  47.  
  48.         return false
  49.  
  50.     end
  51.     #if array contains 2, 7, 1
  52. end
  53.  
  54. puts has267([1,2,7,1])
  55.  
  56. class definitions
  57. use ruby car.rb
  58.  
  59. class Car
  60.     def drive()
  61.         puts "vroom vroom"
  62.     end
  63.    
  64.     def stop()
  65.         puts "screehh"
  66.     end
  67.  
  68.     def start()
  69.         puts "rrr"
  70.     end
  71. end
  72.  
  73. require './car.rb'
  74.  
  75. c1 = Car.new
  76. c1.start()
  77. c1.drive()
  78. c2 = Car.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement