ytyt003 commited on
Commit
ca288e0
·
verified ·
1 Parent(s): c64b230

Update graders.py

Browse files
Files changed (1) hide show
  1. graders.py +64 -27
graders.py CHANGED
@@ -6,23 +6,30 @@ def grade_easy_task(db_path: str) -> float:
6
  try:
7
  with sqlite3.connect(db_path) as conn:
8
  c = conn.cursor()
9
- c.execute("SELECT name, signup_date FROM customers ORDER BY id")
10
- rows = c.fetchall()
11
 
12
- if rows:
13
- correct_rows = 0
14
- expected = [
15
- ('Alice', '2022-12-31'),
16
- ('Bob', '2023-01-15'),
17
- ('Charlie', '2023-05-14'),
18
- ('David', '2023-11-01')
19
- ]
 
 
 
 
 
 
 
 
 
 
20
 
21
- for actual, exp in zip(rows, expected):
22
- if actual[0] == exp[0] and actual[1] == exp[1]:
23
- correct_rows += 1
24
-
25
- actual_score = float(correct_rows) / len(expected)
26
  except sqlite3.Error:
27
  actual_score = 0.0
28
 
@@ -39,21 +46,32 @@ def grade_medium_task(db_path: str) -> float:
39
  with sqlite3.connect(db_path) as conn:
40
  c = conn.cursor()
41
 
42
- # Check if target tables exist (0.4 points)
43
  c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('customers', 'orders')")
44
  tables = [row[0] for row in c.fetchall()]
45
  if 'customers' in tables and 'orders' in tables:
46
  score += 0.4
47
 
48
- # Check if customers are deduplicated correctly (0.3 points)
49
- c.execute("SELECT COUNT(*) FROM customers")
50
- if c.fetchone()[0] == 2: # Alice and Bob
 
51
  score += 0.3
52
 
53
- # Check if orders map correctly to customers (0.3 points)
54
- c.execute("SELECT COUNT(*) FROM orders")
55
- if c.fetchone()[0] == 3:
56
- score += 0.3
 
 
 
 
 
 
 
 
 
 
57
  except sqlite3.Error:
58
  pass
59
 
@@ -69,13 +87,32 @@ def grade_hard_task(db_path: str) -> float:
69
  try:
70
  with sqlite3.connect(db_path) as conn:
71
  c = conn.cursor()
72
- # Check if the view exists and has the correct logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  c.execute("SELECT account_id, net_balance FROM account_balances ORDER BY account_id")
74
- rows = c.fetchall()
75
 
76
- expected = [(101, 250.0), (102, 1000.0)]
77
- if rows == expected:
78
  actual_score = 1.0
 
 
 
 
 
79
  except sqlite3.Error:
80
  actual_score = 0.0
81
 
 
6
  try:
7
  with sqlite3.connect(db_path) as conn:
8
  c = conn.cursor()
 
 
9
 
10
+ # Get total number of rows to calculate percentages
11
+ c.execute("SELECT COUNT(*) FROM customers")
12
+ total_rows = c.fetchone()[0]
13
+
14
+ if total_rows > 0:
15
+ # Count how many names are perfectly trimmed
16
+ c.execute("SELECT COUNT(*) FROM customers WHERE name = TRIM(name)")
17
+ trimmed_names = c.fetchone()[0]
18
+
19
+ # Count how many dates follow the strict YYYY-MM-DD format
20
+ # (length 10, hyphen at pos 5 and 8)
21
+ c.execute("""
22
+ SELECT COUNT(*) FROM customers
23
+ WHERE length(signup_date) = 10
24
+ AND substr(signup_date, 5, 1) = '-'
25
+ AND substr(signup_date, 8, 1) = '-'
26
+ """)
27
+ formatted_dates = c.fetchone()[0]
28
 
29
+ # Calculate partial credit: 50% for names, 50% for dates
30
+ name_score = trimmed_names / total_rows
31
+ date_score = formatted_dates / total_rows
32
+ actual_score = (name_score + date_score) / 2.0
 
33
  except sqlite3.Error:
34
  actual_score = 0.0
35
 
 
46
  with sqlite3.connect(db_path) as conn:
47
  c = conn.cursor()
48
 
49
+ # 1. Check if target tables exist (0.4 points)
50
  c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('customers', 'orders')")
51
  tables = [row[0] for row in c.fetchall()]
52
  if 'customers' in tables and 'orders' in tables:
53
  score += 0.4
54
 
55
+ # 2. Check for deduplication: No duplicate names in customers (0.3 points)
56
+ c.execute("SELECT COUNT(*) FROM (SELECT name FROM customers GROUP BY name HAVING COUNT(*) > 1)")
57
+ duplicate_groups = c.fetchone()[0]
58
+ if duplicate_groups == 0:
59
  score += 0.3
60
 
61
+ # 3. Check Referential Integrity: No orphaned orders (0.3 points)
62
+ # Assumes 'orders' has a 'customer_id' column mapping to customers(id)
63
+ # If your orders table uses 'customer_name', change 'customer_id' to 'customer_name' and 'id' to 'name'
64
+ try:
65
+ c.execute("""
66
+ SELECT COUNT(*) FROM orders
67
+ WHERE customer_id NOT IN (SELECT id FROM customers)
68
+ """)
69
+ orphaned_orders = c.fetchone()[0]
70
+ if orphaned_orders == 0:
71
+ score += 0.3
72
+ except sqlite3.Error:
73
+ # Column might not exist or agent failed to create it properly
74
+ pass
75
  except sqlite3.Error:
76
  pass
77
 
 
87
  try:
88
  with sqlite3.connect(db_path) as conn:
89
  c = conn.cursor()
90
+
91
+ # THE GOLDEN QUERY: We dynamically calculate the true answer
92
+ # Assumes the raw data is in a table called 'transactions'
93
+ golden_query = """
94
+ SELECT
95
+ account_id,
96
+ SUM(CASE WHEN type = 'credit' THEN amount ELSE -amount END) as true_balance
97
+ FROM transactions
98
+ GROUP BY account_id
99
+ ORDER BY account_id
100
+ """
101
+ c.execute(golden_query)
102
+ golden_rows = c.fetchall()
103
+
104
+ # Fetch the agent's view
105
  c.execute("SELECT account_id, net_balance FROM account_balances ORDER BY account_id")
106
+ agent_rows = c.fetchall()
107
 
108
+ # Compare the dynamic result to the agent's view
109
+ if len(golden_rows) > 0 and agent_rows == golden_rows:
110
  actual_score = 1.0
111
+ elif len(agent_rows) > 0:
112
+ # Partial credit calculation: how many rows matched exactly?
113
+ matches = len(set(agent_rows) & set(golden_rows))
114
+ actual_score = matches / len(golden_rows)
115
+
116
  except sqlite3.Error:
117
  actual_score = 0.0
118