Skip to content

Commit 5322f3c

Browse files
committed
exercism ruby track 15-20
1 parent 198870a commit 5322f3c

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

Diff for: README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
214214
<a name="exercism"/>
215215

216216
### [Exercism Ruby Track](https://exercism.org/tracks/ruby)
217-
218217
#### Problems
219218
1. [Hello World](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/hello_world.rb)
220219
2. [Lasagna](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/lasagna.rb)
@@ -227,11 +226,15 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
227226
9. [Chess Game](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/chess_game.rb)
228227
10. [BlackJack](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/blackjack.rb)
229228
11. [Bird Count](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/bird_count.rb)
230-
11. [Boutique Inventory](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/boutique_inventory.rb)
231-
11. [Boutique Inventory Improvements](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/boutique_inventory_improvements.rb)
232-
11. [Locomotive Engineer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/locomotive_engineer.rb)
233-
11. [Moviegoer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/moviegoer.rb)
234-
229+
12. [Boutique Inventory](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/boutique_inventory.rb)
230+
13. [Boutique Inventory Improvements](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/boutique_inventory_improvements.rb)
231+
14. [Locomotive Engineer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/locomotive_engineer.rb)
232+
15. [Moviegoer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/moviegoer.rb)
233+
16. [Simple Calculator](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/simple_calculator.rb)
234+
17. [Two fer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/two_fer.rb)
235+
18. [Resistor Color Duo](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/resistor_color_duo.rb)
236+
19. [Acronym](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/acronym.rb)
237+
20. [High Scores](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/high_scores.rb)
235238

236239
<a name="leetcode"/>
237240

Diff for: exercism/acronym.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/acronym
2+
3+
#Solution 1
4+
class Acronym
5+
def self.abbreviate(str)
6+
words = str.gsub("-"," ").split(" ")
7+
abbreviation=""
8+
words.each do |x|
9+
abbreviation+= x[0]
10+
end
11+
abbreviation.upcase
12+
end
13+
end
14+
15+
#Solution 2 (using regex)
16+
class Acronym
17+
def self.abbreviate(string)
18+
string.scan(/\b[a-zA-Z]/).join.upcase
19+
end
20+
end

Diff for: exercism/high_scores.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#Problem :https://exercism.org/tracks/ruby/exercises/high-scores
2+
3+
#Solution
4+
class HighScores
5+
def initialize(scores)
6+
@scores=scores
7+
end
8+
def scores
9+
@scores
10+
end
11+
def latest
12+
@scores[-1]
13+
end
14+
def personal_best
15+
@scores.max
16+
end
17+
def personal_top_three
18+
@scores.max(3)
19+
end
20+
def latest_is_personal_best?
21+
@scores.max==@scores[-1]
22+
end
23+
end

Diff for: exercism/resistor_color_dueo.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/resistor-color-duo
2+
3+
# Solution 1
4+
class ResistorColorDuo
5+
COLORS = %w[
6+
black
7+
brown
8+
red
9+
orange
10+
yellow
11+
green
12+
blue
13+
violet
14+
grey
15+
white
16+
].freeze
17+
def self.value(colors)
18+
colors.first(2).map do |color|
19+
COLORS.index(color)
20+
end.join.to_i
21+
end
22+
end
23+
24+
#Solution 2
25+
class ResistorColorDuo
26+
27+
def self.value(colours)
28+
colour_codes = {Black: 0,Brown: 1,Red: 2,Orange: 3,Yellow: 4,Green: 5,Blue:6,Violet: 7,Grey: 8,White: 9}
29+
band1 =colours[0].to_sym.capitalize
30+
band2= colours[1].to_sym.capitalize
31+
32+
colour_codes[band2]+(10*colour_codes[band1])
33+
end
34+
end
35+

Diff for: exercism/simple_calculator.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Problem : https://exercism.org/tracks/ruby/exercises/simple-calculator
2+
3+
#Solution
4+
class SimpleCalculator
5+
ALLOWED_OPERATIONS = ['+', '/', '*'].freeze
6+
UnsupportedOperation=""
7+
def self.calculate(first_operand, second_operand, operation)
8+
raise ArgumentError unless first_operand.kind_of? (Integer) and second_operand.kind_of?(Integer)
9+
case operation
10+
when "+"
11+
"#{first_operand} #{operation} #{second_operand} = #{first_operand+second_operand}"
12+
when "*"
13+
"#{first_operand} #{operation} #{second_operand} = #{first_operand*second_operand}"
14+
when "/"
15+
begin
16+
"#{first_operand} #{operation} #{second_operand} = #{first_operand/second_operand}"
17+
rescue ZeroDivisionError
18+
"Division by zero is not allowed."
19+
end
20+
else
21+
raise UnsupportedOperation
22+
end
23+
end
24+
end

Diff for: exercism/two_fer.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/two-fer
2+
3+
# Solution
4+
class TwoFer
5+
def self.two_fer(name="you")
6+
"One for #{name}, one for me."
7+
end
8+
end

0 commit comments

Comments
 (0)