Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerable for negative ranges

Tags:

python

c#

range

In python we have a range that can produce for an array with negative integers too.

For example:

In[4]: range(-2, 2+1)
Out[4]: [-2, -1, 0, 1, 2]

Is there an equivalent system in C#; I am aware of IEnumerable method, but upon trying it I get the following output:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine("Hello, world!");
            int half_window = 2;
            var mySeq1 = Enumerable.Range(-2, 2+1).ToArray();
            foreach(var item in mySeq1)
    Console.Write(item.ToString() + " ");
        } 
    }
}

Produces the output:

Hello, world!
-2 -1 0

Is there already an inbuilt method that I can use to get the python's output ?

like image 697
opto abhi Avatar asked Mar 29 '16 14:03

opto abhi


1 Answers

The second argument of Enumerable.Range specifies the count of the elements to be generated, not the stop value.

Console.WriteLine("Hello, world!");
int half_window = 2;
var mySeq1 = Enumerable.Range(-2, 5).ToArray();
//                                ^
foreach(var item in mySeq1)
    Console.Write(item.ToString() + " ");

Output:

Hello, world!
-2 -1 0 1 2 
like image 181
cbr Avatar answered Sep 23 '22 22:09

cbr