Certainly! Below is a Python program that parses the provided employee (emp) and department (dept) data. After parsing, it performs the specified query:
/* 7. List clerks who do not have a salesman in their department as a colleague. */
This program will output the clerks who do not have any salesmen working in their respective departments.
import re
# Multi-line string containing the data
data = """
% emp(Employee number, Name, Job, Superior, Employed from, Salary, Department number)
emp(11, king, president, null, 1990, 5000, 10).
emp(12, blake, manager, 11, 1992, 2850, 30).
emp(13, clark, manager, 11, 1993, 1500, 10).
emp(14, jones, manager, 11, 1995, 2975, 20).
emp(15, martin, salesman, 12, 1998, 2650, 30).
emp(16, allen, salesman, 12, 1998, 1900, 30).
emp(17, turner, salesman, 12, 1995, 1500, 30).
emp(18, james, clerk, 15, 2000, 950, 30).
emp(19, ward, salesman, 12, 1996, 1750, 30).
emp(20, ford, analyst, 13, 1997, 3000, 20).
emp(21, smith, clerk, 12, 1993, 800, 20).
emp(22, scott, analyst, 13, 1996, 3000, 20).
emp(23, adams, clerk, 11, 1999, 1100, 20).
emp(24, miller, clerk, 14, 2000, 1300, 10).
% dept(Department number, Name, Location)
dept(10, accounting, newyork).
dept(20, research, dallas).
dept(30, sales, chicago).
dept(40, operations, boston). and /* 7. List clerks who do not have a salesman in their department as a colleague. */
"""
# Regular expressions to parse emp and dept entries
emp_pattern = re.compile(
r'emp\(\s*(\d+),\s*' # Employee number
r'(\w+),\s*' # Name
r'(\w+),\s*' # Job
r'(null|\d+),\s*' # Superior
r'(\d{4}),\s*' # Employed from
r'(\d+),\s*' # Salary
r'(\d+)\s*\)\.' # Department number
)
dept_pattern = re.compile(
r'dept\(\s*(\d+),\s*' # Department number
r'(\w+),\s*' # Department name
r'(\w+)\s*\)\.' # Location
)
# Parse emp entries
employees = []
for match in emp_pattern.finditer(data):
emp = {
'Employee number': int(match.group(1)),
'Name': match.group(2),
'Job': match.group(3),
'Superior': int(match.group(4)) if match.group(4) != 'null' else None,
'Employed from': int(match.group(5)),
'Salary': int(match.group(6)),
'Department number': int(match.group(7))
}
employees.append(emp)
# Parse dept entries
departments = {}
for match in dept_pattern.finditer(data):
dept_num = int(match.group(1))
departments[dept_num] = {
'Name': match.group(2),
'Location': match.group(3)
}
# Display parsed data (optional)
# print("Employees:")
# for emp in employees:
# print(emp)
# print("\nDepartments:")
# for dept_num, dept_info in departments.items():
# print(f"{dept_num}: {dept_info}")
# Query 7: List clerks who do not have a salesman in their department as a colleague.
# Step 1: Create a mapping from Department number to list of jobs in that department
dept_to_jobs = {}
for emp in employees:
dept = emp['Department number']
job = emp['Job']
if dept not in dept_to_jobs:
dept_to_jobs[dept] = set()
dept_to_jobs[dept].add(job)
# Step 2: Identify clerks without salesmen in their department
clerks_without_salesmen = []
for emp in employees:
if emp['Job'].lower() == 'clerk':
dept = emp['Department number']
jobs_in_dept = dept_to_jobs.get(dept, set())
if 'salesman' not in jobs_in_dept:
clerks_without_salesmen.append(emp)
# Step 3: Display the result
print("Clerks who do not have a salesman in their department as a colleague:")
print("-----------------------------------------------------------------------")
for clerk in clerks_without_salesmen:
dept_info = departments.get(clerk['Department number'], {})
print(f"Employee Number: {clerk['Employee number']}, "
f"Name: {clerk['Name']}, "
f"Department: {clerk['Department number']} ({dept_info.get('Name', 'Unknown')})")
Explanation of the Program
-
Data Definition:
- The provided data is stored as a multi-line string named
data. This includes both employee (emp) and department (dept) records.
-
Parsing Employee (emp) Entries:
- A regular expression
emp_pattern is defined to extract each field from the emp entries.
- The program iterates over all matches of
emp_pattern in the data and constructs a list of dictionaries named employees, where each dictionary represents an employee with its corresponding attributes.
-
Parsing Department (dept) Entries:
- Similarly, a regular expression
dept_pattern is defined to extract fields from the dept entries.
- A dictionary named
departments is created where each key is the department number, and the value is another dictionary containing the department's name and location.
-
Performing the Query:
- Mapping Departments to Jobs: The program first creates a mapping (
dept_to_jobs) from each department number to a set of job titles present in that department.
- Identifying Target Clerks: It then iterates through all employees to find those with the job title
'clerk'. For each clerk, it checks if 'salesman' is not present in their department's job set (dept_to_jobs). If this condition is true, the clerk is added to the clerks_without_salesmen list.
-
Output:
- Finally, the program prints out the clerks who do not have any salesmen in their departments, displaying their employee number, name, and department information.
Sample Output
Clerks who do not have a salesman in their department as a colleague:
-----------------------------------------------------------------------
Employee Number: 21, Name: smith, Department: 20 (research)
Employee Number: 23, Name: adams, Department: 20 (research)
Employee Number: 24, Name: miller, Department: 10 (accounting)
Notes
-
Case Sensitivity: The program assumes that job titles are consistently lowercase (e.g., 'salesman'). If the data might contain mixed cases (e.g., 'Salesman', 'SalesMan'), consider normalizing the case using .lower() when comparing job titles.
-
Data Validation: This program assumes that the input data is well-formed. For more robust applications, you might want to add error handling to manage unexpected or malformed data entries.
-
Extensibility: The parsing logic can be extended or modified to handle additional fields or different data formats as needed.
Feel free to integrate this script into your application or modify it further to suit your specific requirements!