Reputation: 967
Below is a simple class that allows for setting HighTemps
and LowTemps
. There is also a read only property TempRange
that uses a loop in its' getter
to calculate. Is there a more elegant way to do this other than the loop? Given that TempRange will always be derived from HighTemps
and LowTemps
and that all three will basically have 7 items with an index, is there a better way to do this without a loop to set the value of TempRange
?
using System;
using System.Collections.Generic;
public class Program
{
public class Weather{
public Weather(Dictionary<int,int> highTemps,Dictionary<int,int> lowTemps,Dictionary<int,int> tempRange){
HighTemps = highTemps;
LowTemps = lowTemps;
}
public Dictionary<int,int> HighTemps {get;set;}
public Dictionary<int,int> LowTemps {get;set;}
public Dictionary<int,int> TempRange{
get{
Dictionary<int,int> result = new Dictionary<int,int>();
foreach (KeyValuePair<int, int> pair in HighTemps)
{
result.Add(pair.Key,pair.Value - LowTemps[pair.Key]);
}
return result;
}
}
}
public static void Main(){
Weather temps = new Weather(new Dictionary<int,int>(),new Dictionary<int,int>(),new Dictionary<int,int>());
//add day of week and temp
temps.HighTemps.Add(1, 84);
temps.HighTemps.Add(2, 86);
temps.HighTemps.Add(3, 81);
temps.HighTemps.Add(4, 82);
temps.HighTemps.Add(5, 82);
temps.HighTemps.Add(6, 83);
temps.HighTemps.Add(7, 84);
temps.LowTemps.Add(1, 65);
temps.LowTemps.Add(2, 66);
temps.LowTemps.Add(3, 71);
temps.LowTemps.Add(4, 60);
temps.LowTemps.Add(5, 64);
temps.LowTemps.Add(6, 69);
temps.LowTemps.Add(7, 70);
foreach (KeyValuePair<int, int> pair in temps.TempRange)
{
Console.WriteLine("The range for Day {0} of the week was {1}", pair.Key, pair.Value);
}
}
}
//Output:
The range for Day 1 of the week was 19
The range for Day 2 of the week was 20
The range for Day 3 of the week was 10
The range for Day 4 of the week was 22
The range for Day 5 of the week was 18
The range for Day 6 of the week was 14
The range for Day 7 of the week was 14
Upvotes: 0
Views: 110
Reputation: 31
What if you use Linq's toDictionary? https://www.dotnetperls.com/todictionary
public Dictionary<int, int> TempRange
{
get
{
return HighTemps.ToDictionary(k => k.Key, v => v.Value - LowTemps[v.Key]);
}
}
Upvotes: 0
Reputation: 12360
You can't avoid the loop happening, but you can avoid coding it by learning some Linq:
public Dictionary<int,int> TempRange
{
get {
return LowTemps.ToDictionary(l => l.Key, l => HighTemps[l.Key] - l.Value);
}
}
but I agree with all @Zyo's points.
Upvotes: 0
Reputation: 29
Here a possible solution to your problem. Create and work with a Object that represents a week day and the weather peaks (High and Low Temps) for that particular date. Additionaly we add a property range that calculates the difference. Create a list and add all days to it. After that just iterate each day and return the range. See my code example below. Open VS, create new Console App and paste example.
public class Program
{
public static void Main()
{
List<WeatherDay> weatherDays = new List<WeatherDay>();
//add day of week and temp
weatherDays.Add(new WeatherDay(1, 84, 65));
weatherDays.Add(new WeatherDay(2, 86, 66));
weatherDays.Add(new WeatherDay(3, 81, 71));
weatherDays.Add(new WeatherDay(4, 82, 60));
weatherDays.Add(new WeatherDay(5, 82, 64));
weatherDays.Add(new WeatherDay(6, 83, 69));
weatherDays.Add(new WeatherDay(7, 84, 70));
weatherDays.ForEach(wetherDay => {
Console.WriteLine("The range for Day {0} of the week was {1}", wetherDay.Day, wetherDay.Range);
});
Console.ReadLine();
}
}
public class WeatherDay
{
public int Day { get; set; }
public int HighTemp { get; set; }
public int LowTemp { get; set; }
public WeatherDay(int day, int highTemp, int lowTemp)
{
this.Day = day;
this.HighTemp = highTemp;
this.LowTemp = lowTemp;
}
public int Range { get { return HighTemp - LowTemp; } }
}
I hope this can help you further! Cheers!
Upvotes: 0
Reputation: 2068
I don't think there is a better way to do it than a loop.
Small things:
WeatherDay
. Something like:public class WeatherDay {
public int Min;
public int Max;
public int Range => Max - Min;
}
Upvotes: 1