Skip to content

Commit

Permalink
Added percentile ranking calculation based on all population.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisiaM committed Oct 21, 2017
1 parent 49592e1 commit a9838ca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
23 changes: 21 additions & 2 deletions BMI/BMI/BMIReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,29 @@ public string GetBmiCategory(double index)
public Dictionary<string, int> GetBmiPopulationReport(
List<UserDetails> measurmentsList)
{
return measurmentsList
.Select(o => GetBmiCategory(GetBmiIndex(o.Height, o.Weight)))
return GetBmiIndexForAllEntries(measurmentsList)
.Select(GetBmiCategory)
.GroupBy(o => o)
.ToDictionary(category => category.Key, category => category.Count());
}

public double GetUsersPercentile(List<UserDetails> measurmentsList, double userToFind)
{
var allEntries = GetBmiIndexForAllEntries(measurmentsList);
var list = allEntries.Append(userToFind).ToList();

list.Sort();
var indexInList = list.BinarySearch(userToFind);

var rank = indexInList / (double)list.Count * 100;

return Math.Round(rank, MidpointRounding.AwayFromZero);
}

private IEnumerable<double> GetBmiIndexForAllEntries(IEnumerable<UserDetails> measurmentsList)
{
return measurmentsList
.Select(o => GetBmiIndex(o.Height, o.Weight));
}
}
}
4 changes: 4 additions & 0 deletions BMI/BMI/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public IActionResult GuestUser(UserDetails details)
Count = o.Value
})
.ToList();

var usersRanking = _bmiReport.GetUsersPercentile(population, bmiIndex);

ViewData["UsersRanking"] = usersRanking;
}

return View();
Expand Down
1 change: 1 addition & 0 deletions BMI/BMI/IBmiReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IBmiReport
double GetBmiIndex(double height, double weight);
string GetBmiCategory(double index);
Dictionary<string, int> GetBmiPopulationReport(List<UserDetails> measurmentsList);
double GetUsersPercentile(List<UserDetails> measurmentsList, double userToFind);
}
}
8 changes: 6 additions & 2 deletions BMI/BMI/Views/Home/GuestUser.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

@Html.Partial("UserInput")

<h3>@ViewData["Result"]</h3>
<h4>@ViewData["Result"]</h4>

<h3>Population BMI Report</h3>
<br/>
<table border="1">
<tr>
<th>
Expand All @@ -30,4 +32,6 @@
</tr>
}

</table>
</table>

<h4> You rank in @ViewData["UsersRanking"]% percentile of the population.</h4>

0 comments on commit a9838ca

Please sign in to comment.