Skip to content

Commit 58250dc

Browse files
author
Aman Kumar
committed
run rubocop
1 parent c1632ad commit 58250dc

36 files changed

+1074
-1185
lines changed
+60-61
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,76 @@
11
# How to find common elements in three sorted array?
22

3-
43
def find_common_elements(ar1, ar2, ar3)
5-
common = []
6-
l1 = ar1.length
7-
l2 = ar2.length
8-
l3 = ar3.length
9-
i,j,k = 0,0,0
10-
common = []
11-
while i<l1 && j<l2 && k<l3
12-
if (ar1[i] == ar2[j] && ar2[j] == ar3[k])
13-
common.push ar1[i]
14-
i += 1
15-
j += 1
16-
k += 1
17-
elsif(ar1[i] < ar2[j])
18-
i += 1
19-
elsif (ar2[j] < ar3[k])
20-
j += 1
21-
else
22-
k += 1
23-
end
24-
end
25-
common
4+
common = []
5+
l1 = ar1.length
6+
l2 = ar2.length
7+
l3 = ar3.length
8+
i = 0
9+
j = 0
10+
k = 0
11+
common = []
12+
while i < l1 && j < l2 && k < l3
13+
if ar1[i] == ar2[j] && ar2[j] == ar3[k]
14+
common.push ar1[i]
15+
i += 1
16+
j += 1
17+
k += 1
18+
elsif ar1[i] < ar2[j]
19+
i += 1
20+
elsif ar2[j] < ar3[k]
21+
j += 1
22+
else
23+
k += 1
24+
end
25+
end
26+
common
2627
end
2728

28-
29-
FIXNUM_MIN = -(2**(0.size * 8 -2))
29+
FIXNUM_MIN = -(2**(0.size * 8 - 2))
3030

3131
def find_common_with_duplicates(ar1, ar2, ar3)
32-
common = []
33-
l1 = ar1.length - 1
34-
l2 = ar2.length - 1
35-
l3 = ar3.length - 1
36-
i,j,k = 0,0,0
37-
prev1, prev2, prev3 = FIXNUM_MIN, FIXNUM_MIN, FIXNUM_MIN
38-
while i<l1 && j<l2 && k<l3
39-
while (ar1[i] == prev1 && i<l1)
40-
i += 1
41-
end
42-
while(ar2[j] == prev2 && j < l2)
43-
j += 1
44-
end
45-
while(ar3[k] == prev3 && k < l3)
46-
k += 1
47-
end
32+
common = []
33+
l1 = ar1.length - 1
34+
l2 = ar2.length - 1
35+
l3 = ar3.length - 1
36+
i = 0
37+
j = 0
38+
k = 0
39+
prev1 = FIXNUM_MIN
40+
prev2 = FIXNUM_MIN
41+
prev3 = FIXNUM_MIN
42+
while i < l1 && j < l2 && k < l3
43+
i += 1 while ar1[i] == prev1 && i < l1
44+
j += 1 while ar2[j] == prev2 && j < l2
45+
k += 1 while ar3[k] == prev3 && k < l3
4846

49-
puts [i,j,k].to_s
47+
puts [i, j, k].to_s
5048

51-
if(ar1[i] == ar2[j] && ar2[j] == ar3[k])
52-
common << ar1[i]
53-
prev1, prev2, prev3 = ar1[i], ar1[i], ar1[i]
54-
i = i + 1
55-
j = j + 1
56-
k = k + 1
57-
elsif ar1[i] < ar2[j]
58-
prev1 = ar1[i]
59-
i += 1
60-
elsif (ar2[j] < ar3[k])
61-
prev2 = ar2[j]
62-
j += 1
63-
else
64-
prev3 = ar3[k]
65-
k += 1
66-
end
67-
end
68-
common
49+
if ar1[i] == ar2[j] && ar2[j] == ar3[k]
50+
common << ar1[i]
51+
prev1 = ar1[i]
52+
prev2 = ar1[i]
53+
prev3 = ar1[i]
54+
i += 1
55+
j += 1
56+
k += 1
57+
elsif ar1[i] < ar2[j]
58+
prev1 = ar1[i]
59+
i += 1
60+
elsif ar2[j] < ar3[k]
61+
prev2 = ar2[j]
62+
j += 1
63+
else
64+
prev3 = ar3[k]
65+
k += 1
66+
end
67+
end
68+
common
6969
end
70-
ar1 = [1, 5, 10, 20, 40, 80, 80, 80, 80 ]
70+
ar1 = [1, 5, 10, 20, 40, 80, 80, 80, 80]
7171
ar2 = [6, 7, 20, 80, 80, 80, 100]
7272
ar3 = [3, 4, 15, 20, 30, 70, 80, 120, 120, 120]
7373

7474
puts find_common_with_duplicates(ar1, ar2, ar3).to_s
7575

76-
7776
# Load Stock pricing:
+10-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# delete dublicate sorted array
22

3-
43
def delete_duplicate(ar)
5-
k = 1
6-
for i in 1..ar.length
7-
if(ar[i] != ar[i-1])
8-
ar[k] = ar[i]
9-
k = k + 1
10-
end
11-
end
12-
ar[0...k-1]
4+
k = 1
5+
(1..ar.length).each do |i|
6+
if ar[i] != ar[i - 1]
7+
ar[k] = ar[i]
8+
k += 1
9+
end
10+
end
11+
ar[0...k - 1]
1312
end
1413

15-
16-
puts delete_duplicate([1,1,2,3,4,4,5,5,5]).to_s
17-
puts delete_duplicate([1,2,3,4,4,4,5,5,5]).to_s
14+
puts delete_duplicate([1, 1, 2, 3, 4, 4, 5, 5, 5]).to_s
15+
puts delete_duplicate([1, 2, 3, 4, 4, 4, 5, 5, 5]).to_s

arrays/find_dublicates_in_array.rb

+23-34
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
# Find duplicates in a given array when elements are not limited to a range
22

3-
43
def findDuplicates(ar)
5-
dublicates = []
6-
ar_hash = {}
7-
size = ar.length
8-
ar.each_with_index do |val, index|
9-
10-
[index+1..size].each do |ind|
11-
if(ar[index] == val)
12-
ar_hash[val] = ar_hash[val].to_i + 1
13-
end
14-
end
15-
end
16-
ar_hash.each do |k, v|
17-
if v > 1
18-
dublicates.push k
19-
end
20-
end
21-
dublicates.to_s
4+
dublicates = []
5+
ar_hash = {}
6+
size = ar.length
7+
ar.each_with_index do |val, index|
8+
[index + 1..size].each do |_ind|
9+
ar_hash[val] = ar_hash[val].to_i + 1 if ar[index] == val
10+
end
11+
end
12+
ar_hash.each do |k, v|
13+
dublicates.push k if v > 1
14+
end
15+
dublicates.to_s
2216
end
2317

24-
2518
def findDuplicatesWithHash(ar)
26-
dublicates = []
27-
ar_hash = {}
28-
size = ar.length
29-
ar.each_with_index do |val, index|
30-
ar_hash[val] = ar_hash[val].to_i + 1
31-
end
32-
ar_hash.each do |k, v|
33-
if v > 1
34-
dublicates.push k
35-
end
36-
end
37-
dublicates.to_s
19+
dublicates = []
20+
ar_hash = {}
21+
size = ar.length
22+
ar.each_with_index do |val, _index|
23+
ar_hash[val] = ar_hash[val].to_i + 1
24+
end
25+
ar_hash.each do |k, v|
26+
dublicates.push k if v > 1
27+
end
28+
dublicates.to_s
3829
end
3930

40-
41-
42-
puts findDuplicatesWithHash([2, 10,10, 100, 2, 10, 11,2,11,2])
31+
puts findDuplicatesWithHash([2, 10, 10, 100, 2, 10, 11, 2, 11, 2])

arrays/first_non_repeating_char.rb

+41-50
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,53 @@
11
# Given a string, find its first non-repeating character
2-
# Given a string, find the first non-repeating character in it.
3-
# For example, if the input string is “GeeksforGeeks”, then the output should be ‘f’ and if the input string is “GeeksQuiz”, then the output should be ‘G’.
4-
2+
# Given a string, find the first non-repeating character in it.
3+
# For example, if the input string is “GeeksforGeeks”, then the output should be ‘f’ and if the input string is “GeeksQuiz”, then the output should be ‘G’.
54

65
def first_non_repeating(str)
7-
ar = Array.new(256, 0)
8-
str.each_char {|c| ar[c.ord] += 1}
9-
str.each_char do |c|
10-
if(ar[c.ord] == 1)
11-
return c
12-
end
13-
end
14-
return nil
6+
ar = Array.new(256, 0)
7+
str.each_char { |c| ar[c.ord] += 1 }
8+
str.each_char do |c|
9+
return c if ar[c.ord] == 1
10+
end
11+
nil
1512
end
1613

17-
18-
1914
def first_non_repeating_optimized(str)
20-
ar = Array.new(256) { [0, -1]}
21-
str.split('').each_with_index do |c, index|
22-
ar[c.ord][0] += 1
23-
ar[c.ord][1] = index if ar[c.ord][1] == -1
24-
end
25-
ar[0][1] = -11
26-
27-
first = nil
28-
min_index = str.length
29-
ar.each_with_index do |arr, index|
30-
31-
if(arr[0] == 1 && arr[1] < min_index)
32-
first = index.chr
33-
min_index = [min_index , arr[1]].min
34-
end
35-
end
36-
return first
15+
ar = Array.new(256) { [0, -1] }
16+
str.split('').each_with_index do |c, index|
17+
ar[c.ord][0] += 1
18+
ar[c.ord][1] = index if ar[c.ord][1] == -1
19+
end
20+
ar[0][1] = -11
21+
22+
first = nil
23+
min_index = str.length
24+
ar.each_with_index do |arr, index|
25+
if arr[0] == 1 && arr[1] < min_index
26+
first = index.chr
27+
min_index = [min_index, arr[1]].min
28+
end
29+
end
30+
first
3731
end
3832

39-
4033
# INT_MAX = (8*8)**2
4134
def first_non_repeating_optimized2(str)
42-
ar = Array.new(256) { -1}
43-
str.split('').each_with_index do |c, index|
44-
if(ar[c.ord] == -1)
45-
ar[c.ord] = index
46-
else
47-
ar[c.ord] = -2
48-
end
49-
end
50-
51-
first = str.length
52-
for e in ar
53-
if e >=0
54-
first = [first, e].min
55-
end
56-
end
57-
str[first]
35+
ar = Array.new(256) { -1 }
36+
str.split('').each_with_index do |c, index|
37+
ar[c.ord] = if ar[c.ord] == -1
38+
index
39+
else
40+
-2
41+
end
42+
end
43+
44+
first = str.length
45+
ar.each do |e|
46+
first = [first, e].min if e >= 0
47+
end
48+
str[first]
5849
end
5950

60-
puts first_non_repeating_optimized("GeeksforGeeks")
61-
puts first_non_repeating_optimized("Geeks")
62-
puts first_non_repeating_optimized("aman")
51+
puts first_non_repeating_optimized('GeeksforGeeks')
52+
puts first_non_repeating_optimized('Geeks')
53+
puts first_non_repeating_optimized('aman')

arrays/first_repeating_element.rb

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
# first repeating element
22

3-
4-
5-
63
def first_repeating_element(ar)
7-
8-
ar_hash = {}
9-
for e in ar
10-
ar_hash[e] = ar_hash[e].to_i + 1
11-
end
12-
h = ar_hash.find { |key,value| value > 1 }
13-
h[0]
4+
ar_hash = {}
5+
ar.each do |e|
6+
ar_hash[e] = ar_hash[e].to_i + 1
7+
end
8+
h = ar_hash.find { |_key, value| value > 1 }
9+
h[0]
1410
end
1511

16-
1712
ar = [10, 5, 3, 4, 3, 5, 6]
1813
# puts first_repeating_element(ar)
1914

20-
puts first_repeating_element([6, 10, 5, 4, 9, 120, 4, 6, 10])
15+
puts first_repeating_element([6, 10, 5, 4, 9, 120, 4, 6, 10])

arrays/getMissing1to100Xor.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# How to find the missing number in an integer array of 1 to 100?
22

33
def getMissingNo(ar)
4-
a = 0
5-
b = 0
6-
ar.each{|e| a = a ^ e}
7-
len = ar.length
8-
(0..len+1).each{|e| b = b ^ e}
9-
return a ^ b
4+
a = 0
5+
b = 0
6+
ar.each { |e| a = a ^ e }
7+
len = ar.length
8+
(0..len + 1).each { |e| b = b ^ e }
9+
a ^ b
1010
end
1111

1212
ar = [1, 2, 3, 4, 6, 7, 9, 8, 10]
1313
print getMissingNo(ar)
14-

0 commit comments

Comments
 (0)