Skip to content

Instantly share code, notes, and snippets.

@ReeceHub
Created June 22, 2017 11:01
Show Gist options
  • Save ReeceHub/c85f86af91c77e67ae9da982ee362201 to your computer and use it in GitHub Desktop.
Save ReeceHub/c85f86af91c77e67ae9da982ee362201 to your computer and use it in GitHub Desktop.
First attempt at Gilded Rose kata
# -*- coding: utf-8 -*-
class GildedRose(object):
def __init__(self, items):
self.items = items
self.quality_change_per_day = {
"Aged Brie": 1,
"Backstage passes to a TAFKAL80ETC concert": {
'ten_or_less': 2,
'five_or_less': 3,
},
"Conjured Mana Cake": -2,
"Sulfuras, Hand of Ragnaros": 0,
}
self.sell_in_change_per_day = {
"Sulfuras, Hand of Ragnaros": 0,
}
def update_quality(self):
for item in self.items:
item.sell_in += self.sell_in_change_per_day.get(item.name, -1)
self.incrementally_change_quality_of_item(item)
if item.sell_in < 0:
self.incrementally_change_quality_of_item(item)
def incrementally_change_quality_of_item(self, item):
if item.name == "Backstage passes to a TAFKAL80ETC concert":
if item.sell_in < 0:
item.quality = 0
elif item.sell_in <= 5:
item.quality += self.quality_change_per_day[item.name]['five_or_less']
elif item.sell_in <= 10:
item.quality += self.quality_change_per_day[item.name]['ten_or_less']
else:
item.quality += self.quality_change_per_day.get(item.name, -1)
item.quality = max(item.quality, 0)
if item.name != "Sulfuras, Hand of Ragnaros":
item.quality = min(item.quality, 50)
class Item:
def __init__(self, name, sell_in, quality):
self.name = name
self.sell_in = sell_in
self.quality = quality
def __repr__(self):
return "%s, %s, %s" % (self.name, self.sell_in, self.quality)
======================================
Gilded Rose Requirements Specification
======================================
Hi and welcome to team Gilded Rose. As you know, we are a small inn with a prime location in a
prominent city ran by a friendly innkeeper named Allison. We also buy and sell only the finest goods.
Unfortunately, our goods are constantly degrading in quality as they approach their sell by date. We
have a system in place that updates our inventory for us. It was developed by a no-nonsense type named
Leeroy, who has moved on to new adventures. Your task is to add the new feature to our system so that
we can begin selling a new category of items. First an introduction to our system:
- All items have a SellIn value which denotes the number of days we have to sell the item
- All items have a Quality value which denotes how valuable the item is
- At the end of each day our system lowers both values for every item
Pretty simple, right? Well this is where it gets interesting:
- Once the sell by date has passed, Quality degrades twice as fast
- The Quality of an item is never negative
- "Aged Brie" actually increases in Quality the older it gets
- The Quality of an item is never more than 50
- "Sulfuras", being a legendary item, never has to be sold or decreases in Quality
- "Backstage passes", like aged brie, increases in Quality as its SellIn value approaches;
Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but
Quality drops to 0 after the concert
We have recently signed a supplier of conjured items. This requires an update to our system:
- "Conjured" items degrade in Quality twice as fast as normal items
Feel free to make any changes to the UpdateQuality method and add any new code as long as everything
still works correctly. However, do not alter the Item class or Items property as those belong to the
goblin in the corner who will insta-rage and one-shot you as he doesn't believe in shared code
ownership (you can make the UpdateQuality method and Items property static if you like, we'll cover
for you).
Just for clarification, an item can never have its Quality increase above 50, however "Sulfuras" is a
legendary item and as such its Quality is 80 and it never alters.
import unittest
from gilded_rose import Item, GildedRose
class GildedRoseTest(unittest.TestCase):
def test_foo(self):
items = [Item('foo', 0, 0)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
self.assertEqual('foo', items[0].name)
def test_in_date_foo_deterioration(self):
items = [Item('foo', 10, 10)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 9
assert items[0].sell_in == 9
def test_out_of_date_foo_deterioration(self):
items = [Item('foo', -1, 10)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 8
assert items[0].sell_in == -2
def test_quality_of_foo_never_becomes_negative(self):
items = [Item('foo', 0, 0)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 0
assert items[0].sell_in == -1
def test_aged_brie_quality_increses_over_time_while_in_date(self):
items = [Item('Aged Brie', 10, 0)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 1
assert items[0].sell_in == 9
def test_aged_brie_quality_increses_by_two_when_out_of_date(self):
items = [Item('Aged Brie', -5, 0)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 2
assert items[0].sell_in == -6
def test_aged_brie_quality_caps_at_50_when_out_of_date(self):
items = [Item('Aged Brie', -5, 50)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 50
assert items[0].sell_in == -6
def test_aged_brie_quality_caps_at_50_when_in_date(self):
items = [Item('Aged Brie', 10, 50)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 50
assert items[0].sell_in == 9
def test_in_date_conjured_mana_cake_deterioration(self):
items = [Item('Conjured Mana Cake', 10, 10)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 8
assert items[0].sell_in == 9
def test_out_of_date_conjured_mana_cake_deterioration(self):
items = [Item('Conjured Mana Cake', -1, 10)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 6
assert items[0].sell_in == -2
def test_sulfuras_never_decreases_sell_by_or_quality(self):
items = [Item('Sulfuras, Hand of Ragnaros', 100, 30)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 30
assert items[0].sell_in == 100
def test_sulfuras_can_be_over_fifty_in_quality(self):
items = [Item('Sulfuras, Hand of Ragnaros', 100, 80)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 80
assert items[0].sell_in == 100
def test_backstage_passes_while_ten_days_or_less_left(self):
items = [Item('Backstage passes to a TAFKAL80ETC concert', 10, 20)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 22
assert items[0].sell_in == 9
def test_backstage_passes_while_five_days_or_less_left(self):
items = [Item('Backstage passes to a TAFKAL80ETC concert', 5, 20)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 23
assert items[0].sell_in == 4
def test_backstage_passes_quality_after_sell_by(self):
items = [Item('Backstage passes to a TAFKAL80ETC concert', 0, 20)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
assert items[0].quality == 0
assert items[0].sell_in == -1
if __name__ == '__main__':
unittest.main()
import sys
from gilded_rose import Item, GildedRose
def main():
print('OMGHAI!')
items = [
Item(name='+5 Dexterity Vest', sell_in=10, quality=20),
Item(name='Aged Brie', sell_in=2, quality=0),
Item(name='Elixir of the Mongoose', sell_in=5, quality=7),
Item(name='Sulfuras, Hand of Ragnaros', sell_in=0, quality=80),
Item(name='Sulfuras, Hand of Ragnaros', sell_in=-1, quality=80),
Item(name='Backstage passes to a TAFKAL80ETC concert', sell_in=15, quality=20),
Item(name='Backstage passes to a TAFKAL80ETC concert', sell_in=10, quality=49),
Item(name='Backstage passes to a TAFKAL80ETC concert', sell_in=5, quality=49),
Item(name='Conjured Mana Cake', sell_in=3, quality=6), # <-- :O
]
days = 2
if len(sys.argv) > 1:
days = int(sys.argv[1]) + 1
for day in range(days):
print('-------- day {} --------'.format(day))
print('name, sellIn, quality')
for item in items:
print(item)
print('')
GildedRose(items).update_quality()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment