Question: # Function to read data from an Excel file def read _ excel ( file _ path, sheet _ name = None ) : try:

# Function to read data from an Excel file
def read_excel(file_path, sheet_name=None):
try:
workbook = openpyxl.load_workbook(file_path)
if sheet_name:
active_sheet = workbook[sheet_name]
else:
active_sheet = workbook.active
data =[]
for row in active_sheet.iter_rows(min_row=2, values_only=True):
row_data =[cell_value for cell_value in row]
data.append(row_data)
return data
except FileNotFoundError:
print("File not found. Please provide a valid file path.")
return []
except Exception as e: # Catch broader errors
print(f"An error occurred: {e}")
return []
# Function to generate preferences based on votes
def generate_preferences(votes):
preferences ={}
for agent_id, agent_votes in enumerate(votes, start=1):
# Ensure agent_votes is a list before sorting
agent_votes_list = list(agent_votes) # Convert to list if necessary
# Sort indices based on values in agent_votes, breaking ties with original order
ordered_indices = sorted(range(len(agent_votes_list)), key=lambda i: (-agent_votes_list[i], i))
# Obtain ordered preferences from sorted indices
ordered_preferences =[]
for index in ordered_indices:
ordered_preferences.append(index +1) # Add 1 to align with 1-based indexing
preferences[agent_id]= ordered_preferences
return preferences

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!