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

1
  • 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? Commented Sep 21, 2010 at 8:52

3 Answers 3

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);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

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 :)

3 Comments

Though this implementation will work, it requires the input to be direct-accessible. A generic "IEnumerable" solution is to be preferred.
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.
@xtofl: Parameter type can be changed to ReadOnlyCollection<int>. But the solution of testalino is still file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.