Skip to content

Commit 01bd56e

Browse files
committed
exercism 50-55
1 parent 9986cca commit 01bd56e

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
268268
48. [Flatten Array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/flatten_array.rb)
269269
49. [Phone Number](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/phone_number.rb)
270270
50. [Grains](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/grains.rb)
271+
51. [Resistor Color Trio](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/resistor_color_trio.rb)
272+
52. [Saddle Points](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/saddle_points.rb)
273+
53. [ETL](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/etl.rb)
274+
54. [Nucleotide Count](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/nucleotide_count.rb)
275+
55. [Pythogorean Triplet](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/pythogorean_triplet.rb)
271276

272277
<a name="leetcode"/>
273278

exercism/etl.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/etl
2+
3+
#Solution
4+
class ETL
5+
def self.transform(old_mapping)
6+
new_mapping = {}
7+
old_mapping.each do |point,letters|
8+
letters.each do |letter|
9+
new_mapping[letter.downcase]=point
10+
end
11+
end
12+
new_mapping
13+
end
14+
end

exercism/nucleotide_count.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/nucleotide-count
2+
3+
#Solution
4+
class Nucleotide
5+
def initialize(dna_strand)
6+
nucleotides = dna_strand.split("")
7+
@nucleotide_count = { 'A' => 0, 'T' => 0, 'C' => 0, 'G' => 0 }
8+
nucleotides.each do |nucleotide|
9+
raise ArgumentError unless @nucleotide_count.has_key?(nucleotide)
10+
@nucleotide_count[nucleotide]+=1
11+
end
12+
end
13+
14+
def self.from_dna(dna_strand)
15+
Nucleotide.new(dna_strand)
16+
end
17+
18+
def count(nucleotide)
19+
@nucleotide_count[nucleotide]
20+
end
21+
22+
def histogram
23+
@nucleotide_count
24+
end
25+
end

exercism/pythogorean_triplet.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/pythagorean-triplet
2+
3+
#Solution
4+
class PythagoreanTriplet
5+
def self.triplets_with_sum(n)
6+
triplets = []
7+
for b in 4..(n-1)
8+
deno = 2*(n-b)
9+
n_sq = n**2
10+
a = (n_sq-(2*n*b))/deno
11+
c = (n_sq/deno)-b
12+
triplets.push([a,b,c]) if a>0 and a<b and c>b and a+b+c==n
13+
end
14+
triplets = triplets.sort_by{|val| val[0]}
15+
end
16+
end
17+
18+
#Solution 2
19+
class PythagoreanTriplet
20+
def self.triplets_with_sum(sum)
21+
(1..sum / 3).each_with_object([]) do |side_a, triplets|
22+
side_b = (sum * (sum - 2 * side_a)) / (2 * (sum - side_a))
23+
side_c = sum - side_a - side_b
24+
next if side_b < side_a || side_c < side_b
25+
triplets << [side_a, side_b, side_c] if (side_a**2 + side_b**2 == side_c**2)
26+
end
27+
end
28+
end

exercism/resistor_color_trio.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Problem : https://exercism.org/tracks/ruby/exercises/resistor-color-trio
2+
3+
#Solution
4+
class ResistorColorTrio
5+
COLOR_CODES = {
6+
"black" => 0,
7+
"brown" => 1,
8+
"red" => 2,
9+
"orange" => 3,
10+
"yellow" => 4,
11+
"green" => 5,
12+
"blue" => 6,
13+
"violet" => 7,
14+
"grey" => 8,
15+
"white" => 9
16+
}
17+
def initialize(colors)
18+
@color_bands = colors
19+
end
20+
21+
def label
22+
resistor_val = (COLOR_CODES[@color_bands[0]]*10 + COLOR_CODES[@color_bands[1]])*(10**COLOR_CODES[@color_bands[2]])
23+
unit = "ohms"
24+
if resistor_val>1000
25+
resistor_val/=1000
26+
unit = "kiloohms"
27+
end
28+
"Resistor value: #{resistor_val} #{unit}"
29+
end
30+
end

exercism/saddle_points.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/saddle-points
2+
3+
#Solution
4+
class Grid
5+
def self.saddle_points(input)
6+
row_min_map = self.get_row_min_map(input)
7+
col_min_map = self.get_col_min_map(input)
8+
saddle_points = []
9+
input.each_with_index do |row,row_index|
10+
row.each_with_index do |element,col_index|
11+
if element>=row_min_map[row_index+1] && element<=col_min_map[col_index+1]
12+
saddle_points.append({"row"=>row_index+1,"column"=>col_index+1})
13+
end
14+
end
15+
end
16+
return saddle_points
17+
end
18+
19+
def self.get_row_min_map(input)
20+
row_min_map={}
21+
input.each_with_index do |row,index|
22+
row_min_map[index+1] = row.max
23+
end
24+
row_min_map
25+
end
26+
27+
def self.get_col_min_map(input)
28+
col_min_map={}
29+
for i in (0...input[0].length) do
30+
min = input[0][i]
31+
for j in (0...input.length) do
32+
min = input[j][i] if min>=input[j][i]
33+
end
34+
col_min_map[i+1]=min
35+
end
36+
col_min_map
37+
end
38+
end

0 commit comments

Comments
 (0)