Extract constants and return early for Sulfuras

No behaviour change. Named the item strings and the 0/50 bounds, moved
the body into _update_item and return early for Sulfuras so the rest of
the ladder only deals with items that actually change.
This commit is contained in:
uzairarslan committed 2026-09-24 03:21:18 +05:00
1 parent 3809be0b03
commit 19011580f5
1 file changed
+28 -14
+28 -14
View File
@@ -1,5 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
AGED_BRIE = "Aged Brie"
BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert"
SULFURAS = "Sulfuras, Hand of Ragnaros"
MIN_QUALITY = 0
MAX_QUALITY = 50
class GildedRose(object): class GildedRose(object):
def __init__(self, items): def __init__(self, items):
@@ -7,32 +15,38 @@ class GildedRose(object):
def update_quality(self): def update_quality(self):
for item in self.items: for item in self.items:
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": self._update_item(item)
if item.quality > 0:
if item.name != "Sulfuras, Hand of Ragnaros": @staticmethod
def _update_item(item):
if item.name == SULFURAS:
return # legendary: never has to be sold and never changes
if item.name != AGED_BRIE and item.name != BACKSTAGE_PASS:
if item.quality > MIN_QUALITY:
item.quality = item.quality - 1 item.quality = item.quality - 1
else: else:
if item.quality < 50: if item.quality < MAX_QUALITY:
item.quality = item.quality + 1 item.quality = item.quality + 1
if item.name == "Backstage passes to a TAFKAL80ETC concert": if item.name == BACKSTAGE_PASS:
if item.sell_in < 11: if item.sell_in < 11:
if item.quality < 50: if item.quality < MAX_QUALITY:
item.quality = item.quality + 1 item.quality = item.quality + 1
if item.sell_in < 6: if item.sell_in < 6:
if item.quality < 50: if item.quality < MAX_QUALITY:
item.quality = item.quality + 1 item.quality = item.quality + 1
if item.name != "Sulfuras, Hand of Ragnaros":
item.sell_in = item.sell_in - 1 item.sell_in = item.sell_in - 1
if item.sell_in < 0: if item.sell_in < 0:
if item.name != "Aged Brie": if item.name != AGED_BRIE:
if item.name != "Backstage passes to a TAFKAL80ETC concert": if item.name != BACKSTAGE_PASS:
if item.quality > 0: if item.quality > MIN_QUALITY:
if item.name != "Sulfuras, Hand of Ragnaros":
item.quality = item.quality - 1 item.quality = item.quality - 1
else: else:
item.quality = item.quality - item.quality item.quality = MIN_QUALITY
else: else:
if item.quality < 50: if item.quality < MAX_QUALITY:
item.quality = item.quality + 1 item.quality = item.quality + 1