Scoreboard sorting (Secondary sort, skip duplicate ranking)











up vote
0
down vote

favorite












I have two different scoreboard sorting function but one of them is not working in some test cases (which I don't know).



Example input/output



Input: 4 Contestants and then the list of the score.



4
AAA 3 2 1
CCC 2 3 1
BBB 2 3 1
DDD 1 1 0


Output: BBB, CCC is using the same ranking so there is no 3rd place then DDD is using 4th place instead



1 AAA 3 2 1 6
2 BBB 2 3 1 6
2 CCC 2 3 1 6
4 DDD 1 1 0 2


Sorting Order (Primary, Secondary, Tertiary, Quaternary)




  • Gold Medal (Descending - Most on top)

  • Silver Medal (Descending)

  • Bronze Medal (Descending)

  • Contestant's name (Alphabetical Order)


Here's the first one (Working Correctly)



This one uses dictionary characteristic which key cannot be duplicated.



ranking = dict()
for _ in range(int(input())):
lst = input().split()
contestant, score = lst[0], tuple(map(int, lst[1:]))
if ranking.get(score): #If same score exist append it
ranking.get(score).append(contestant)
else: ranking[score] = [contestant] #If not create new key

ranking = list(map(lambda i: (i[0], sorted(i[1])), sorted(ranking.items(), reverse=True)))
rank = 1

for i, j in ranking: #Print out the result
for k in j:
print(rank, k, " ".join(map(str, i)), sum(i))
rank += len(j) #Skip the duplicate rank


Here's what ranking will look like:



[((3, 2, 1), ['AAA']), ((2, 3, 1), ['BBB', 'CCC']), ((1, 1, 0), ['DDD'])]


Second One (Working Correctly but not on some cases)



This one just appends to the list and check for a duplicate when printing.



ranking = [input().split() for _ in range(int(input()))]
#Convert each items into format like (Contestant, Gold, Silver, Bronze, Total)
ranking = map(lambda i: [i[0]] + list(map(int, i[1:])) + [sum(map(int, i[1:]))], ranking)
#Sort by descending on medals but ascending on contestant's name
ranking = sorted(ranking, key=lambda i: (-i[1], -i[2], -i[3], i[0]))

rank = 0 #Current Ranking
same = 0 #1 if last item's have the same score else 0
last = #Store last printed score
for i in ranking:
if i[1:] != last:
rank += 1
else: same = 1
if same and i[1:] != last:
same = 0
rank += 1
print(rank, *i)
last = i[1:]


ranking for this one



[['AAA', 3, 2, 1, 6], ['BBB', 2, 3, 1, 6], ['CCC', 2, 3, 1, 6], ['DDD', 1, 1, 0, 2]]


I have no idea what causes these two to output different results.









share


























    up vote
    0
    down vote

    favorite












    I have two different scoreboard sorting function but one of them is not working in some test cases (which I don't know).



    Example input/output



    Input: 4 Contestants and then the list of the score.



    4
    AAA 3 2 1
    CCC 2 3 1
    BBB 2 3 1
    DDD 1 1 0


    Output: BBB, CCC is using the same ranking so there is no 3rd place then DDD is using 4th place instead



    1 AAA 3 2 1 6
    2 BBB 2 3 1 6
    2 CCC 2 3 1 6
    4 DDD 1 1 0 2


    Sorting Order (Primary, Secondary, Tertiary, Quaternary)




    • Gold Medal (Descending - Most on top)

    • Silver Medal (Descending)

    • Bronze Medal (Descending)

    • Contestant's name (Alphabetical Order)


    Here's the first one (Working Correctly)



    This one uses dictionary characteristic which key cannot be duplicated.



    ranking = dict()
    for _ in range(int(input())):
    lst = input().split()
    contestant, score = lst[0], tuple(map(int, lst[1:]))
    if ranking.get(score): #If same score exist append it
    ranking.get(score).append(contestant)
    else: ranking[score] = [contestant] #If not create new key

    ranking = list(map(lambda i: (i[0], sorted(i[1])), sorted(ranking.items(), reverse=True)))
    rank = 1

    for i, j in ranking: #Print out the result
    for k in j:
    print(rank, k, " ".join(map(str, i)), sum(i))
    rank += len(j) #Skip the duplicate rank


    Here's what ranking will look like:



    [((3, 2, 1), ['AAA']), ((2, 3, 1), ['BBB', 'CCC']), ((1, 1, 0), ['DDD'])]


    Second One (Working Correctly but not on some cases)



    This one just appends to the list and check for a duplicate when printing.



    ranking = [input().split() for _ in range(int(input()))]
    #Convert each items into format like (Contestant, Gold, Silver, Bronze, Total)
    ranking = map(lambda i: [i[0]] + list(map(int, i[1:])) + [sum(map(int, i[1:]))], ranking)
    #Sort by descending on medals but ascending on contestant's name
    ranking = sorted(ranking, key=lambda i: (-i[1], -i[2], -i[3], i[0]))

    rank = 0 #Current Ranking
    same = 0 #1 if last item's have the same score else 0
    last = #Store last printed score
    for i in ranking:
    if i[1:] != last:
    rank += 1
    else: same = 1
    if same and i[1:] != last:
    same = 0
    rank += 1
    print(rank, *i)
    last = i[1:]


    ranking for this one



    [['AAA', 3, 2, 1, 6], ['BBB', 2, 3, 1, 6], ['CCC', 2, 3, 1, 6], ['DDD', 1, 1, 0, 2]]


    I have no idea what causes these two to output different results.









    share
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have two different scoreboard sorting function but one of them is not working in some test cases (which I don't know).



      Example input/output



      Input: 4 Contestants and then the list of the score.



      4
      AAA 3 2 1
      CCC 2 3 1
      BBB 2 3 1
      DDD 1 1 0


      Output: BBB, CCC is using the same ranking so there is no 3rd place then DDD is using 4th place instead



      1 AAA 3 2 1 6
      2 BBB 2 3 1 6
      2 CCC 2 3 1 6
      4 DDD 1 1 0 2


      Sorting Order (Primary, Secondary, Tertiary, Quaternary)




      • Gold Medal (Descending - Most on top)

      • Silver Medal (Descending)

      • Bronze Medal (Descending)

      • Contestant's name (Alphabetical Order)


      Here's the first one (Working Correctly)



      This one uses dictionary characteristic which key cannot be duplicated.



      ranking = dict()
      for _ in range(int(input())):
      lst = input().split()
      contestant, score = lst[0], tuple(map(int, lst[1:]))
      if ranking.get(score): #If same score exist append it
      ranking.get(score).append(contestant)
      else: ranking[score] = [contestant] #If not create new key

      ranking = list(map(lambda i: (i[0], sorted(i[1])), sorted(ranking.items(), reverse=True)))
      rank = 1

      for i, j in ranking: #Print out the result
      for k in j:
      print(rank, k, " ".join(map(str, i)), sum(i))
      rank += len(j) #Skip the duplicate rank


      Here's what ranking will look like:



      [((3, 2, 1), ['AAA']), ((2, 3, 1), ['BBB', 'CCC']), ((1, 1, 0), ['DDD'])]


      Second One (Working Correctly but not on some cases)



      This one just appends to the list and check for a duplicate when printing.



      ranking = [input().split() for _ in range(int(input()))]
      #Convert each items into format like (Contestant, Gold, Silver, Bronze, Total)
      ranking = map(lambda i: [i[0]] + list(map(int, i[1:])) + [sum(map(int, i[1:]))], ranking)
      #Sort by descending on medals but ascending on contestant's name
      ranking = sorted(ranking, key=lambda i: (-i[1], -i[2], -i[3], i[0]))

      rank = 0 #Current Ranking
      same = 0 #1 if last item's have the same score else 0
      last = #Store last printed score
      for i in ranking:
      if i[1:] != last:
      rank += 1
      else: same = 1
      if same and i[1:] != last:
      same = 0
      rank += 1
      print(rank, *i)
      last = i[1:]


      ranking for this one



      [['AAA', 3, 2, 1, 6], ['BBB', 2, 3, 1, 6], ['CCC', 2, 3, 1, 6], ['DDD', 1, 1, 0, 2]]


      I have no idea what causes these two to output different results.









      share













      I have two different scoreboard sorting function but one of them is not working in some test cases (which I don't know).



      Example input/output



      Input: 4 Contestants and then the list of the score.



      4
      AAA 3 2 1
      CCC 2 3 1
      BBB 2 3 1
      DDD 1 1 0


      Output: BBB, CCC is using the same ranking so there is no 3rd place then DDD is using 4th place instead



      1 AAA 3 2 1 6
      2 BBB 2 3 1 6
      2 CCC 2 3 1 6
      4 DDD 1 1 0 2


      Sorting Order (Primary, Secondary, Tertiary, Quaternary)




      • Gold Medal (Descending - Most on top)

      • Silver Medal (Descending)

      • Bronze Medal (Descending)

      • Contestant's name (Alphabetical Order)


      Here's the first one (Working Correctly)



      This one uses dictionary characteristic which key cannot be duplicated.



      ranking = dict()
      for _ in range(int(input())):
      lst = input().split()
      contestant, score = lst[0], tuple(map(int, lst[1:]))
      if ranking.get(score): #If same score exist append it
      ranking.get(score).append(contestant)
      else: ranking[score] = [contestant] #If not create new key

      ranking = list(map(lambda i: (i[0], sorted(i[1])), sorted(ranking.items(), reverse=True)))
      rank = 1

      for i, j in ranking: #Print out the result
      for k in j:
      print(rank, k, " ".join(map(str, i)), sum(i))
      rank += len(j) #Skip the duplicate rank


      Here's what ranking will look like:



      [((3, 2, 1), ['AAA']), ((2, 3, 1), ['BBB', 'CCC']), ((1, 1, 0), ['DDD'])]


      Second One (Working Correctly but not on some cases)



      This one just appends to the list and check for a duplicate when printing.



      ranking = [input().split() for _ in range(int(input()))]
      #Convert each items into format like (Contestant, Gold, Silver, Bronze, Total)
      ranking = map(lambda i: [i[0]] + list(map(int, i[1:])) + [sum(map(int, i[1:]))], ranking)
      #Sort by descending on medals but ascending on contestant's name
      ranking = sorted(ranking, key=lambda i: (-i[1], -i[2], -i[3], i[0]))

      rank = 0 #Current Ranking
      same = 0 #1 if last item's have the same score else 0
      last = #Store last printed score
      for i in ranking:
      if i[1:] != last:
      rank += 1
      else: same = 1
      if same and i[1:] != last:
      same = 0
      rank += 1
      print(rank, *i)
      last = i[1:]


      ranking for this one



      [['AAA', 3, 2, 1, 6], ['BBB', 2, 3, 1, 6], ['CCC', 2, 3, 1, 6], ['DDD', 1, 1, 0, 2]]


      I have no idea what causes these two to output different results.







      python sorting unit-testing





      share












      share










      share



      share










      asked 2 mins ago









      phwt

      536




      536



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209341%2fscoreboard-sorting-secondary-sort-skip-duplicate-ranking%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209341%2fscoreboard-sorting-secondary-sort-skip-duplicate-ranking%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'