I would like some feedback on my solution for the trip programming challenge. Please go through my code understanding that I am interested in interviewing for a dev position at some point and am also interested in competitive programming (I'm just getting started):
''' @author: Teererai This code is intended to calculate the minimum that a number of students needs to exchange in order to get the ammounts that they spend to within one cent of each other. The idea is to calculate the average then round it up and down to the nearest cent. The next step is to calculate the cumulative absolute positive and negative difference for each ammount spen from the nearest rounded average spend and take the maximum between the cumulative positive and negative difference. ''' import sys from sys import stdin import math from decimal import Decimal def main(): num_students=int(stdin.readline()) #read number of students while num_students!=0: i=num_students total_spending=0 student_spending= # stores amount spend by each student pos_difference=0 # Keeps track of cumulative absolute difference between average spend and ammounts higher than the average. neg_difference=0 #Keeps track of cumulative negative difference. for i in range(i): current_spending=float(stdin.readline()) student_spending.append(current_spending) total_spending+=current_spending average_spending=total_spending/num_students #these following two variables account for the fact that we can never get to the exact average. lower_average_spending=math.floor(average_spending*100)/100 upper_average_spending=math.ceil(average_spending*100)/100 #As the challenge specifies, we are not aiming to get the ammount spend to the exact average but rather to either the nearest cent above and below it. for element in student_spending: if element>average_spending: pos_difference=pos_difference+element-upper_average_spending else: neg_difference=neg_difference+lower_average_spending-element amount_exchanged=Decimal(max(neg_difference,pos_difference)) amount_exchanged=round(amount_exchanged,2) print("$",amount_exchanged,sep="") num_students=int(stdin.readline()) return main() sys.exit(0)
p.s. the code works perfectly and has been accepted by the uva online judge. I am simply looking for advice.