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.
python sorting unit-testing
add a comment |
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.
python sorting unit-testing
add a comment |
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.
python sorting unit-testing
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
python sorting unit-testing
asked 2 mins ago
phwt
536
536
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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