Click here to Skip to main content
15,886,103 members
Home / Discussions / C#
   

C#

 
GeneralRe: Coding Challenge Pin
PIEBALDconsult26-Aug-17 15:26
mvePIEBALDconsult26-Aug-17 15:26 
GeneralRe: Coding Challenge Pin
Mycroft Holmes26-Aug-17 15:33
professionalMycroft Holmes26-Aug-17 15:33 
GeneralRe: Coding Challenge Pin
PIEBALDconsult26-Aug-17 16:14
mvePIEBALDconsult26-Aug-17 16:14 
QuestionRe: Coding Challenge Pin
PIEBALDconsult26-Aug-17 16:56
mvePIEBALDconsult26-Aug-17 16:56 
AnswerRe: Coding Challenge Pin
Mycroft Holmes26-Aug-17 22:20
professionalMycroft Holmes26-Aug-17 22:20 
AnswerRe: Coding Challenge Pin
BillWoodruff26-Aug-17 19:44
professionalBillWoodruff26-Aug-17 19:44 
AnswerRe: Coding Challenge Pin
Peter_in_278027-Aug-17 1:41
professionalPeter_in_278027-Aug-17 1:41 
AnswerRe: Coding Challenge Pin
Richard Deeming29-Aug-17 2:28
mveRichard Deeming29-Aug-17 2:28 
Assuming SQL Server 2012 or later, and the existence a field in your table which defines the order in which you want to process the rows:
SQL
WITH cte As
(
    SELECT
        ROW_NUMBER() OVER (ORDER BY SeqNo) As RN,
        (TheValue - LAG(TheValue) OVER (ORDER BY SeqNo)) / TheValue As Change
    FROM
        YourTable
)
SELECT
    Avg(Change) As AverageChange
FROM
    cte
WHERE
    RN Between 1 And 4
;

Result (using decimal(20, 7) for the source column type): 0.07106985920545242

For C#, using LINQ:
C#
double average = values.Skip(1).Take(3)
    .Zip(values, (second, first) => (second - first) / second)
    .Average();

Result (using decimal type): 0.0710698592054524257914088423
Result (using double type): 0.0710698592054524



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Coding Challenge Pin
Mycroft Holmes4-Sep-17 21:06
professionalMycroft Holmes4-Sep-17 21:06 
SuggestionRe: Coding Challenge Pin
Richard Deeming5-Sep-17 1:55
mveRichard Deeming5-Sep-17 1:55 
GeneralRe: Coding Challenge Pin
Mycroft Holmes4-Sep-17 22:24
professionalMycroft Holmes4-Sep-17 22:24 
GeneralRe: Coding Challenge Pin
Pete O'Hanlon4-Sep-17 23:24
mvePete O'Hanlon4-Sep-17 23:24 
GeneralRe: Coding Challenge Pin
Richard Deeming5-Sep-17 2:04
mveRichard Deeming5-Sep-17 2:04 
AnswerRe: Coding Challenge Pin
Pete O'Hanlon29-Aug-17 3:53
mvePete O'Hanlon29-Aug-17 3:53 
Questionc++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:04
User 1106097925-Aug-17 8:04 
AnswerRe: c++ const in c# (I assume, once again) Pin
OriginalGriff25-Aug-17 8:37
mveOriginalGriff25-Aug-17 8:37 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:43
User 1106097925-Aug-17 8:43 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 9:01
User 1106097925-Aug-17 9:01 
GeneralRe: c++ const in c# (I assume, once again) Pin
harold aptroot25-Aug-17 8:41
harold aptroot25-Aug-17 8:41 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 8:47
User 1106097925-Aug-17 8:47 
AnswerRe: c++ const in c# (I assume, once again) Pin
jschell25-Aug-17 9:40
jschell25-Aug-17 9:40 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 9:50
User 1106097925-Aug-17 9:50 
AnswerRe: c++ const in c# (I assume, once again) Pin
Pete O'Hanlon25-Aug-17 12:19
mvePete O'Hanlon25-Aug-17 12:19 
GeneralRe: c++ const in c# (I assume, once again) Pin
User 1106097925-Aug-17 12:47
User 1106097925-Aug-17 12:47 
AnswerRe: c++ const in c# (I assume, once again) Pin
Gerry Schmitz26-Aug-17 8:54
mveGerry Schmitz26-Aug-17 8:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.