Skip to content

Commit 7cfc0fd

Browse files
committed
exercism 76 List Ops
1 parent 350bc5a commit 7cfc0fd

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
297297
73. [Secret Handshake](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/secret_handshake.rb)
298298
74. [Atbash Cipher](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/atbash_cipher.rb)
299299
75. [Crypto Square](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/crypto_square.rb)
300+
76. [List Ops](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/list_ops.rb)
300301

301302
<a name="leetcode"/>
302303

exercism/list-ops.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/list-ops
2+
3+
# Solution
4+
class ListOps
5+
def self.arrays(arr)
6+
len=0
7+
arr.each do |x|
8+
len+=1
9+
end
10+
len
11+
end
12+
13+
def self.reverser(arr)
14+
return [] if arr==[]
15+
len = self.arrays(arr)
16+
i=0
17+
j=len-1
18+
while i<j
19+
arr[i],arr[j] = arr[j],arr[i]
20+
i+=1
21+
j-=1
22+
end
23+
arr
24+
end
25+
26+
def self.concatter(arr1,arr2)
27+
return arr1||arr2 if arr1==[] || arr2==[]
28+
arr2.each do |num|
29+
arr1.append(num)
30+
end
31+
arr1
32+
end
33+
34+
def self.mapper(arr,&block)
35+
return [] if arr==[]
36+
new_arr=[]
37+
arr.each do |x|
38+
val=block.call(x)
39+
new_arr.append(val)
40+
end
41+
new_arr
42+
end
43+
44+
def self.filterer(arr,&block)
45+
return [] if arr==[]
46+
new_arr=[]
47+
arr.each do |x|
48+
new_arr.append(x) if block.call(x)
49+
end
50+
new_arr
51+
end
52+
53+
def self.sum_reducer(arr)
54+
return 0 if arr==[]
55+
sum=0
56+
arr.each do |x|
57+
sum+=x
58+
end
59+
sum
60+
end
61+
62+
def self.factorial_reducer(arr)
63+
return 1 if arr==[]
64+
factorial=1
65+
arr.each do |x|
66+
factorial*=x
67+
end
68+
factorial
69+
end
70+
end

0 commit comments

Comments
 (0)