So recently have been trying to practice for the USACO this year and I ran into the following problem: http://usaco.org/index.php?page=viewproblem2&cpid=687
I thought this was going to be a pretty easy question, at least easier than most of the other USACO problems, but then I ran into a problem I never faced before.
You see, my machine has Python at version 3.7.0, but the USACO grading server runs the code on version 3.4.0(or 2.7.6, but let's not worry about that). When I ran the first test case on my machine, everything went perfectly fine; I got the correct output. But when I submitted the code for USACO to grade, I failed the first test case.
That's when I realized that there must be some difference between 3.4.0 and 3.7.0 that I don't know about, and that is what is causing the output discrepancy between my machine and the USACO grading server.
Here is my code, commented for understandability:
import sys
fin=open("notlast.in","r")
fout=open("notlast.out","w")#accessing files
cowdict={'Bessie':0,'Elsie':0,'Daisy':0,'Gertie':0,'Annabelle':0,'Maggie':0,'Henrietta':0}#dictionary containing value of each cow
sessions=int(fin.readline())#read the first line and store as int
for _ in range(sessions):#read the next N lines
sessionlist=fin.readline().split()#read line and split into list
cowdict[sessionlist[0]]+=int(sessionlist[1])#add the value of the number to the key with the same name
values=[]
for i in cowdict:#iterate through each item in cowdict
values.append(cowdict[i])#append the values of each key in values
try:
ans=list(set(values))[1]#takes out duplicates and sorts the list, and choosing the 2nd element of that list
except:#catches error if there's only one cow or all cow values are the same
ans=0
fout.write("Tie")
sys.exit()
if values.count(ans)>1:#if multiple cows tie for second to last
fout.write("Tie\n")
else:
for i in range(7):#iterate from 0 to 7
if cowdict[list(cowdict.keys())[i]]==ans:#if the cow value at the i'th key in cowdict equals ans
fout.write(str(list(cowdict.keys())[i]))#output that key
break
As you can see, I have implemented a dictionary to keep track of the milk values for each cow, and used that dictionary to solve the problem.
My question is: What is the difference between 3.4.0 and 3.7.0 that can cause this difference?
EDIT:
Turns out a simple sorted()
function did the trick. Thank you @MichaelButscher for suggesting this fix!
The fix was: I replaced ans=list(set(values))[1]
with ans=sorted(list(set(values)))[1]
.