Last active
October 13, 2023 18:01
-
-
Save theredpea/b3e6521a7bb626a617a04a3c23be545b to your computer and use it in GitHub Desktop.
Learning SortedList
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
// Online C# Editor for free | |
// Write, Edit and Run your C# code using C# Online Compiler | |
class Solution { | |
// TODO: maybe dont need to store class/object TradingEvent | |
public class TradingEvent { | |
// | |
// public DateTime EventTime; | |
public long EventTime; | |
public string EventTicker; | |
public float EventPrice; | |
} | |
static Dictionary<string, SortedList<long, TradingEvent>> SortedEventsByTicker = new Dictionary<string, SortedList<long, TradingEvent>>(); | |
public void Receive( | |
long EventTime, | |
string EventTicker, | |
float? EventPrice, | |
long? NewTime = null, | |
float? NewPrice = null) { | |
// O(1)-- within capacity | |
// O(n)-- resize | |
// if Dictionary | |
// O(1)-- within capacity | |
// No need to resize: | |
// ~~O(n)-- resize~~ | |
// #region List implementation | |
SortedEventsByTicker[EventTicker] | |
= SortedEventsByTicker.GetValueOrDefault(EventTicker) ?? new SortedList<long, TradingEvent>(); | |
if (NewTime is not null) { | |
if (!SortedEventsByTicker[EventTicker].ContainsKey(EventTime)){ | |
throw new ArgumentException($"No trading event found with time {EventTime}"); | |
} | |
// Update (Potentially deleting two records) | |
TradingEvent existingEvent = SortedEventsByTicker[EventTicker][EventTime]; | |
existingEvent.EventTime = NewTime.Value; | |
SortedEventsByTicker[EventTicker].Remove(EventTime); | |
// Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: 2 (Parameter 'key') | |
if (SortedEventsByTicker[EventTicker].ContainsKey(NewTime.Value)){ | |
SortedEventsByTicker[EventTicker].Remove(NewTime.Value); | |
} | |
SortedEventsByTicker[EventTicker].Add(NewTime.Value,existingEvent); | |
} else if (NewPrice is not null){ | |
if (!SortedEventsByTicker[EventTicker].ContainsKey(EventTime)){ | |
throw new ArgumentException($"No trading event found with time {EventTime}"); | |
} | |
SortedEventsByTicker[EventTicker].GetValueOrDefault(EventTime).EventPrice = NewPrice.Value; | |
} else { | |
TradingEvent e = new TradingEvent { | |
EventTime = EventTime, | |
EventTicker = EventTicker, | |
EventPrice = EventPrice.Value | |
}; | |
SortedEventsByTicker[EventTicker].Add(EventTime, e); | |
} | |
// #region Dictionary allowing changes | |
// List Dictionary | |
// SortedList | |
// if (!newTime.IsNull()) { | |
// TradingEvent updateEvent = EventsByTicker[EventTicker] | |
// .Select(e=>e.EventTime == EventTime) | |
// .FirstOrDefault(); | |
// updateEvent.EventTime = newTime.Value; | |
// } | |
// else if (!newPrice.IsNull()){ | |
// TradingEvent updateEvent = EventsByTicker[EventTicker] | |
// .Select(e=>e.EventTime == EventTime) | |
// .FirstOrDefault(); | |
// updateEvent.EventPrice = newPrice.Value; | |
// } | |
// #endregion | |
} | |
public float? FetchLatestPrice(string EventTicker) { | |
// O(n) | |
// if Dictionary... | |
// O(1) | |
// float? latestPrice = TradingEvents | |
// // only want rows with the right ticker | |
// .Where(e=>e.EventTicker==EventTicker) | |
// // ascending; chronological | |
// .Sort(e=>-e.EventTime) | |
// // one row; and only the price | |
// .FistOrDefault(e=>e.EventPrice); | |
// // talk about reuqirements; what to return if there is no Event of that ticker | |
// return (latestPrice ?? -1); | |
// TradingEvent latestEvent = LatestTradingEventByTicker.GetValueOrDefault(EventTicker); | |
// Dictionary<string, Dictionary<long, TradingEvent>> | |
// // TODO: first challenge; now multiple levels of null-checking | |
// LatestEventByTickerByTime[EventTicker] | |
// .ToList() | |
// .Sort(kvp=>-kvp.Value.EventTime) | |
// .FistOrDefault(e=>e.EventPrice); | |
float? eventPrice = ( | |
( | |
(SortedEventsByTicker | |
.GetValueOrDefault(EventTicker) | |
?.LastOrDefault() | |
) | |
?.Value | |
// TODO: maybe dont need to store class/object TradingEvent | |
?.EventPrice | |
) | |
); | |
// otherwise "Nullable object must have a value" | |
return eventPrice;//.Value; | |
} | |
} | |
class Program { | |
public static void Main (string[] args) { | |
Solution i = new Solution(); | |
i.Receive(1,"FDS",1.24f); | |
i.Receive(3,"FDS",3.24f); | |
i.Receive(2,"FDS",2.24f); | |
i.Receive(5,"FDS",5.24f); | |
Console.WriteLine(i.FetchLatestPrice("FDS")); | |
// static Dictionary<string,float> ["FDS"] --> 5.24 | |
// [1,1.24] [2,2.24] [3,3.24] [5,5.24] --> 5.24 | |
// New parameters for recieve function newTime, newPrice | |
// static Dictionary<string, TradingEvent> | |
// static Dictionary<string,float> | |
// static Dictionary<string,float> ["FDS"] --> 3.24 | |
i.Receive(5,"FDS",null,2,null); // Updating time , [1,1.24] [2,5.24] [3,3.24] --> 3.24 | |
Console.WriteLine(i.FetchLatestPrice("FDS")); | |
i.Receive(3,"FDS",null,null,3.3334f); // Updating price [1,1.24] [2,3.24] [3,3.24] --> 3.24 | |
Console.WriteLine(i.FetchLatestPrice("FDS")); | |
i.FetchLatestPrice("FDS"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment