如何在python中加快此算法 [英] How to quicken this algorithm in python
问题描述
问题陈述是:
得分最高的玩家在排行榜上排名为1
.
得分相等的玩家将获得相同的排名号码,下一位玩家将获得紧随其后的排名号码.
例如,四个玩家的得分分别为100、90、90、80和.这些球员的排名分别为1、2、2和3.
The player with the highest score is ranked 1
number on the leaderboard.
Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.
For example, four players have the scores 100, 90, 90, 80 and . Those players will have ranks 1, 2, 2 and 3, respectively.
使用这种排名方法,需要根据玩家在进行过程中所获得的分数来计算玩家的排名,并提供玩家分数作为输入(分数"列表).其次,提供了特定玩家的"alice's分数",对于她玩的每个游戏,都需要计算自己在排行榜上的位置.
Using this ranking method, need to calculate the ranking of a player based on the scores that he/she makes as it proceeds, player scores provided as an input( 'scores' list). Second, a particular player 'alice's' score is provided , for every game she plays, need to compute where she is placed on the leaderboard.
假设排行榜上有6位得分为[100,100,50,40,40,20,10]
的玩家.爱丽丝(Alice)玩她的第一场比赛,得到5分,她被排在排行榜的底部,排在第6位.下一场比赛,她得到25分,现在她排在第4位.接下来,她得到50分,在下一局比赛中,这是她的排名是2.
Suppose there are 6 players on the leaderboard with scores [100,100,50,40,40,20,10]
. Alice plays her first game, gets a score of 5, she is placed at the bottom of the list with rank 6. Next game she gets 25, now she is placed at rank 4. Next she gets 50, in the next game, this time her rank is 2.
#!/bin/python3
import sys
n = int(input().strip())
scores = [int(scores_temp) for scores_temp in input().strip().split(' ')]
m = int(input().strip())
alice = [int(alice_temp) for alice_temp in input().strip().split(' ')]
# your code goes here
for _ in alice:
scores.append(_)
print(list(sorted(set(scores),reverse=True)).index(_)+1)
此代码适用于200〜500范围内的输入.但是,如果输入非常高的输入(例如10 * 9),则代码会由于超时而终止.我想知道如何即兴编写此代码,以使其运行更快
This code works for inputs that are in range 200~500. But if it goes to very high inputs like 10*9, the code is terminating due to timeout. I would like to know how to improvise this code to make it run more quickly
编辑
n = int(input().strip())
scores = sorted(set([int(scores_temp) for scores_temp in input().strip().split(' ')]),reverse=True)
m = int(input().strip())
alice=[int(alice_temp) for alice_temp in input().split(' ')]
for a in alice:
rank=1
for _ in scores:
if(a<_):
rank=rank+1
else:
#print(rank)
break
print(rank)
进行了此更改,但是仍然遇到由于超时而终止"的情况,请提出需要修改的内容
Made this change, however still hitting 'terminating due to timeout', please suggest what needs to be modified
推荐答案
无需将Alice的分数附加到scores
.首先,请确保scores
从高到低排序.
There's no need to append Alice's scores to scores
. First make sure that scores
is sorted from high to low.
然后针对爱丽丝的每个分数,按以下方式计算其排名:
Then for each score of Alice, calculate her rank as follows:
从rank = 1
开始并遍历scores
的列表.每当您要查看的分数小于前一个分数时,在rank
中加1,并在您查看的分数等于或小于爱丽丝的分数时返回rank
的值.
Start with rank = 1
and iterate over the list of scores
. Add 1 to rank
every time the score you're looking at is smaller than the previous one and return the value of rank
as soon as the score you're looking at equals or is smaller than Alice's score.
此答案假设只有每个玩家的最高分数才有意义.
This answer assumes that only the highest score of each player is relevant.
这篇关于如何在python中加快此算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!