Skip to content

Instantly share code, notes, and snippets.

@Neppord
Last active December 11, 2015 06:29
Show Gist options
  • Save Neppord/4559575 to your computer and use it in GitHub Desktop.
Save Neppord/4559575 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
>>> update_item(Item("a", 1, 2))
a, 0, 1
>>> update_item(Item("a", 1, 1))
a, 0, 0
>>> update_item(Item("a", 1, 0))
a, 0, 0
>>> update_item(Item("a", 0, 3))
a, -1, 1
>>> update_item(Item(AGED_BRIE, 1, 1))
Aged Brie, 0, 2
>>> update_item(Item(AGED_BRIE, 0, 1))
Aged Brie, -1, 3
>>> update_item(Item(AGED_BRIE, 0, 50))
Aged Brie, -1, 50
>>> update_item(Item(SULFURAS, 0, 80))
Sulfuras, Hand of Ragnaros, 0, 80
>>> update_item(Item(SULFURAS, 0, 50))
Sulfuras, Hand of Ragnaros, 0, 50
>>> update_item(Item(PASS, 11, 4))
Backstage passes to a TAFKAL80ETC concert, 10, 5
>>> update_item(Item(PASS, 10, 4))
Backstage passes to a TAFKAL80ETC concert, 9, 6
>>> update_item(Item(PASS, 6, 4))
Backstage passes to a TAFKAL80ETC concert, 5, 6
>>> update_item(Item(PASS, 5, 4))
Backstage passes to a TAFKAL80ETC concert, 4, 7
>>> update_item(Item(PASS, 0, 4))
Backstage passes to a TAFKAL80ETC concert, -1, 0
>>> update_item(Item(CONJURED, 3, 5))
Conjured, 2, 3
>>> update_item(Item(CONJURED, 0, 5))
Conjured, -1, 1
>>> update_item(Item(CONJURED, 0, 2))
Conjured, -1, 0
>>> update_item(Item(CONJURED, 1, 1))
Conjured, 0, 0
"""
PASS = "Backstage passes to a TAFKAL80ETC concert"
SULFURAS = "Sulfuras, Hand of Ragnaros"
AGED_BRIE = "Aged Brie"
CONJURED = "Conjured"
def update_item(item):
if item.name == CONJURED:
if item.sell_in <= 0:
item.quality -= 4
else:
item.quality -= 2
item.quality = max(item.quality, 0)
item.sell_in -= 1
return item
if item.name == SULFURAS:
return item
if item.name == AGED_BRIE:
if item.sell_in <= 0:
item.quality += 2
else:
item.quality += 1
item.quality = min(item.quality, 50)
item.sell_in -= 1
return item
if item.name == PASS:
if item.sell_in <= 0:
item.quality = 0
elif item.sell_in <= 5:
item.quality += 3
elif item.sell_in <= 10:
item.quality += 2
else:
item.quality += 1
item.quality = min(item.quality, 50)
item.sell_in -= 1
return item
if item.quality > 0:
item.quality = item.quality - 1
if item.sell_in <= 0:
if item.quality > 0:
item.quality = item.quality - 1
item.sell_in = item.sell_in - 1
return item
def update_quality(items):
for item in items:
update_item(item)
return items
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)
"""
This module, on request, includes a namedtuple that has attr access
as well as index access to the values by name
"""
from collections import namedtuple
Item = namedtuple('Item', ['name', 'sell_in', 'quality'])
Item.__getitem__ = lambda self, index: getattr(self, index) if isinstance(index, str) else super(Item, self).__getitem__(index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment