Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# 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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions