Problem Description: Given an array (or list) of scores, return the array of ranks for each value in the array. The largest value has rank 1, the second largest value has rank 2, and so on. Ties should be handled by assigning the same rank to all tied values. For example:
ranks([9,3,6,10]) = [2,4,3,1]
and
ranks([3,3,3,3,3,5,1]) = [2,2,2,2,2,1,7]
because there is one 1st place value, a five-way tie for 2nd place, and one in 7th place.
Level: 6kyu
Link To Kata: Rank Vector
In this Kata for every element in the given array we will be returning its rank in the vector. We start solving this Kata by creating a copy of the given array using the Javascript slice function. Then we sort the array in decreasing order. We create an empty answer array and then loop through every element of the given array. For every element in the array we check the index of the element in the sorted copy of the array which we created. We push the sum of the index and 1 to the answer array. We repeat this for every element in the given array. Then we return the answer array as the solution to this Kata.
Here is the link to code: Kata Day 59
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
All links to code says "Page not found" :'(
ReplyDeleteSorry for the trouble Gilberto. Here is the link to the repository. You can see all the code solutions here.
Deletehttps://github.com/varunu28/A-Kata-A-Day