forked from ows-ali/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ows-ali#181 from cmattey/add-hackerrank-solutions
Added solution for climbing the leaderboard problem :)
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Problem Link: https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem | ||
|
||
# Complete the climbingLeaderboard function below. | ||
def climbingLeaderboard(scores, alice): | ||
|
||
def binsrch(scores,num): | ||
""" | ||
A binary search function that searches for a particular score in the scores | ||
array, and returns a tuple containing the possible location of the scores | ||
in the list and a boolean to show if the score existed in the list. | ||
Parameters | ||
------------------------------- | ||
scores: list[int] | ||
list of scores | ||
num: int | ||
integer to be searched in the scores list | ||
Returns | ||
-------------------------------- | ||
(pos,isFound): (int,bool) | ||
a tuple containing the (possilbe) position of num in the scores array | ||
and a boolean to indicate if the score was found in the array. | ||
""" | ||
start = 0 | ||
end = len(scores)-1 | ||
|
||
while start<=end: | ||
mid = start + int((end-start)/2) | ||
|
||
if scores[mid]>num: | ||
start = mid+1 | ||
elif scores[mid]<num: | ||
end = mid-1 | ||
elif scores[mid]==num: | ||
return (mid,True) | ||
|
||
if start<len(scores) and num<scores[start]: | ||
return (start+1,False) | ||
else: | ||
return (start-1,False) | ||
|
||
# Create position and found array to indicate, position of alices score in the | ||
# scores array, and a boolean array to indicate if they were already present in | ||
# the scores array or not. | ||
position = [] | ||
found = [] | ||
for score in alice: | ||
pos,isFound = binsrch(scores,score) | ||
position.append(pos) | ||
found.append(isFound) | ||
|
||
# Creating a rank array for scores, which indicates the rank for each entry | ||
# in the scores array | ||
rank = 1 | ||
rank_arr = [0]*len(scores) | ||
rank_arr[0] = 1 | ||
|
||
for i in range(1,len(scores)): | ||
if scores[i]==scores[i-1]: | ||
rank_arr[i]=rank_arr[i-1] | ||
else: | ||
rank+=1 | ||
rank_arr[i] = rank | ||
|
||
|
||
res = [] | ||
for i,pos in enumerate(position): | ||
if pos==-1: | ||
res.append(1) | ||
elif found[i]: | ||
res.append(rank_arr[pos]) | ||
else: | ||
res.append(rank_arr[pos]+1) | ||
|
||
return(res) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
|
||
scores = list(map(int, input().rstrip().split())) | ||
|
||
alice = list(map(int, input().rstrip().split())) | ||
|
||
result = climbingLeaderboard(scores, alice) | ||
|
||
# Sample Input : | ||
# 100 100 50 40 40 20 10 | ||
# 5 25 50 120 | ||
# Sample Output: | ||
# [6,4,2,1] | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters