Created
February 22, 2017 18:18
-
-
Save lorddev/d9ae3d189f461627149775c6e96145e2 to your computer and use it in GitHub Desktop.
Temp Tracker for Interview Cake
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
public class TempTracker | |
{ | |
// For mode | |
// Array of 0s at indices 0..110 | |
private readonly int[] _occurrences = new int[111]; | |
private int _maxOccurrences; | |
// For mean | |
private int _totalNumbers; | |
private int _totalSum; | |
// Mean should be double | |
public double? Mean { get; private set; } | |
public int? Mode { get; private set; } | |
// For min and max | |
public int? Min { get; private set; } | |
public int? Max { get; private set; } | |
public bool Insert(int temperature) | |
{ | |
// For mode | |
_occurrences[temperature]++; | |
if (_occurrences[temperature] > _maxOccurrences) | |
{ | |
Mode = temperature; | |
_maxOccurrences = _occurrences[temperature]; | |
} | |
// For mean | |
_totalNumbers++; | |
_totalSum += temperature; | |
Mean = (double) _totalSum / _totalNumbers; | |
// For min and max | |
if (Max == null || temperature > Max) | |
{ | |
Max = temperature; | |
} | |
if (Min == null || temperature < Min) | |
{ | |
Min = temperature; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment