We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My loop logic got a bit dense and though I tried to make this as easy to read as possible, I think it's improvable.
Anybody have any ideas on ways I can improve my approach?
defclimbingLeaderboard(ranked,player):# pre-process ranked to remove duplicatesrScoresProc=list(dict.fromkeys(ranked))# a dictionary to keep track of the rank value of each player scoreplayersRanks={score:0forscoreinplayer}# create a reversed, sorted, duplicate removed version of playerpScoresProc=list(dict.fromkeys(player))pScoresProc.sort(reverse=True)# traverse through rScoresProc and pScoresProc# and update playersRanks with the rank for each score# index for processed player scorespIdx=0# index for processed ranked scoresrIdx=0# while the scores haven't been exhaustedwhilepIdx<len(pScoresProc):# if a score is placed here, the rank will be 1 more than the rank indexrank=rIdx+1# the current pScore and rScore given the indexcurrPScore=pScoresProc[pIdx]currRScore=rScoresProc[rIdx]# if the current player score is greater than the current ranked scoreifcurrPScore>=currRScore:# update playersRanksplayersRanks.update({currPScore:rank})# move to the next score in pScoresProc if there are still scoresifpIdx<len(pScoresProc)-1:pIdx+=1continueelse:break# if the current player score >= current ranked scoreelse:# increment the rank being considered if possibleifrIdx<len(rScoresProc)-1:rIdx+=1continue# otherwise, the player score is the lowest possible scoreelse:# update playersRanksplayersRanks.update({currPScore:rank+1})pIdx+=1# the final resultresult=[]# create the result from the dict info given the original player arrayforscoreinplayer:result.append(playersRanks.get(score))returnresult
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
My solution in Python3
My loop logic got a bit dense and though I tried to make this as easy to read as possible, I think it's improvable.
Anybody have any ideas on ways I can improve my approach?