Sponsored
Sponsored
This approach involves using SQL JOIN operations to merge relevant data from the Users and Orders tables. We focus on selecting only those orders where order_date
falls in the year 2019, and then count the occurrences for each user.
The time complexity is approximately O(n + m), where n is the number of users and m is the number of orders in 2019. The space complexity is O(n) for storing the result set.
1import pandas as pd
2
3users = pd.DataFrame({
4 'user_id': [1, 2, 3, 4],
5 'join_date': ['2018-01-01', '2018-02-09', '2018-01-19', '2018-05-21'],
6 'favorite_brand': ['Lenovo', 'Samsung', 'LG', 'HP']
7})
8
9orders = pd.DataFrame({
10 'order_id': [1, 2, 3, 4, 5, 6],
11 'order_date': pd.to_datetime(['2019-08-01', '2018-08-02', '2019-08-03', '2018-08-04', '2018-08-04', '2019-08-05']),
12 'item_id': [4, 2, 3, 1, 1, 2],
13 'buyer_id': [1, 1, 2, 4, 3, 2],
14 'seller_id': [2, 3, 3, 2, 4, 4]
15})
16
17# Filter orders for the year 2019
18orders_2019 = orders[orders['order_date'].dt.year == 2019]
19
20# Count orders per user
21order_counts_2019 = orders_2019.groupby('buyer_id').size().reset_index(name='orders_in_2019')
22
23# Merge with users data
24total = pd.merge(users, order_counts_2019, how='left', left_on='user_id', right_on='buyer_id').fillna(0)
25
26total['orders_in_2019'] = total['orders_in_2019'].astype(int)
27
28# Select relevant columns
29result = total[['user_id', 'join_date', 'orders_in_2019']]
30
31print(result)
We use Python's pandas library to load data into DataFrames. By filtering orders to those made in 2019 and then using groupby and size to count orders per user, we mirror the SQL logic and join the resulting counts back onto the Users DataFrame to include all users.
This approach is more general-purpose and involves looping through the datasets of Users and Orders, filtering appropriate records, and calculating the desired order count for each user manually without relying on direct SQL tools.
The time complexity is approximately O(n + m), given that we iterate through each collection separately. The process involves two main passes: one for collating the order data and another for joining this data with user data. The space complexity is O(m), where we preserve order counts in a map.
1import java.util.*;
2
3
This Java implementation traverses the list of orders, filtering by date to the year 2019 and populating a map with counts of orders per buyer. It then iterates over users, matching and appending their order count data.