0

I have something like this:

long[] f = new long[4]{1,10,100,1000};

I want to divide 1000 by 100, 100 by 10 and 10 by 1

Is there a way to return results in an array with the results eg/ 10,10,10

UPDATE: This seems to confuse a few so here is another example

long[] f = new long[3]{1,2,6};

I want to divide 6 by 2 and 2 by 1 with the results in an array

Community
  • 1
  • 1
Jon
  • 38,814
  • 81
  • 233
  • 382
  • The question statement isn't clear for me. Do you want to divide each element to the corresponding power of ten? (10^i). Divide by previous element? In that case, what do you want to happen in the zero division case? – default locale Sep 21 '10 at 08:52

3 Answers3

4

I don't think aggregate is gonna help in that case... Zip would be better suited:

long[] f = new long[4]{1,10,100,1000};
long[] result = f.Skip(1).Zip(f, (a, b) => a / b);

EDIT: if you're on .NET 3.5, you can easily write a Zip extension method yourself:

    public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
    {

        if (first == null)
            throw new ArgumentNullException("first");
        if (second == null)
            throw new ArgumentNullException("second");
        if (selector == null)
            throw new ArgumentNullException("selector");

        return first.ZipIterator(second, selector);
    }

    private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
    {
        using (var enum1 = first.GetEnumerator())
        using (var enum2 = second.GetEnumerator())
        {
            while (enum1.MoveNext() && enum2.MoveNext())
            {
                yield return selector(enum1.Current, enum2.Current);
            }
        }
    }
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
3

If I understand you correctly, you probably don't want to use Aggregate here but instead Pairwise:

long[] result = f.Pairwise((x, y) => y / x).ToArray();

Here is an example implementation of Pairwise:

public static IEnumerable<TResult> Pairwise<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TSource, TResult> resultSelector)
{
    TSource previous = default(TSource);

    using (var it = source.GetEnumerator())
    {
        if (it.MoveNext())
            previous = it.Current;

        while (it.MoveNext())
            yield return resultSelector(previous, previous = it.Current);
    }
}

Source

If you want the results in reverse order then add a call to Reverse.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

Simple solution without using Linq:

    IEnumerable<long> GetResults(long[] input)
    {
        for (int i = input.Length -1; i >= 1; --i)
            yield return input[i] / input[i - 1];
    }

You can use Linq on the return value tough and it works in .NET 3.5 :)

testalino
  • 5,474
  • 6
  • 36
  • 48
  • 1
    Though this implementation will work, it requires the input to be direct-accessible. A generic "IEnumerable" solution is to be preferred. – xtofl Sep 21 '10 at 09:44
  • Well my solution solves the problem and it is very compact. I don't see why anyone would minus a working solution - thats ridiculous. Anyone can provide a better alternative if they know any. – testalino Sep 21 '10 at 10:03
  • @xtofl: Parameter type can be changed to ReadOnlyCollection. But the solution of testalino is still file. – Liton Sep 22 '10 at 07:38