Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1
- # inputs [1,2,3,4,5], 2
- # outputs [2,4,6,8,10]
- def do_something(arr_of_numbers, multiplier)
- result = arr_of_numbers.map do |num|
- num * multiplier
- end
- return result
- # result = []
- # arr_of_numbers.each do |num|
- # result << (num * multiplier)
- # end
- end
- # 2
- # input1 [2,3,4,5,6], 3
- do_something([2,3,4,5,6], 3)
- # 3
- class Student
- attr_accessor :name
- # attr_reader :name
- # attr_writer :name
- def initialize(name, age, gender)
- @name = name
- @age = age
- @gender = gender
- end
- end
- # 4
- harrison = Student.new('harrison', 24, 'male')
- # 5
- tess = Student.new('tess', 25, 'female')
- tess.name = 'Tess'
- # 6
- def first_i_was_afraid(arr_of_strings, word_to_check)
- # arr_of_strings.each do |word|
- # if word == word_to_check
- # return true
- # end
- # end
- # return false
- return arr_of_strings.include?(word_to_check)
- end
- first_i_was_afraid(['i', 'will', 'survive'], 'survive')
- # false
- # 7
- def div_by_7?(num)
- if num % 7 == 0
- return true
- else
- return false
- end
- end
- div_by_7?(14)
- # 8
- def array_splitter(array_of_nums)
- div_by_7 = []
- not_div_by_7 = []
- array_of_nums.each do |num|
- if div_by_7?(num)
- div_by_7 << num
- else
- not_div_by_7 << num
- end
- end
- return [div_by_7, not_div_by_7]
- end
- array_splitter([1,2,3,4,5,6,7])
- # 9
- def closing_time(name)
- return "Thanks #{name} for everything"
- end
- closing_time('harrison')
- # 10
- def pizza_time(string)
- if string.include?('pizza')
- return 'Pizza time'
- else
- return 'Meatloaf'
- end
- end
- pizza_time('I got 99 problems but a pizza aint one')
- # Pizza time
- pizza_time('Home now')
- # Mealoaf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement