static void Main(string[] args)
{
LinkedList<Day> Days = new LinkedList<Day>();
Day day1 = new Day() { sunUp = 800, sunDown = 1800 };
Day day2 = new Day() { sunUp = 755, sunDown = 1805 };
Day day3 = new Day() { sunUp = 750, sunDown = 1810 };
Day day4 = new Day() { sunUp = 745, sunDown = 1815 };
Day day5 = new Day() { sunUp = 740, sunDown = 1820 };
Days.AddLast(day1);
Days.AddLast(day2);
Days.AddLast(day3);
Days.AddLast(day4);
Days.AddLast(day5);
Console.WriteLine("There is an average of {0} minutes of night over the past {1} days.", Calculator(Days), Days.Count);
}
public static int ToMinutes(int time)
{
return time / 100 * 60 + time - time / 100 * 100;
}
class Day
{
public int sunUp;
public int sunDown;
}
public static int Calculator(LinkedList<Day> Days)
{
var current = Days.First;
int nightDuration = 0;
int count = 0;
while (current.Next != null)
{
nightDuration += ToMinutes(current.Value.sunDown) - ToMinutes(current.Next.Value.sunUp);
current = current.Next;
count++;
}
return nightDuration/count;
}
Requirement: Day must be stored in a LinkedList.
Is there a clean Lambda Expression equivalent to the Calculator method above? I am having trouble using Lambda's to calculate a function with variables across connected nodes.
Thanks for the help!
Cheers, Kyle
Quite a few things are wrong with your pseudo code of using a calculator (for addition in this instance)
You have a class that has two integers. I believe your idea is to add the two numbers together along with two numbers from every other node of the linked list. But, you are adding only the first number from Pair and adding it to 2nd number of the next node... dont think it will work.
should be
Working Example
Ran this in the main,
Output
Lambda expression you can use to add up all the linked lists can be summed up as,
Output
Hope it helps