samedi 27 juin 2015

How can you read a CSV file in c# with different number of columns

I'm a beginner with c#, I have a csv file of integer data with 140 rows and every row has different number of columns csv file looks like this:

1,1,2,2,3

1,2,2,2,4,4,4,4

6,7

how can i load every row into a new array using a loop which has similar action like

int[][] inputSequences = {

    new[] { 1, 1, 2, 2, 3 },        
    new[] { 1, 2, 2, 2, 4, 4, 4, 4 },     
    new[] { 6, 7 },     

}

How to merge three sorted arrays without using any unnecessary space?

For example I've written C++ code to merge three arrays, but here you can see that first I had to merge first two arrays and then merge its resulting array to third array..

while (p < n1 && q < n2)    // n1,n2,n3 are sizes of a,b,c respectively 
{
    if (a[p] < b[q])    
    {
        res[r] = a[p];  // res is array is intermediate merged array
        r++;
        p++;
    }
    else
    {
        res[r] = b[q];
        r++;
        q++;
    }
}

while (q < n2)
{
    res[r] = b[q];
    r++;
    q++;
}

while (p < n1)
{
    res[r] = a[p];
    r++;
    p++;
}

while (s < r && t < n3)
{
    if (res[s] < c[t])
    {
        res2[r2] = res[s];  // res2 is finally merged array
        r2++;
        s++;
    }
    else
    {
        res2[r2] = c[t];
        r2++;
        t++;
    }
}

while (s < r)
{
    res2[r2] = res[s];
    s++;
    r2++;
}

while (t < n3)
{
    res2[r2] = c[t];
    r2++;
    t++;
}

I don't want to use intermediate array in my program.Is there any way I can do it?

Also, Is there any methodology using which we can merge any number of sorted arrays in one go ?

How to create a Ruby array slice which shares the same instance of the original array?

So that if the slice gets changed, the original array will also change?

a = [1, 2, 3]
b = a[1, 2]
b[0] = 42 # due to COW, only b changes, a remains unchanged

Expected result: when run b[0] = 42, a[1] will also changed to 42.

error in creating array of linked list

I have to put nodes of binary search tree of every level in a linked list. That is if the height of the tree is 'h' then 'h+1' linked lists would be created and then each linked list would have all the nodes of each level. For this I have thought of creating an array of linked list. But the nodes are not being inserted in the list I guess. The code is as follows:-

struct node{ 
    int data;
    struct node *left;
    struct node *right;
    };

struct linked_list
{

    int data;
    struct linked_list *next;
};

    linkedlistofbst(struct node *new,struct linked_list *n1[], int level)
    {
    //printf("%d ",new->data);
    if(new==NULL)
    {
        return;
    }

    if(n1[level]==NULL)
    {
        struct linked_list *a =(struct linked_list *)malloc(sizeof(struct linked_list));
        a->data=new->data;
        a->next=NULL;
        n1[level]=a;
        printf("%d ",a->data);
    }
    else
    {
        struct linked_list *b =(struct linked_list *)malloc(sizeof(struct     linked_list));
        while(n1[level]->next!=NULL)
        {
            n1[level]=n1[level]->next;
        }
        b->data=new->data;
        b->next=NULL;
        n1[level]=b;
    }
    linkedlistofbst(new->left,n1,level+1);
    linkedlistofbst(new->right,n1,level+1);
    }

    main()

{
    struct linked_list *l=(struct linked_list *)malloc((a+1)*sizeof(struct    linked_list));//'a' is the height of the tree
    linkedlistofbst(new,&l, 0);//new is the pointer to the root node of the tree.
}

Unexpected behavior of Array.prototype.splice

While implementing an internal EventEmitter for a project I was working on, I came across a strange quirk when using Array.prototype.splice inside a for... in loop. The function does not successfully remove the indeces from the array within the loop:

var array = [1, 2, 3, 4, 5], index;

for (index in array) {
    if (index === 2) {
        array.splice(index, 1);
    }

    console.log(array[index]);
}

console.log(array);

Running on Google Chrome version 43, this outputs

1
2
3
4
5
[1, 2, 3, 4, 5]

when I'm expecting something like

1
2
4
5
undefined†
[1, 2, 4, 5]

Is this intential behavior or a bug? I cannot find any documented reference to this behavior.

Possibly, if length is not calculated during each iteration of Array.prototype.splice implementation

Using arrays in regular expressions (ruby)?

Does anyone know if there is a way to use an array in a regular expression? suppose i want to find out if "somefile.txt" contains one of an array's elements. Obviously the code below doesn't work, but is there something similar that does work?

array = [thing1 thing2 thing3]
file = File.open("somefile.txt")

file.each_do |line|
if /array/.match(line)
puts line
end

basically i've got a big list of words to search for, and i'd like to avoid something like this:

($somefile =~ /(thing1|thing2|thing3)/)

Thanks in advance!

How would I optimize this search function?

I'm having a tough time thinking through how I can make my search function more optimal. So I have a situation where I load an array of objects from a SQLITE database. Essentially one element in the array has multiple properties that can be accessed. Inside one element of the array, you have properties like time and rooms. I want to make the search flexible without having the user to filter what they are searching for. What I don't like about my implementation is that I filter through both time and room, then I for loop the filtered arrays and add them to a general "searchArrayToDisplay." I think the for loop is wasteful. I was wondering if there is a way I can append the filtered elements to the general "searchArrayToDisplay" without having to for loop? The method below is generally fast, but I think there's a better way that I'm just not seeing. Hopefully I am clear. Thanks for any help!

let filteredTime = searchArray.filter { (time: ProductModel) -> Bool in
               if time.customText1 == nil {
                 return false
                  } else {
                  return  time.customText1.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) != nil

                }   
            }
            var m : ProductModel
            for m in filteredTime {

                searchArrayToDisplay.append(m)

            }

            let filteredRooms = searchArray.filter { (session: ProductModel) -> Bool in

                if session.roomName == nil {
                    return false
                } else {
                return session.roomName.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) != nil
                }   
            }
            for pm in filteredRooms {
                searchArrayToDisplay.append(pm)
            }