From 671461d44098fe657032de2acc2a27e61035a690 Mon Sep 17 00:00:00 2001 From: zoengsw Date: Mon, 10 Nov 2025 15:24:15 +0000 Subject: [PATCH 1/4] refactor: add switch cases --- python/tests/gilded_rose.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/python/tests/gilded_rose.py b/python/tests/gilded_rose.py index 4f21ea64..9cd20bb5 100644 --- a/python/tests/gilded_rose.py +++ b/python/tests/gilded_rose.py @@ -1,12 +1,25 @@ # -*- coding: utf-8 -*- class GildedRose(object): + BACKSTAGE = 'Backstage passes to a TAFKAL80ETC concert' + SULFURAS = 'Sulfuras, Hand of Ragnaros' + AGED_BRIE = 'Aged Brie' def __init__(self, items): self.items = items def update_quality(self): for item in self.items: + + match item.name: + case self.AGED_BRIE: + pass + case self.SULFURAS: + pass + case self.BACKSTAGE: + pass + case _: + pass if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": if item.quality > 0: if item.name != "Sulfuras, Hand of Ragnaros": From d700742aa67a2715e8cb81772b8ea22b5af415cf Mon Sep 17 00:00:00 2001 From: zoengsw Date: Mon, 10 Nov 2025 16:39:25 +0000 Subject: [PATCH 2/4] refactor: helper functions in incrementing and decrementing --- python/tests/gilded_rose.py | 64 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/python/tests/gilded_rose.py b/python/tests/gilded_rose.py index 9cd20bb5..9629cfee 100644 --- a/python/tests/gilded_rose.py +++ b/python/tests/gilded_rose.py @@ -4,13 +4,19 @@ class GildedRose(object): BACKSTAGE = 'Backstage passes to a TAFKAL80ETC concert' SULFURAS = 'Sulfuras, Hand of Ragnaros' AGED_BRIE = 'Aged Brie' + MAX_QUAL = 50 def __init__(self, items): self.items = items + def __qual_inc(self, item, inc): + item.quality = max(50, item.quality + inc) + + def __qual_dec(self, item, dec): + item.quality = min(0, item.quality - dec) + def update_quality(self): for item in self.items: - match item.name: case self.AGED_BRIE: pass @@ -19,34 +25,36 @@ class GildedRose(object): case self.BACKSTAGE: pass case _: - pass - if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": - if item.quality > 0: - if item.name != "Sulfuras, Hand of Ragnaros": - item.quality = item.quality - 1 - else: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name == "Backstage passes to a TAFKAL80ETC concert": - if item.sell_in < 11: - if item.quality < 50: - item.quality = item.quality + 1 - if item.sell_in < 6: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name != "Sulfuras, Hand of Ragnaros": + self.__qual_dec(item, 1) + if not item.name == self.SULFURAS: item.sell_in = item.sell_in - 1 - if item.sell_in < 0: - if item.name != "Aged Brie": - if item.name != "Backstage passes to a TAFKAL80ETC concert": - if item.quality > 0: - if item.name != "Sulfuras, Hand of Ragnaros": - item.quality = item.quality - 1 - else: - item.quality = item.quality - item.quality - else: - if item.quality < 50: - item.quality = item.quality + 1 + # if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": + # if item.quality > 0: + # if item.name != "Sulfuras, Hand of Ragnaros": + # item.quality = item.quality - 1 + # else: + # if item.quality < 50: + # item.quality = item.quality + 1 + # if item.name == "Backstage passes to a TAFKAL80ETC concert": + # if item.sell_in < 11: + # if item.quality < 50: + # item.quality = item.quality + 1 + # if item.sell_in < 6: + # if item.quality < 50: + # item.quality = item.quality + 1 + # if item.name != "Sulfuras, Hand of Ragnaros": + # item.sell_in = item.sell_in - 1 + # if item.sell_in < 0: + # if item.name != "Aged Brie": + # if item.name != "Backstage passes to a TAFKAL80ETC concert": + # if item.quality > 0: + # if item.name != "Sulfuras, Hand of Ragnaros": + # item.quality = item.quality - 1 + # else: + # item.quality = item.quality - item.quality + # else: + # if item.quality < 50: + # item.quality = item.quality + 1 class Item: From 37352f6fd479e946b5f8f0941338d8e6207d1c4d Mon Sep 17 00:00:00 2001 From: zoengsw Date: Mon, 10 Nov 2025 17:05:07 +0000 Subject: [PATCH 3/4] refactor: implement all cases --- python/tests/gilded_rose.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/python/tests/gilded_rose.py b/python/tests/gilded_rose.py index 9629cfee..7e076dd0 100644 --- a/python/tests/gilded_rose.py +++ b/python/tests/gilded_rose.py @@ -10,24 +10,37 @@ class GildedRose(object): self.items = items def __qual_inc(self, item, inc): - item.quality = max(50, item.quality + inc) + item.quality = min(self.MAX_QUAL, item.quality + inc) def __qual_dec(self, item, dec): - item.quality = min(0, item.quality - dec) + item.quality = max(0, item.quality - dec) def update_quality(self): for item in self.items: match item.name: case self.AGED_BRIE: - pass + if item.sell_in > 0: + self.__qual_inc(item, 1) + else: + self.__qual_inc(item, 2) case self.SULFURAS: pass case self.BACKSTAGE: - pass + if item.sell_in > 10: + self.__qual_inc(item, 1) + elif item.sell_in > 5: + self.__qual_inc(item, 2) + elif item.sell_in > 0: + self.__qual_inc(item, 3) + else: + item.quality = 0 case _: - self.__qual_dec(item, 1) + if item.sell_in > 0: + self.__qual_dec(item, 1) + else: + self.__qual_dec(item, 2) if not item.name == self.SULFURAS: - item.sell_in = item.sell_in - 1 + item.sell_in -= 1 # if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": # if item.quality > 0: # if item.name != "Sulfuras, Hand of Ragnaros": From 8dec8acacb2bb79b88a6cd3461fc71f2bc741a1b Mon Sep 17 00:00:00 2001 From: zoengsw Date: Mon, 10 Nov 2025 17:06:18 +0000 Subject: [PATCH 4/4] refactor: remove commented code --- python/tests/gilded_rose.py | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/python/tests/gilded_rose.py b/python/tests/gilded_rose.py index 7e076dd0..40fa0039 100644 --- a/python/tests/gilded_rose.py +++ b/python/tests/gilded_rose.py @@ -41,33 +41,6 @@ class GildedRose(object): self.__qual_dec(item, 2) if not item.name == self.SULFURAS: item.sell_in -= 1 - # if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": - # if item.quality > 0: - # if item.name != "Sulfuras, Hand of Ragnaros": - # item.quality = item.quality - 1 - # else: - # if item.quality < 50: - # item.quality = item.quality + 1 - # if item.name == "Backstage passes to a TAFKAL80ETC concert": - # if item.sell_in < 11: - # if item.quality < 50: - # item.quality = item.quality + 1 - # if item.sell_in < 6: - # if item.quality < 50: - # item.quality = item.quality + 1 - # if item.name != "Sulfuras, Hand of Ragnaros": - # item.sell_in = item.sell_in - 1 - # if item.sell_in < 0: - # if item.name != "Aged Brie": - # if item.name != "Backstage passes to a TAFKAL80ETC concert": - # if item.quality > 0: - # if item.name != "Sulfuras, Hand of Ragnaros": - # item.quality = item.quality - 1 - # else: - # item.quality = item.quality - item.quality - # else: - # if item.quality < 50: - # item.quality = item.quality + 1 class Item: