From 6a44228e1b12670d14182d1c3be3df68b105ddf2 Mon Sep 17 00:00:00 2001 From: krishnasanaka1 <167261942+krishnasanaka1@users.noreply.github.com> Date: Sun, 21 Apr 2024 10:07:04 -0500 Subject: [PATCH 1/4] Create main.py --- src/main.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..e3b22d7 --- /dev/null +++ b/src/main.py @@ -0,0 +1,36 @@ +from collections import defaultdict +from datetime import datetime + +class CustomerData: + def __init__(self): + self.events = defaultdict(list) + + 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))) + + 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 + + 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] + +# Example usage +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}") From 164c06cc05b357d549201a0931569dc8b669df80 Mon Sep 17 00:00:00 2001 From: krishnasanaka1 <167261942+krishnasanaka1@users.noreply.github.com> Date: Sun, 21 Apr 2024 10:12:20 -0500 Subject: [PATCH 2/4] Update output.txt --- output/output.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 6be426e67dd237e726f4288292ac383f91822c86 Mon Sep 17 00:00:00 2001 From: krishnasanaka1 <167261942+krishnasanaka1@users.noreply.github.com> Date: Sun, 21 Apr 2024 10:13:15 -0500 Subject: [PATCH 3/4] Update input.txt --- input/input.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From 8a795e100af95e6cee4abe0ac08edfc171427700 Mon Sep 17 00:00:00 2001 From: krishnasanaka1 <167261942+krishnasanaka1@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:23:23 -0500 Subject: [PATCH 4/4] Update main.py --- src/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index e3b22d7..100c162 100644 --- a/src/main.py +++ b/src/main.py @@ -4,21 +4,24 @@ 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] -# Example usage if __name__ == "__main__": events = [ ("CUSTOMER", "customer1", datetime(2021, 1, 1), {"last_name": "ch", "adr_city": "Dallas ", "adr_state": "TX"}),