Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.
- def diff21(n)
- # difference between n and 21
- temp = n - 21
- # get absolute value of result in 1
- new_temp = temp.abs
- # if n > 21 then return 2 * result
- if n > 21
- return 2 * new_temp
- OR
- ans = 2 * new_temp
- end
- ans
- end
- SHORT VERSION
- def diff21(n)
- n > 21 ? (21 - n).abs * 2 : (21 - n)
- end
- 2.
- def monkey_trouble(a, b)
- # if a and b are true or a and b are false
- # then return true
- # if a is opposite to b
- # then return false
- if (a == true and b == true) || (a == false and b == false) #can also use or instead of ||
- true
- else
- false
- end
- end
- 6.
- nums = [1, 2, 7, 1]
- def has267(num)
- #contains a 2, 7, 1 pattern
- #comparing an array with another array
- #if array size < 3
- if (nums.size < 3)
- return false
- else
- list = nums*"" # asterisk converts array into string
- if list.include?("271")
- return true
- end
- return false
- end
- #if array contains 2, 7, 1
- end
- puts has267([1,2,7,1])
- class definitions
- use ruby car.rb
- class Car
- def drive()
- puts "vroom vroom"
- end
- def stop()
- puts "screehh"
- end
- def start()
- puts "rrr"
- end
- end
- require './car.rb'
- c1 = Car.new
- c1.start()
- c1.drive()
- c2 = Car.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement