-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.rb
More file actions
111 lines (87 loc) · 1.51 KB
/
solution.rb
File metadata and controls
111 lines (87 loc) · 1.51 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Implementation of our own Enumerable class
module MyEnumerable
def map
Array.new.tap do |arr|
each do |element|
value = yield element
arr << value
end
end
end
def filter
Array.new.tap do |arr|
each do |element|
arr << element if (yield element)
end
end
end
def first
element = nil
each do |x|
element = x
break
end
element
end
def reduce(initial = nil)
skip_first = false
if initial.nil?
initial = first
skip_first = true
end
each do |x|
if skip_first
skip_first = false
next
end
initial = yield initial, x
end
initial
end
def negate_block(&block)
proc { |x| !block.call(x) }
end
def reject(&block)
filter(negate_block(&block))
end
def size
map { |_| 1 }.reduce(0, &:+)
end
def any?(&block)
filter(&block).size > 0
end
def all?(&block)
filter(&block).size == size
end
def include?(element)
# Your code goes here
end
def count(element = nil)
return size if element.nil?
filter { |x| x == element }.size
end
def min
# Your code goes here.
end
def min_by
# Your code goes here.
end
def max
# Your code goes here.
end
def max_by
# Your code goes here.
end
def take(n)
# Your code goes here.
end
def take_while
# Your code goes here.
end
def drop(n)
# Your code goes here.
end
def drop_while
# Your code goes here.
end
end