0

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].

Aiden Chow
  • 391
  • 6
  • 17
  • Thanks to whoever suggested that link; I now know what is the problem. – Aiden Chow Nov 03 '20 at 01:18
  • To me this question seems not a proper duplicate of the referenced one because the mistake here seems the assumption that a **set** is sorted. The implementation of CPython 3.7 may create sorted sets but this isn't standardized. – Michael Butscher Nov 03 '20 at 01:31
  • @MichaelButscher When I was testing `list(set(list_name))` it seemed to always give the sorted list back, while removing duplicates. Does this not hold true in 3.4.0? – Aiden Chow Nov 03 '20 at 01:49
  • No. E.g. with 3.4.4 `list(set((3,2,6,4, 13,4)))` returns `[2, 3, 4, 13, 6]` for me (but may vary for others because it isn't standardized). Use `sorted(set((3,2,6,4, 13,4)))` instead. – Michael Butscher Nov 03 '20 at 01:55
  • @MichaelButscher Oh ok thanks. I will try implementing `sorted()` into my code. – Aiden Chow Nov 03 '20 at 01:58
  • @MichaelButscher Thanks for the help! Your suggestion fixed my code and I got all the test cases correct! I guess it wasn't a dictionary problem after all. – Aiden Chow Nov 03 '20 at 02:01
  • @AidenChow: If you use `sorted`, drop the `list` wrapping; `sorted` already implies a conversion to `list`, so the `list` wrapping just makes an extra temporary `list` that you immediately throw away. Instead of `ans=sorted(list(set(values)))[1]`, just do `ans=sorted(set(values))[1]`. – ShadowRanger Nov 03 '20 at 02:10
  • @ShadowRanger Thanks! I will keep that in mind next time I am doing a similar thing to this. – Aiden Chow Nov 03 '20 at 02:26

0 Answers0