3

Let's say I have a sequence of integers, from 0 (always starting at 0) to 3. Now, I have an array of integers, which will hold those sequence looped one after the other, starting from a certain point. For example:

An array of 10 elements, the sequence is 0 to 3, and start at 2, should yield 2, 3, 0, 1, 2, 3, 0, 1, 2, 3.

An array of 5 elements, the sequence 0 to 5, and start at 5, should yield 5, 0, 1, 2, 3.

An array of 5 elements, the sequence 0 to 10, and start at 3, should yield 3, 4, 5, 6, 7.

I'm suffering brain freeze! What's the best way to create this array if you know the array size, max number in sequence, and starting value?


My best attempt was:

private static int[] CreateIndexers(int index, int size, int players) 
{
  var indexers = new int[size];
  for (int i = 0; i < size; i++)
  { 
    var division = i / players; 
    var newInt = division + i >= players ? ((division + i) - players) : division + i;
    indexers[i] = newInt; 
  } 

  return indexers; 
} 
3
  • 4
    Can you post some code? Explain what you tried and where you are stuck. As it stands, this reads like you want us to write your code for you. Commented Feb 21, 2013 at 21:15
  • Did you tried anything so far? Commented Feb 21, 2013 at 21:16
  • Echoing the "What have you tried?" route - this question is far too "write it for me" as written. Commented Feb 21, 2013 at 21:19

5 Answers 5

9
public static IEnumerable<int> Foo(int count, int start, int max)
{
    return Enumerable.Range(0, count)
        .Select(n => (n + start) % (max + 1));
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Clever, very nice. It took me half a minute to anderstand how it works :)
+1 similar to what I have, but you can simplify by starting at start
@vlad Yeah, I suppose you can. It doesn't seem like the kind of operation worth spending too much time over though.
6
public int[] Cycle(int max, int start, int count)
{
    int cycles = count / max + 1;
    return Enumerable.Repeat(Enumerable.Range(0, max+1), cycles)
        .SelectMany(seq => seq)
        .Skip(start)
        .Take(count)
        .ToArray();
}

2 Comments

Awesome, I'd have never thought at this!
You may as well just repeat for int.MaxValue, since the Take ensures you never got to the end anyway.
3

Use LINQ:

public static IEnumerable<int> Foo(int length, int start, int end)
{
    return Enumerable.Range(start, length).Select(n => n % (end + 1));
}

Comments

0

Here's a less "LINQy" version of a solution:

    public static IEnumerable<int> GetSequence(int start, int end, int count)
    {
        var fullSequence = new List<int>();
        var baseRange = Enumerable.Range(0, end + 1);
        fullSequence.AddRange(baseRange.Skip(start));

        while (fullSequence.Count < count)
        {
            fullSequence.AddRange(baseRange);
        }

        return fullSequence.Take(count);
    }

2 Comments

You can write 'you can use Linq'
For a less LINQy solution, it still has a lot of LINQ in it...(not that that's a bad thing, just saying).
0
int numberOfElements = 10;
int sequenceStartElement = 0;
int sequenceCount = 4;
int firstElement = 2;

IEnumerable<int> sequence = Enumerable.Range(sequenceStartElement, sequenceCount)
int[] array = sequence
     //you could figure out a lesser number to Repeat... but it's deferred, doesn't matter.
  .Repeat(numberOfElements) 
  .SkipWhile(x => x != firstElement)
  .Take(numberOfElements)
  .ToArray();

What's the best way to create this array if you know the array size, max number in sequence, and starting value?

I see there are three listed inputs, instead of the four previously considered. Here's how to do it with your approach.

private static int[] CreateIndexers(int firstElement, int numberOfElements, int sequenceMax) 
{
  int sequenceCount = sequenceMax + 1
  var indexers = new int[numberOfElements];
  for (int i = 0; i < numberOfElements; i++)
  { 
    indexers[i] = (i + firstElement) % sequenceCount;
  } 
  return indexers; 
} 

Comments

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.