Saturday, 3 August 2013

WorkFlow - Sample Application to Create a workflow and print a value in console screen














        Windows Workflow foundation which provides a model to define and execute processes using a set 
of building blocks called activites. WF provides a Designer, That allows to drag and drop activites from the toolbox on to the design surface to create a custom workflow template.

Let we create a sample application , Start visual studio and create a new project in WF and select WorkFlow Console Application.





Giving the project name and select OK.

Following are the Activities present in the ToolBox of WF.






To execute the workflow we have to create a instance of WorkflowInstance 


WorkflowInvoker.Invoke(new Workflow1());

Now we see the steps to create a Workflow

Step 1 :

  • Add the Sequence activity from the toolbox
  • Add a new variable as name in string type in sequence scope which is present below the designer
  • Add a Assign activity from the designer inside the Sequence scope and place the name variable in the left side and assign the value in right side.
  • Attach a break point in that activity , by right click on the activity and select break point.




Step 2 :

  • Add a another activity Delay inside the WF of sequence activity and after the assign activity 
  • Select the properties of Delay activity
  • Give the Delay of 10 seconds.



  • Add a WriteLine activity after the delay activity 
  • give the variable name inside that

Now Press F5 and see the program will hit on the break point . now you can debug and see the sequence flow by press F11. you can see the output of workflow




I Hope from this article you will learn the how to create a sample WorkFlow Application. 





Friday, 2 August 2013

ASP.NET MVC - Basics

MVC  is the abbreviation of Model View Controller, MVC is the Framework for building web applications

Model - Business Layer (Database)
View   - UI Layer (front end)
Controller - Action takes on Controller event


Let we see how the MVC works

Steps to create MVC application 

  • Open Visual studio 2010
  • Select ASP.NET MVC 2 Application.
  • Give the Project name and select OK.



In Solution Explorer You can see the Various Folders , Now we can see few of them which is important for this example. Controll

Controllers Folder : Hold the controller that means action to perform
Models Folder : Hold the Model or Database information with data
Views Folder : Holds the View of each and every Controller.

Now press F5 and see , The URL deosn't end with any extension but it shows the Home page Related to
HomeController -> action of Index, Because under the home view folder the action of Index page is set as Default page to launch at startup.


http://localhost:1429/  

Actual URL of above url is 

http://localhost:1429/Home

OR

http://localhost:1429/Home/Index

To Call a About Page or About Action inside a HomeController Just Place the Prefix name of the Controller Like "Home" For "HomeController" Along with concat of forward slash then add the name of the Action to call the page Like "Home/About".

http://localhost:1429/Home/About.

Now Let we see Programming , Add the Following code in Controller class "HomeController.cs"

Let we see Create a New View in Existing Controller
Step 1 :
Assign the value of name in ViewData 

public ActionResult Test()
{
   ViewData["name"] = "Rajesh";
   return View();

}


Step 2 : Add the Test.aspx view page in Home Folder of Views .

  •   Right Click the Home Folder Under the Views
  •   Select the Add Option
  •   Select the View,Then click ok.

Step 3 :
Add the following code in test.aspx, This will get the value of name from the viewData which is assigned in the HomeController class of Test Action.

   <h2><%: ViewData["name"] %></h2>


 Step 4 :
     Press the F5, Go to The URL "http://localhost:1429/Home/test".


Let we see Create a New View in New Controller

Instead of Step 1 We can add a new controller class  in controller and place the code in new controller. Instead of placing in existing controller.

Place the following steps instead of step 1 for add a new controller.


 Step 1 :
  •   Right Click the Controller folder 
  •   Select the Add Option
  •   Select the Controller to add the New controller, give any name post fix with controller "ValidateController" and click ok.

Now place the below code in ValidateController

public ActionResult Test()
{
   ViewData["name"] = "Rajesh";
   return View();

}



 Step 2 :


  • Add the Folder nameValidate under the View Folder.
  • Add the view name Test.aspx and Index.aspx under the Validate Folder.
  • Paste the following code in that Text.aspx
   <h2><%: ViewData["name"%></h2>

Press F5 , Go to URL http://localhost:1429/Validate/Test . You will get the Result.




Output:



From this article I hope that you can learn how to add the controller and view and how to transfer the data between them . 

Thursday, 1 August 2013

Sticky Sticks Hexagon Game in C#

Problem Statement

Rajesh is playing a very interesting mathematical game. He has collection of sticks of length 1,2,3,....,N. He is making hexagon out of his collection by using 6 sticks for each hexagon. He considers a hexagon "good" if the biggest stick has length at least L and lengths of all the other sticks are not more than X.A "good" hexagon does not have sticks of the same length more than K times. 
How many ways he can make a "Good" hexagon?

Input/Output Specs

Input Specification:Input contains four integers-
N( 2 <= N <= 10^9)
L ( 2 <= L <= N
 and N-L<=100)
X( 1<=X< L )
K ( 1 <= K <= 5)


Output Specification:

Output the number of different ways to make a "Good" hexagon% 1000000007

Examples

Example:1 when N=8, L=7, X=5, K=3: { 1,2,2,5,5,7 } is a good hexagon but {1,2,2,2,2,7}, { 1,2,3,4,5,6},{1,2,3,4,6,7} are not.Two hexagons are considered different if their side length sets are different.

For example
 {1,2,3,4,5,6} , {1,1,1,2,2,3} and {1,1,2,2,2,3} are all different hexagons. But {1,2,3,4,5,6} and { 2,4,6,1,3,5} are not different.

Example:2 When N=10, L=8, X=6, K=2
Output:       Total hexagons= 374

Note: Please return -1 for invalid cases.

One point we need to understand, in irregular hexagon sum of all other sides must be greater than the bigger side stick length then only it is consider as Good Hexagon

using System.IO;
using System;
using LL = System.Int64;
using System.Collections.Generic;

public class CandidateCode
{

    static LL MOD = 1000000007;
    static LL N, K, L, X;



    static LL gcd(LL a, LL b)
    {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    static LL[] arr = new LL[10];


    static LL nck(LL n, LL k)
    {
        LL i, j, d;
        LL ret = 1;
        if (k > n) return 0;
        for (i = 0; i < k; i++) arr[i] = n - i;
        for (i = 2; i <= k; i++)
            for (d = i, j = 0; j < k; j++)
            {
                LL g = gcd(arr[j], d);
                arr[j] /= g; d /= g;
            }
        for (i = 0; i < k; i++) ret = (((LL)ret * arr[i]) % MOD);
        return ret;
    }



    static LL calcAll(LL n, LL x, LL k)
    {
        LL ret = 0;
        LL i;
        if (x == 0) return nck(n, k);
        for (i = 1; i <= x && i <= K; i++)
            ret = ((ret + calcAll(n, x - i, k + 1)) % MOD);

        return ret;
    }



    static void gen(LL pos, LL carry, LL pattern, LL c, LL sum, LL val, LL idx)
    {
        LL i;

        if (pos == 5)
        {
            if ((sum / 10) != carry) return;
            LL x = carry * total + pattern, y = c * total + ids[val], d = sum % 10;
            cnt1[(vals[0] % 10), d, x, y, 0]++;
            if ((vals[4] % 10) == 0) cnt1[(vals[0] % 10), d, x, y, 1]++;
            return;
        }

        for (i = 0; i <= 9; i++)
        {
            if ((sum + i) / 10 > carry) break;
            vals[pos] = pat[pos] * 10 + i;
            if ((pos != 0) && (vals[pos] > vals[pos - 1]))
                break;
            LL nidx = ((pos == 0) || (vals[pos] == vals[pos - 1])) ? idx : idx - 1;
            gen(pos + 1, carry, pattern, c, sum + i, (val * 10 + nidx), nidx);
        }
    }


    static List<Int64> now = new List<Int64>();
    static LL[][] states = new LL[20][];
    static LL[] ids = new LL[100000];
    static LL[] valid = new LL[80];
    static LL total;
    static LL[, ,] next = new LL[2, 85, 85];
    static LL[,] size = new LL[2, 85];

    static void genStates(LL pos, LL val, LL last) // generates all possible encodings
    {
        if (pos == 5)
        {
            ids[val] = total;

            states[total++] = now.ToArray();

            return;
        }
        now[(int)pos] = last;
        genStates(pos + 1, val * 10 + last, last);
        if (pos != 0) { now[(int)pos] = last - 1; genStates(pos + 1, val * 10 + last - 1, last - 1); }
    }


    static LL[, , , ,] cnt1 = new LL[10, 10, 85, 85, 2];
    static LL[, , , ,] cnt2 = new LL[10, 10, 85, 85, 2];
    static LL[, , , ,] cnt3 = new LL[10, 10, 85, 85, 2];
    static LL[, , , ,] cnt4 = new LL[10, 10, 85, 85, 2];
    static LL[] vals = new LL[10];
    static LL[] pat = new LL[10];


    static LL preCalc(LL i, LL j, LL k, LL l, LL x)
    {
        LL cc = cnt1[k, l, i, j, x];
        LL cd = ((k != 0) && (l != 0)) ? cnt4[k - 1, l - 1, i, j, x] : 0;
        LL ca = (l != 0) ? cnt2[k, l - 1, i, j, x] : 0;
        LL cb = (k != 0) ? cnt3[k - 1, l, i, j, x] : 0;

        cnt2[k, l, i, j, x] = ca + cc;
        cnt3[k, l, i, j, x] = cb + cc;
        cnt4[k, l, i, j, x] = ca + cb + cc + cd;

        return ca + cb + cc + cd;
    }

    static void make(LL carry, LL pattern) // compute the edges
    {
        LL i, j, k, l, x = carry * total + pattern;

        for (i = 0; i < 5; i++)
        {


            pat[i] = states[pattern][i];
        }

        for (i = 0; i < 5; i++)
            gen(0, carry, pattern, i, i, 0, 9);

        for (i = x, j = 0; j < 80; j++)
        {
            LL ok1 = 0, ok2 = 0;
            for (k = 0; k <= 9; k++)
                for (l = 0; l <= 9; l++)
                {
                    if (preCalc(i, j, k, l, 0) != 0) ok1 = 1;
                    if (preCalc(i, j, k, l, 1) != 0) ok2 = 1;
                }
            if (ok1 != 0) next[0, x, (size[0, x]++)] = j;
            if (ok2 != 0) next[1, x, (size[1, x]++)] = j;
        }
    }


    static LL[, , ,] memo = new LL[12, 2, 2, 85];
    static LL[, , ,] seen = new LL[12, 2, 2, 85];
    static LL length, cnt;
    static LL[] MM = new LL[15];
    static LL[] XX = new LL[15];

    static LL solve(LL pos, LL prefixX, LL prefixM, LL x, LL z)
    {
        LL i, ret = 0;

        if (pos == length) return valid[x];
        if (seen[pos, prefixX, prefixM, x] == cnt) return memo[pos, prefixX, prefixM, x];

        LL a = XX[pos], b = MM[pos];

        for (i = 0; i < size[z, x]; i++)
        {
            LL y = next[z, x, i];

            if ((prefixX != 0) && (prefixM != 0))
            {
                if (cnt1[a, b, x, y, z] != 0)
                    ret = ((ret + (LL)cnt1[a, b, x, y, z] * solve(pos + 1, 1, 1, y, z)) % MOD);
                if ((b != 0) && (cnt2[a, b - 1, x, y, z] != 0))
                    ret = ((ret + (LL)cnt2[a, b - 1, x, y, z] * solve(pos + 1, 1, 0, y, z)) % MOD);
                if ((a != 0) && (cnt3[a - 1, b, x, y, z] != 0))
                    ret = ((ret + (LL)cnt3[a - 1, b, x, y, z] * solve(pos + 1, 0, 1, y, z)) % MOD);
                if ((a != 0) && (b != 0) && (cnt4[a - 1, b - 1, x, y, z] != 0))
                    ret = ((ret + (LL)cnt4[a - 1, b - 1, x, y, z] * solve(pos + 1, 0, 0, y, z)) % MOD);
            }

            if ((prefixX != 0) && (prefixM == 0))
            {
                if (cnt2[a, 9, x, y, z] != 0)
                    ret = ((ret + (LL)cnt2[a, 9, x, y, z] * solve(pos + 1, 1, 0, y, z)) % MOD);
                if ((a != 0) && (cnt4[a - 1, 9, x, y, z] != 0))
                    ret = ((ret + (LL)cnt4[a - 1, 9, x, y, z] * solve(pos + 1, 0, 0, y, z)) % MOD);
            }

            if ((prefixX == 0) && (prefixM != 0))
            {
                if (cnt3[9, b, x, y, z] != 0)
                    ret = ((ret + (LL)cnt3[9, b, x, y, z] * solve(pos + 1, 0, 1, y, z)) % MOD);
                if ((b != 0) && cnt4[9, b - 1, x, y, z] != 0)
                    ret = ((ret + (LL)cnt4[9, b - 1, x, y, z] * solve(pos + 1, 0, 0, y, z)) % MOD);
            }

            if ((prefixX == 0) && (prefixM == 0))
            {
                if (cnt4[9, 9, x, y, z] != 0)
                    ret = ((ret + (LL)cnt4[9, 9, x, y, z] * solve(pos + 1, 0, 0, y, z)) % MOD);
            }
        }

        seen[pos, prefixX, prefixM, x] = cnt;
        return memo[pos, prefixX, prefixM, x] = ret;
    }




    static LL calc(LL X, LL M, LL z)
    {
        LL ret = 0;

        ++cnt;
        length = 0;

        while ((X != 0) || (M != 0))
        {
            XX[length] = X % 10;
            MM[length] = M % 10;
            X /= 10; M /= 10;
            length++;
        }

        if (length > 1)
        {
            for (LL i = 0; i < length / 2; i++)
            {
                LL temp = XX[i];
                XX[i] = XX[(length - 1) - i];
                XX[(length - 1) - i] = temp;

            }

            for (LL i = 0; i < length / 2; i++)
            {
                LL temp = MM[i];
                MM[i] = MM[(length - 1) - i];
                MM[(length - 1) - i] = temp;

            }   
        }

        ret = (solve(0, 1, 1, 0, z));
        return ret;
    }



    static LL calc()
    {
        LL ret = 0;
        LL i;
        LL p = ((calcAll(X, 5, 0)) % MOD);
        for (i = L; i <= N; i++)
        {
            LL q = ((calc(X, i, 0) - calc(X, i, 1)) % MOD);
            ret = (ret + (p - q) % MOD) % MOD;
        }
        if (ret < 0) ret += MOD;
        return ret;
    }





    public static int goodHexa(Int64 input1, Int64 input2, Int64 input3, Int64 input4)
    {
        N = input1; L = input2; X = input3; K = input4;
        LL ans;


        if (N >= 2 && N <= (Math.Pow(10, 9)) && (N - L <= 100) && L >= 2 && L <= N && X >= 1 && X < L && K >= 1 && K <= 5)
        {  
            now.Add(0); now.Add(0); now.Add(0); now.Add(0); now.Add(0);
            LL i, j, a;
            genStates(0, 0, 9);
            for (i = 0; i < 5; i++)
                for (j = 0; j < 16; j++)
                    make(i, j);



            for (i = 0; i < total; i++)
            {
                LL cnt = 0, ok = 1;
                for (j = 0; j < 5; j++)
                {

                    if (j == 0)
                    {
                        a = 0;
                    }
                    else
                        a = states[i][j - 1];

                    if ((j == 0) || (states[i][j] != a))
                        cnt = 1;
                    else
                        cnt++;

                    if (cnt > K) ok = 0;
                }
                if (ok != 0)
                    valid[i] = 1;
            }

            ans = calc();
        }
        else
        {
            ans = -1;

        }

        return (int)ans; 
    }

}





Monday, 29 July 2013

Create a Bootable CD from Floppy image or from files

In this article we are going to see how to create a our own bootable CD. First thing we need following items
  • mkisofs
  • Source Floppy image to write to CD

For Windows user you can download it here ftp://ftp.berlios.de/pub/cdrecord/alpha/win32/ 

  • Go to the location where floppy image is located
         cd  ~/myos

  • Create a directory to store the contents of the CD
         mkdir  cddir

  • Copy the contents that are need to be added in the CD
         cp bootfiles/* cddir

  • Copy the floppy image inside the directory         
         cp  floppy.img cddir

          Here  
               -o        indicates output file
               -v        indicates label of the CD
               -b        floppy image
              cddir   where all files should be taken from.

  • Create a CD image from Floppy image
         mkisofs -o  osboot.iso -v myos -b floppy.img cddir

our_kernel  is the name of your kernel 

Command line to create a Bootable cd with out floppy image
   mkdir  -p  $ISO_DIR/boot/grub
   cp  $GRUB_BIN/stage2_eltorito $ISO_DIR/boot/grub
   cp menu.lst $ISO_DIR/boot/grub
   cp  our_kernel $ISO_DIR

         mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot-boot-load-size 4 -boot-info-table -o          bootcd.iso  $ISO_DIR

I Hope from this article you can learn how to create a bootable CD , by having the source as floppy image or files.

Sunday, 28 July 2013

Add Row Dynamically in Table Using JQuery

Below code is used to add the row dynamically to table using JQuery.

How to find the row in tbody inside the Table using Jquery : 
$(‘#Search_table’).find("tbody").find("tr");

<table id="Search_table">
<thead>
<tr><td>USER ID</td>
<td>USER NAME</td>
<td>CURRENT POINT</td>
<td>ADD POINT</td>
<td>ADD DESCRIPTION</td>
<td>ACTION</td>
</tr>
 </thead>
<tbody> </tbody>
</table>

<script>
$(document).ready(function ()
{      
var table_tr="<tr><td>RajeshG</td><td>Rajesh
</td><td>100</td><td>100</td><td>
point added </td><td>ACTION </td></tr>";

$('#Search_table').find("tbody").append(table_tr);
//to append the tr in the tbody tag                  
});
</script>