diff --git a/my_delorean.py b/my_delorean.py new file mode 100644 index 00000000..07d68a8e --- /dev/null +++ b/my_delorean.py @@ -0,0 +1,29 @@ +import delorean +import parse +from decimal import Decimal +class PriceLog(object): + def __init__(self, timestamp, product_id, price): + self.timestamp = timestamp + self.product_id = product_id + self.price = price + def __repr__(self): + return ''.format(self.timestamp, + self.product_id, + self.price) + @classmethod + def parse(cls, text_log): + ''' + Parse from a text log with the format + [] - SALE - PRODUCT: - PRICE: $ + to a PriceLog object + ''' + divide_it = text_log.split(' - ') + tmp_string, _, product_string, price_string = divide_it + timestamp = delorean.parse(tmp_string.strip('[]')) + product_id = int(product_string.split(':')[-1]) + price = Decimal(price_string.split('$')[-1]) + return cls(timestamp=timestamp, product_id=product_id, price=price) + + # test code +log = '[2018-05-05T11:07:12.267897] - SALE - PRODUCT: 1345 - PRICE: $09.99' +PriceLog.parse(log) diff --git a/sortsqr.py b/sortsqr.py new file mode 100644 index 00000000..17dd4c5c --- /dev/null +++ b/sortsqr.py @@ -0,0 +1,20 @@ +'''give a sorted list, print the sorted square of the list''' +ls1=[-9,-2,3,4,5] + +def sortsql(ls1): + left=0 + right=len(ls1) -1 + result = [0] * len(ls1) + for x in range(right,-1,-1): + print(x) + if abs(ls1[left]) > abs(ls1[right]): + result[x] = ls1[left] ** 2 + print('left', left) + left=left+1 + else: + result[x] = ls1[right] **2 + print('right', right) + right=right-1 + return result +result = sortsql(ls1) +print(list(result)) diff --git a/template.py b/template.py new file mode 100644 index 00000000..d5dad727 --- /dev/null +++ b/template.py @@ -0,0 +1,15 @@ +# INPUT DATA +data = [ + (1000, 10), + (2000, 17), + (2500, 170), + (2500, -170), +] +# Print the header for reference +print('REVENUE | PROFIT | PERCENT') +# This template aligns and displays the data in the proper format +TEMPLATE = '{revenue:>7,} | {profit:>+6} | {percent:>7.2%}' +# Print the data rows +for revenue, profit in data: + row = TEMPLATE.format(revenue=revenue, profit=profit, percent=profit / revenue) + print(row)