-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.rb
54 lines (41 loc) · 1.07 KB
/
2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# q :- 2. Write the following functions to extend the array class, by adding the methods:
# square()
# cube()
# divisible_by(x)
# strictly_above(x)
# strictly_below(x)
# Examples
# [1, 2, 3].square ➞ [1, 4, 9]
# [1, 2, 3].cube ➞ [1, 8, 27]
# [1, 2, 3, 4].divisible_by(2) ➞ [2, 4]
# [1, 2, 3, 4].strictly_above(1) ➞ [2, 3, 4]
# [1, 2, 3, 4].strictly_below(2) ➞ [1]
class Array
def square
self.map { |ij| ij.to_i * ij.to_i }
end
def cube
self.map { |ij| ij.to_i * ij.to_i * ij.to_i }
end
def divisible_by by
byy = by.to_i
self.map do |ij|
if (ij.to_i % byy == 0)
ij.to_i
end
end
end
def strictly_above by
by = by.to_i
self.select { |i| i > by }
end
def strictly_below by
by = by.to_i
self.select { |i| i < by }
end
end
puts [1, 2, 3].square
puts [1, 2, 3].cube
puts [1, 2, 3, 4, 5, 6].divisible_by 2
puts [1, 2, 3, 4].strictly_above 2
puts [1, 2, 3, 4].strictly_below 3