Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion input/input.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion output/output.txt
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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}")