diff --git a/input/input.txt b/input/input.txt index 25fa3f7..b3e8480 100644 --- a/input/input.txt +++ b/input/input.txt @@ -1 +1,3 @@ -Your input data here +CUSTOMER,NEW,2024-01-01,customer1,ch,Dallas,TX +SITE_VISIT,NEW,2023-01-05,customer1,tag1,tag2 +ORDER,NEW,2021-01-10,customer1,100 diff --git a/output/output.txt b/output/output.txt index 9e38a14..6cb933a 100644 --- a/output/output.txt +++ b/output/output.txt @@ -1 +1,2 @@ -Your output data will end up here +Top Customers with the highest Simple Lifetime Value: +Customer ID: customer1, Simple Lifetime Value: 5200.0 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..100c162 --- /dev/null +++ b/src/main.py @@ -0,0 +1,39 @@ +from collections import defaultdict +from datetime import datetime + +class CustomerData: + def __init__(self): + self.events = defaultdict(list) + + #Ingests a customer event by storing event type, customer ID, event time and additional keyword arguments# + + + def ingest_event(self, event_type, customer_id, event_time, **kwargs): + self.events[customer_id].append((event_time, event_type, kwargs.get('total_amount', 0))) + + #calculate the LTV for customer ID by sum of the the total expenditure from 'ORDER' events and dividing it by the visit frequency from 'SITE_VISIT' events# + def calculate_lifetime_value(self, customer_id): + events = self.events[customer_id] + total_expenditure = sum(amount for _, event_type, amount in events if event_type == 'ORDER') + visit_frequency = sum(1 for _, event_type, _ in events if event_type == 'SITE_VISIT') + return 52 * (total_expenditure / visit_frequency) if visit_frequency else 0 +#return top x customer with high LTV# + def top_x_simple_ltv_customers(self, x): + ltv_customers = [(self.calculate_lifetime_value(customer_id), customer_id) for customer_id in self.events] + return sorted(ltv_customers, reverse=True)[:x] + +if __name__ == "__main__": + events = [ + ("CUSTOMER", "customer1", datetime(2021, 1, 1), {"last_name": "ch", "adr_city": "Dallas ", "adr_state": "TX"}), + ("SITE_VISIT", "customer1", datetime(2023, 1, 5), {"tags": ["tag1", "tag2"]}), + ("ORDER", "customer1", datetime(2024, 1, 10), {"total_amount": 100}) + ] + + customer_data = CustomerData() + for event_type, customer_id, event_time, kwargs in events: + customer_data.ingest_event(event_type, customer_id, event_time, **kwargs) + + top_customers = customer_data.top_x_simple_ltv_customers(1) + print("Top Customers with the highest Simple Lifetime Value:") + for ltv, customer_id in top_customers: + print(f"Customer ID: {customer_id}, Simple Lifetime Value: {ltv}")