Skip to content

Commit fef92b3

Browse files
committed
exercism 59
1 parent 8e73f2f commit fef92b3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
276276
56. [Collatz Conjecture](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/collatz_conjecture.rb)
277277
57. [Seive](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/seive.rb)
278278
58. [Proverb](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/proverb.rb)
279+
59. [Palindrome Products](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/palindrome_products.rb)
279280

280281
<a name="leetcode"/>
281282

Diff for: exercism/palindrome_products.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/palindrome-products
2+
3+
#Solution
4+
class Palindromes
5+
attr_accessor :value,:factors
6+
def initialize(max_factor:,min_factor:nil)
7+
@max_factor = max_factor ? max_factor : 1
8+
@min_factor = min_factor ? min_factor : 1
9+
@palindromes = []
10+
end
11+
12+
def generate
13+
for i in @min_factor..@max_factor
14+
for j in i..@max_factor
15+
@palindromes.push(i*j) if (i*j).to_s == (i*j).to_s.reverse
16+
end
17+
end
18+
@palindromes
19+
end
20+
21+
def largest
22+
value = @palindromes.sort!.last
23+
factors = get_factors(value)
24+
largest_pal = Struct.new(:value,:factors)
25+
largest_pal.new(value,factors)
26+
end
27+
28+
def smallest
29+
value = @palindromes.first
30+
factors = get_factors(value)
31+
smallest_pal = Struct.new(:value,:factors)
32+
smallest_pal.new(value,factors)
33+
end
34+
private
35+
36+
def get_factors(num)
37+
factors = []
38+
for i in @min_factor..@max_factor
39+
factors.push([i,num/i]) if num%i == 0 and (num/i)>=@min_factor and (num/i)<=@max_factor and (num/i)>=i
40+
end
41+
factors
42+
end
43+
end

0 commit comments

Comments
 (0)