solve in python :
Disparate Data Sets
Time limit: 1000 ms
Memory limit: 256 MB
IEEE Xplore
Warm greetings to all IEEExtreme Participants from the Xplore API Team!
As part of the IEEExtreme Competition, you will be tasked with a programming challenge to determine the topic of an academic journal. The input content file of this exercise is static (non-changing) to allow for consistent results during this competition.
For a full dynamic database search IEEE Xplore API is available for your IEEE research needs. Xplore API provides metadata on 4.9mm academic works and is now delivering full-text content on 50k 'Open Access' articles. Xplore API will make your research needs fast and easy. The Xplore API Portal supports PHP, Python and Java as well as providing output in JSON and XML formats. Many API use cases are listed within the API Portal.
Xplore API registration is free. To learn more about IEEE Xplore API please visit https://developer.ieee.org/ and register for an API key TODAY!
Challenge
Conferences can occur as stand-alone events or as part of a serialized set. There are two disparate data storages: the first one contains the serialized sets with their parents and the second one contains stand-alone events. Due to a wrong SQL script, they were mixed into one data storage containing all the records in a random order and with (possibly) some fields removed. The complete data has been provided in a CSV format containing all the events' records. Your task is to connect the parent events records to their serialized sets, fill in the missing data and exclude some events.
Standard input
The input will contain CSV records with the parent events, serialized events and stand-alone events.
Event ID is a unique set of letters and numbers to identify each record.
Event Title is the title of the event. Double quotes within the title will be escaped with standard CSV escape of two double quotes "". Example: “This is a ""test"" title”. If the input contains escaped double quotes then the output must contain the escaped double quotes. There could be more than one event with the same title, since some of them just use their parent event title. All the titles will contain only printable ASCII characters.
Acronyms are assigned to the parent and the serialized events belonging to the set. When the acronym was assigned, there was no validation to determine if the acronym already existed. Therefore the acronym may not be unique across serialized sets.
Project Code is a string containing letters and numbers assigned to each conference. It could be empty.
3 Digit Project Code is the last three characters of the Project Code. This must be the same for all conferences in a serialized set.
Record Type is either “Parent Event” or "IEEE Event".
Assistant
The parent event has no Project Code or 3 Digit Project Code. If a parent event has children events, then its 3 Digit Project Code must updated to:
???, if the children don't have the same 3 Digit Project Code, or
The 3 Digit Project Code of its children.
There are some conditions to exclude the events from the records, these are:
Any parent event without children should be exclude from the output.
All serialized sets that don't have a unique parent event should be eliminated from the output.
Any record with no acronym is eliminated from the output.
Any child event without a parent should be eliminated from the output.
Case is important when comparing acronyms. Rat, RAT, RaT, etc. are all different acronyms.
Standard output
Print all the serialized sets sorted by acronym after applying the exclusion conditions. For each serialized set, print the parent event with its corresponding children afterwards. For each child record append the parent ID to the end of it as the samples show.
The parent must be the first record in each set followed by its children sorted lexicographically by title. If their titles are the same one, then compare their event IDs.
Constraints and notes
There will be at most
360
360 records to read.
Input Output Explanation
a094x00000EXXzC,"IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",,,"Parent Event"
a094x00002MGty5,"2023 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",23X51,X51,"IEEE Event"
a094x00001IYX7e,"2022 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",22X51,X51,"IEEE Event"
a094x00000EXXrU,"Antennas Design and Measurement International Conference","acsos",,,"Parent Event"
a094x00000Bm112,"2021 Antennas Design and Measurement International Conference","acsos",21U43,U43,"IEEE Event"
a094x00000Bm0VM,"2019 Antennas Design and Measurement International Conference","acsos",19U43,U43,"IEEE Event"
a094x00000EXXzCxxx,"IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",,,"Parent Event"
a094x00002MGty5xxx,"2023 IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",23P00,P00,"IEEE Event"
a094x00000EXXzCyyy,"IEEE Conference on Autonomic Computing","bACSOS",,,"Parent Event"
a094x00002MGty5yyy,"2023 IEEE Conference on Autonomic Computing","bACSOS",23YN0,YN0,"IEEE Event"
a094x00000EXXrUxxx,"Antennas Design and Measurement US Conference","XYZ",,,"Parent Event"
a094x00002MGu5Nxxx,"1999 Antennas Design and Measurement Africa Conference","",23U43,U43,"IEEE Event"
a094x00000EXXzCxxx,"IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",,P00,"Parent Event"
a094x00002MGty5xxx,"2023 IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",23P00,P00,"IEEE Event",a094x00000EXXzCxxx
a094x00000EXXzC,"IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",,X51,"Parent Event"
a094x00001IYX7e,"2022 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",22X51,X51,"IEEE Event",a094x00000EXXzC
a094x00002MGty5,"2023 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",23X51,X51,"IEEE Event",a094x00000EXXzC
a094x00000EXXrU,"Antennas Design and Measurement International Conference","acsos",,U43,"Parent Event"
a094x00000Bm0VM,"2019 Antennas Design and Measurement International Conference","acsos",19U43,U43,"IEEE Event",a094x00000EXXrU
a094x00000Bm112,"2021 Antennas Design and Measurement International Conference","acsos",21U43,U43,"IEEE Event",a094x00000EXXrU
a094x00000EXXzCyyy,"IEEE Conference on Autonomic Computing","bACSOS",,YN0,"Parent Event"
a094x00002MGty5yyy,"2023 IEEE Conference on Autonomic Computing","bACSOS",23YN0,YN0,"IEEE Event",a094x00000EXXzCyyy
The excluded events' IDs are:
a094x00000EXXrUxxx, since it doesn't have any children.
a094x00002MGu5Nxxx, since it doesn't have a parent event.
a094x00000EXYC2,"IEEE Nanotechnology Symposium (ANTS)","ANTS",,,"Parent Event"
a094x00000Bm0iK,"2020 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2069D,69D,"IEEE Event"
a094x00000Bm0uC,"2021 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2169D,69D,"IEEE Event"
a094x00001IYXBs,"2022 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2269D,69D,"IEEE Event"
a094x00000Bm0Hb,"2018 IEEE Nanotechnology Symposium (ANTS)","ANTS",18M86,M86,"IEEE Event"
a094x00000BlzyU,"2017 IEEE Nanotechnology Symposium (ANTS)","ANTS",17M86,M86,"IEEE Event"
a094x00000Bm01T,"2017 IEEE Australia New Zealand Student and Young Professional Congress (ANZSCON)","ANZSCON",17M74,M74,"IEEE Event"
a094x00000EXXtT,"IEEE Arctic and Northern Oceans Forum (ANOF)","ANOF",,,"Parent Event"
a094x00000Bm0U3,"2019 IEEE Arctic and Northern Oceans Forum (ANOF)","ANOF",19U27,U27,"IEEE Event"
a094x00000EXYG4,"IEEE Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",,,"Parent Event"
a094x00000Bm0CS,"2018 IEEE 1st Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",18P57,P57,"IEEE Event"
a094x00000Bm0TB,"2019 IEEE 2nd Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",19P57,P57,"IEEE Event"
a094x00000EXYG1,"IEEE Workshop on Advanced NeuroTechnologies for Brain Initiatives (ANTBI)","ANTBI",,,"Parent Event"
a094x00000EXXu2,"IEEE Brain Initiative Workshop on Advanced NeuroTechnologies for BRAIN Initiatives (ANTBI)","ANTBI",,,"Parent Event"
a094x00000Blzml,"2016 IEEE Workshop on Advanced NeuroTechnologies for Brain Initiatives (ANTBI)","ANTBI",16H29,H29,"IEEE Event"
a094x00000EXYG4,"IEEE Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",,P57,"Parent Event"
a094x00000Bm0CS,"2018 IEEE 1st Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",18P57,P57,"IEEE Event",a094x00000EXYG4
a094x00000Bm0TB,"2019 IEEE 2nd Workshop on Animation in Virtual and Augmented Environments (ANIVAE)","ANIVAE",19P57,P57,"IEEE Event",a094x00000EXYG4
a094x00000EXXtT,"IEEE Arctic and Northern Oceans Forum (ANOF)","ANOF",,U27,"Parent Event"
a094x00000Bm0U3,"2019 IEEE Arctic and Northern Oceans Forum (ANOF)","ANOF",19U27,U27,"IEEE Event",a094x00000EXXtT
a094x00000EXYC2,"IEEE Nanotechnology Symposium (ANTS)","ANTS",,???,"Parent Event"
a094x00000BlzyU,"2017 IEEE Nanotechnology Symposium (ANTS)","ANTS",17M86,M86,"IEEE Event",a094x00000EXYC2
a094x00000Bm0Hb,"2018 IEEE Nanotechnology Symposium (ANTS)","ANTS",18M86,M86,"IEEE Event",a094x00000EXYC2
a094x00000Bm0iK,"2020 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2069D,69D,"IEEE Event",a094x00000EXYC2
a094x00000Bm0uC,"2021 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2169D,69D,"IEEE Event",a094x00000EXYC2
a094x00001IYXBs,"2022 IEEE International Conference on Advanced Networks and Telecommunications Systems (ANTS)","ANTS",2269D,69D,"IEEE Event",a094x00000EXYC2
The excluded events' IDs are:
a094x00000Bm01T, since it has no parent event.
a094x00000EXYG1, a094x00000EXXu2 and a094x00000Blzml, since the first two are both parent events and the latter must be excluded as well because it's their child.
Notice that the 3 Digit Project Code for event with ID a094x00000EXYC2 is ??? since its children have two different 3 Digit Project Codes: 69D and M86.
To tackle this problem, we need to process a CSV file containing records of events, determine parent-child relationships among them, apply various exclusion rules, and finally output the filtered and structured data in a specific format.
Here's a step-by-step approach:
Parsing the CSV Input: Use Python's csv module to accurately parse each record, handling cases where fields might contain escaped quotes or commas.
Grouping by Acronym: Organize the records by their acronym. This helps in identifying which records are related to each other.
Identifying Parent and Child Events:
Parent Events: These are events with the record type "Parent Event". Each acronym should ideally have only one parent.
IEEE Events: These are child events associated with a parent via their acronym.
Applying Exclusion Rules:
Exclude any records with empty acronyms.
Exclude parent events that have no associated IEEE events (children).
Exclude any acronym group that has more than one parent event.
Exclude child events that don't have a corresponding parent.
Updating Parent's 3 Digit Project Code: For each valid parent, if all its children share the same 3-digit project code, update the parent's code to that value; otherwise, set it to "???".
Sorting and Formatting the Output:
Sort the serialized sets by acronym.
Within each set, the parent event is listed first, followed by its children sorted lexicographically by their titles. If titles are identical, sort by Event ID.
Ensure proper CSV formatting with correct escaping of quotes.
Below is the Python code implementing the described approach:
python
import sys
import csv
def main():
import sys
import csv
# Define a Record class to hold event data
class Record:
def __init__(self, event_id, event_title, acronym, project_code, three_digit_pc, record_type):
self.event_id = event_id
self.event_title = event_title
self.acronym = acronym
self.project_code = project_code
self.three_digit_pc = three_digit_pc
self.record_type = record_type
self.parent_id = None # To be set for children
# Read all lines from standard input
input_lines = sys.stdin.read().splitlines()
# Parse CSV using csv.reader
csv_reader = csv.reader(input_lines, delimiter=',', quotechar='"', escapechar='\\')
all_records = []
event_id_map = {}
for row in csv_reader:
# Handle cases where some fields might be missing
# Expected fields: 6
if len(row) < 6:
# Pad missing fields with empty strings
row += [''] * (6 - len(row))
event_id = row[0]
event_title = row[1]
acronym = row[2]
project_code = row[3]
three_digit_pc = row[4]
record_type = row[5]
record = Record(event_id, event_title, acronym, project_code, three_digit_pc, record_type)
all_records.append(record)
event_id_map[event_id] = record
# Build acronym_map: acronym -> {'parents': [...], 'children': [...]}
from collections import defaultdict
acronym_map = defaultdict(lambda: {'parents': [], 'children': []})
for record in all_records:
if record.acronym == '':
continue # Exclude records with no acronym (condition c)
if record.record_type == "Parent Event":
acronym_map[record.acronym]['parents'].append(record)
elif record.record_type == "IEEE Event":
acronym_map[record.acronym]['children'].append(record)
else:
# Unknown record type, can be ignored or handled as needed
pass
# Now, filter the sets based on conditions
included_sets = []
for acronym, group in acronym_map.items():
parents = group['parents']
children = group['children']
if len(parents) != 1:
continue # Exclude sets without exactly one parent (condition b)
if len(children) == 0:
continue # Exclude parent events without children (condition a)
parent = parents[0]
# Assign parent_id to children
for child in children:
child.parent_id = parent.event_id
included_sets.append((acronym, parent, children))
# Now, for each included set, update the parent's 3 Digit Project Code
for acronym, parent, children in included_sets:
child_three_pcs = set(child.three_digit_pc for child in children)
if len(child_three_pcs) == 1:
parent.three_digit_pc = child_three_pcs.pop()
else:
parent.three_digit_pc = "???"
# Sort the included sets by acronym
included_sets.sort(key=lambda x: x[0])
# Define a helper function to format CSV fields
def format_field(field):
if '"' in field:
field = field.replace('"', '""')
return f'"{field}"'
elif ',' in field or '\n' in field:
return f'"{field}"'
else:
return field
# Collect output lines
output_lines = []
for acronym, parent, children in included_sets:
# Format the parent record
parent_fields = [
parent.event_id,
parent.event_title,
parent.acronym,
parent.project_code,
parent.three_digit_pc,
parent.record_type
]
formatted_parent_fields = [format_field(field) for field in parent_fields]
parent_line = ",".join(formatted_parent_fields)
output_lines.append(parent_line)
# Sort the children lex by title, then by event_id if titles are same
children_sorted = sorted(children, key=lambda c: (c.event_title, c.event_id))
for child in children_sorted:
child_fields = [
child.event_id,
child.event_title,
child.acronym,
child.project_code,
child.three_digit_pc,
child.record_type,
child.parent_id
]
formatted_child_fields = [format_field(field) for field in child_fields]
child_line = ",".join(formatted_child_fields)
output_lines.append(child_line)
# Print all output lines
for line in output_lines:
print(line)
if __name__ == "__main__":
main()