c# - Function to look through list and determine trend -
so have list of items. each item on list has property called notional. now, list sorted. need is, develop function sets type of list 1 of following:
- bullet -
notionalsame every item - amortizing -
notionaldecreases on course of schedule (might stay same element element should never go up, , should end lower) - accreting -
notionalincreases on course of schedule (might stay same element element should never go down, , should end higher) - rollercoaster -
notionalgoes , down (could end same, higher, or lower, shouldn't same each element , shouldn't classfied other types)
what method , efficient way go through list , figure out?
thanks!
this straightforward way it:
bool hasgoneup = false; bool hasgonedown = false; t previous = null; // t type of objects in list; assuming ref type foreach(var item in list) { if (previous == null) { previous = item; continue; } hasgoneup = hasgoneup || item.notional > previous.notional; hasgonedown = hasgonedown || item.notional < previous.notional; if(hasgoneup && hasgonedown) { return trend.rollercoaster; } previous = item; } if (!hasgoneup && !hasgonedown) { return trend.bullet; } // 1 of hasgoneup , hasgonedown true point return hasgoneup ? trend.accreting : trend.amortizing;
Comments
Post a Comment