Skip to content

Commit

Permalink
Merge pull request ows-ali#181 from cmattey/add-hackerrank-solutions
Browse files Browse the repository at this point in the history
Added solution for climbing the leaderboard problem 
:)
  • Loading branch information
ows-ali committed Jan 17, 2019
2 parents a421041 + abcda27 commit ed81043
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
94 changes: 94 additions & 0 deletions Hackerrank/climbing_the_leaderboard.py
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)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ In this repository, you can find the solutions (as source code) for the problems
| [Deepanshu Sinha](https://github.com/sinhaDroid) <br> <img src="https://github.com/sinhaDroid.png" width="100" height="100"> | India | Python, Android, Java, Javascript | https://www.linkedin.com/in/deepanshu-sinha/ |
| [Jared Moser](https://github.com/JM0S3R) <br> <img src="https://github.com/JM0S3R.png" width="100" height="100"> | United States | C++, Javascript | |
| [Matei Oltean](https://github.com/Matei13) <br> <img src="https://github.com/Matei13.png" width="100" height="100"> | France | OCaml | |
| [Chaitanya Mattey](https://github.com/cmattey/) <br> <img src="https://github.com/cmattey.png" width="100" height="100"> | United States | Python | |
| [Andreas Petridis](https://github.com/petridisa) <br> <img src="https://github.com/petridisa.png" width="100" height="100"> | Greece | Java | |
| [Lam Tran](https://github.com/Lam7150) <br> <img src="https://avatars2.githubusercontent.com/u/29765024?s=460&v=4" width="100" height="100"> | United States | Java | http://linkedin.com/in/lamgtran/ |

Expand Down

0 comments on commit ed81043

Please sign in to comment.