Skip to content

Commit 8241ba7

Browse files
committed
exercism 87,88 bank-acc and Gilded-Rose
1 parent b8dab3a commit 8241ba7

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
312312
84. [Complex Number](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/complex_number.rb)
313313
85. [Microwave](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/microwave.rb)
314314
86. [Darts](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/darts.rb)
315+
87. [Bank Account](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/bank_account.rb)
316+
88. [Gilded Rose](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/gilded_rose.rb)
315317

316318
<a name="leetcode"/>
317319

exercism/bank_account.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/bank-account
2+
3+
# Solution
4+
class BankAccount
5+
def initialize
6+
@opened = false
7+
@balance = 0
8+
end
9+
10+
def open
11+
raise ArgumentError.new("You can't open an already open account") if @opened
12+
@opened = true
13+
end
14+
15+
def balance
16+
raise ArgumentError.new("You can't check the balance of a closed account") unless @opened
17+
@balance
18+
end
19+
20+
def deposit(amount)
21+
raise ArgumentError.new("You can't deposit a negative amount") if amount<0
22+
raise ArgumentError.new("You can't deposit money into a closed account") unless @opened
23+
@balance+= amount
24+
end
25+
26+
def withdraw(amount)
27+
raise ArgumentError.new("You can't withdraw money into a closed account") unless @opened
28+
raise ArgumentError.new("You can't withdraw a negative amount") if amount<=0
29+
raise ArgumentError.new("You can't withdraw more than you have") if amount>@balance
30+
@balance-=amount
31+
end
32+
33+
def close
34+
raise ArgumentError.new("You can't close an already closed account") unless @opened
35+
@opened = false
36+
@balance = 0
37+
end
38+
end

exercism/gilded_rose.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/gilded-rose
2+
3+
# Solution
4+
Item = Struct.new(:name, :sell_in, :quality) #instead of struct a class Item can be created
5+
6+
class GildedRose
7+
REVERSE_AGING_ITEMS = ["Aged Brie", "Sulfuras, Hand of Ragnaros", "Backstage passes to a TAFKAL80ETC concert"]
8+
9+
def initialize(items)
10+
@items = items
11+
end
12+
13+
def update!
14+
@items.each do |item|
15+
update_quality(item)
16+
update_sell_in(item)
17+
end
18+
end
19+
20+
def update_quality(item)
21+
is_conjured = item.name.downcase.start_with?("conjured")
22+
if is_conjured
23+
return update_conjured_item_quality(item)
24+
end
25+
26+
if REVERSE_AGING_ITEMS.include?(item.name)
27+
return update_reverse_aging(item)
28+
end
29+
30+
update_normal_item(item,false)
31+
end
32+
33+
def update_sell_in(item)
34+
item.sell_in-=1 unless item.name == "Sulfuras, Hand of Ragnaros"
35+
end
36+
37+
def update_normal_item(item,is_conjured=false)
38+
sell_in_not_arrived = (item.sell_in>0)
39+
if sell_in_not_arrived
40+
item.quality-=1 if is_conjured
41+
item.quality = item.quality>=1 ? item.quality-1 : 0
42+
return item.quality
43+
end
44+
item.quality = item.quality>=2 ? item.quality-2 : 0
45+
end
46+
47+
def update_reverse_aging(item)
48+
return if item.quality==50 or item.name.include?("Sulfuras, Hand of Ragnaros")
49+
return update_backstage_passes(item) if item.name.include?("Backstage passes to a TAFKAL80ETC concert")
50+
item.quality = item.sell_in>0 ? item.quality+1 : item.quality+2
51+
item.quality = item.quality > 50 ? 50 : item.quality
52+
end
53+
54+
def update_backstage_passes(item,is_conjured=false)
55+
return item.quality=0 if item.sell_in <=0
56+
if item.sell_in<=5
57+
item.quality+=3
58+
elsif item.sell_in<=10
59+
item.quality+=2
60+
else
61+
item.quality+=1
62+
end
63+
item.quality-=1 if is_conjured
64+
item.quality = item.quality > 50 ? 50 : item.quality
65+
end
66+
67+
def update_conjured_item_quality(item)
68+
return item.quality=0 if item.sell_in <=0
69+
if item.name.include?("backstage passes to a TAFKAL80ETC concert")
70+
return update_backstage_passes(item,true)
71+
end
72+
return update_reverse_aging(item) if item.name.include?("Sulfuras") or item.name.include?("Aged Brie")
73+
return update_normal_item(item,true)
74+
end
75+
end

0 commit comments

Comments
 (0)