-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.rb
More file actions
115 lines (77 loc) · 1.54 KB
/
Copy pathmethods.rb
File metadata and controls
115 lines (77 loc) · 1.54 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
112
113
114
115
# 'ciucik'.each_char { |char| puts char }
# appending to array:
# array = []
# array << 'ciucik'
# array << 'to'
# array << 'pies'
def my_split(string, delimiter)
array = []
el = ''
string.each_char{|char|
if char != delimiter
el = el + char
else
array << el
el = ''
end
}
array << el
array
end
# turns array into string, using separator
def my_join(array, separator)
string = ''
array.each_with_index do |elem, index|
if index < array.length - 1
string << elem + separator
else
string << elem
end
end
string
end
# returns sum of array elements (elements are numbers)
def my_sum(array)
return nil if array.length == 0
sum = 0
array.each do |elem|
sum = sum + elem
end
sum
end
# returns product of array elements (elements are numbers)
# (product is result of multiplication)
def my_product(array)
return nil if array.length == 0
product = 1
array.each do |elem|
product = product * elem
end
product
end
# return array which elems are multiplied by `num`
def multiply_all_by(array, num)
result = []
array.each do |elem|
result << elem * num
end
result
end
def my_map(array)
result = []
array.each do |elem|
result << yield(elem)
end
result
end
def add_to_all(array, num)
result = []
array.each do |elem|
result << elem + num
end
result
end
# checks if all elements are divided by `num`
# def all_divided_by(array, num)
# checks if any element is divided by `num`
# def any_divided_by(array, num)