Wednesday 19 March 2014

Pivot xml to another xml  
/redefine the tree structure of xml


Pivot.XSL
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:key name="muench" match="/bar/part " use="@id"/>
<xsl:template match="/">
<bar>
<xsl:for-each select="/structure/r[generate-id() = generate-id(key('muench',@id)[1])]">
<xsl:call-template name="pivot">
<xsl:with-param name="key" select="@id"/>
</xsl:call-template>
</xsl:for-each>
</bar>
</xsl:template>
<xsl:template name="pivot">
<xsl:param name="key"/>
<series key="{$key}">
<xsl:for-each select="/bar/part[@id=$key]">
<xsl:copy-of select="."/>
</xsl:for-each>
</series>
</xsl:template>
</xsl:stylesheet>

Input XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<bar>
    <part id='1' count='1' bad='1'/>
    <part id='2' count='2' bad='2'/>
    <part id='3' count='3' bad='3'/>
    <part id='4' count='4' bad='4'/>
    <part id='5' count='5' bad='5'/>
    <part id='6' count='6' bad='6'/>
    <part id='7' count='10' bad='7'/>
    <part id='8' count='20' bad='8'/>
    <part id='9' count='10' bad='9'/>
    <part id='10' count='20' bad='10'/>
  </bar>
  
Output XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<bar>
    <part id='1' count='1' bad='1'/>
 </bar>
<bar> 
  <part id='2' count='2' bad='2'/>
 </bar>
<bar>
    <part id='3' count='3' bad='3'/>
 </bar>
<bar>
<part id='4' count='4' bad='4'/>
 </bar>
<bar>
    <part id='5' count='5' bad='5'/>
 </bar>
<bar>
    <part id='6' count='6' bad='6'/>
 </bar>
<bar>
    <part id='7' count='10' bad='7'/>
 </bar>
<bar>
    <part id='8' count='20' bad='8'/>
 </bar>
<bar>
    <part id='9' count='10' bad='9'/>
 </bar>
<bar>
    <part id='10' count='20' bad='10'/>
 </bar>

C# code to send HTML page with inline images in mail body as inline image


C# page

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Xml;
using System.IO;


class Program
{
    static void Main(string[] args)
    {
       MailMessage mail = new MailMessage();
        //set the addresses
        mail.From = new MailAddress("yourusername@gmail.com");
        mail.To.Add("yourusername@gmail.com");

        //set the content
        mail.Subject = "Sucess Fully Sent the HTML and COntent of mail";

        //first we create the Plain Text part
        string plainText = "Non HTML Plane Text Message for Non HTML enable mode";
        AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainText, null, "text/plain");
        XmlTextReader reader = new XmlTextReader(@"C:\..\Windows\your path\HTMLPage.htm");
        string[] address = new string[30];
        string finalHtml = "";
        var i = -1;
        while (reader.Read())
        {
                if(reader.NodeType == XmlNodeType.Element)
                { // The node is an element.
                            if (reader.AttributeCount <= 1)
                            {
                                if (reader.Name == "img")
                                {
                                    finalHtml += "<" + reader.Name;
                                    while (reader.MoveToNextAttribute())
                                    {
                                        if (reader.Name == "src")
                                        {
                                            i++;
                                            address[i] = reader.Value;
                                            address[i] = address[i].Remove(0, 8);
                                            finalHtml += " " + reader.Name + "=" + "cid:chartlogo" + i.ToString();
                                        }
                                        else
                                        {
                                            finalHtml += " " + reader.Name + "='" + reader.Value + "'";
                                        }
                                    }
                                    finalHtml += ">";
                                }
                                else
                                {
                                    finalHtml += "<" + reader.Name;
                                    while (reader.MoveToNextAttribute())
                                    {
                                        finalHtml += " " + reader.Name + "='" + reader.Value + "'";
                                    }
                                    finalHtml += ">";
                                }
                            }
                     
                }else if( reader.NodeType==   XmlNodeType.Text)
            { //Display the text in each element.
                     finalHtml +=reader.Value;
                }else if(reader.NodeType== XmlNodeType.EndElement){
                    //Display the end of the element.
                     finalHtml +="</" + reader.Name;
                     finalHtml += ">";
            }
           
        }

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(finalHtml, null, "text/html");  
        LinkedResource[] logo = new LinkedResource[i+1];  
        for (int j = 0; j <= i; j++)
        {
           logo[j] = new LinkedResource(address[j]);
           logo[j].ContentId = "chartlogo" + j;
           htmlView.LinkedResources.Add(logo[j]);
        }
        mail.AlternateViews.Add(plainView);
        mail.AlternateViews.Add(htmlView);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new NetworkCredential(
            "yourusername@gmail.com", "your password");
        smtp.EnableSsl = true;
        Console.WriteLine();
        smtp.Send(mail);
    }
}




HTMLPage.html


<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>HTML Page to Send Mail </title></head>
<body>
    <table border= "0" >
        <tr>
        <td>Chart1</td>
        <td>Chart2</td>
        <td>Chart3</td>
        </tr>
 <tr>
<td><img src = "file:///C:/Users/Windows/Desktop/ChartSnap/1.jpg"  /></td>
<td><img src = "file:///C:/Users/Windows/Desktop/ChartSnap/2.jpg"  /></td>
<td><img src = "file:///C:/Users/Windows/Desktop/ChartSnap/3.jpg" /></td>
</tr><tr>
<td><img src = "file:///C:/Users/Windows/Desktop/ChartSnap/4.jpg" /></td>
</tr>
</table>

</body>

</html>

Sunday 9 February 2014

Problem 1.1.2 Other than speed, what other measures of efficiency might one use in a real-world setting

Scalability, Computation, defects, human error, computation, and many other factors like tear-wear.
strength,strain , stress etc

Problem 1.1-1 Give a real-world example in which one of the following computational problems appears: sorting, determining the best order for multiplying matrices, or finding the convex hull.

Problem 1.1-1, Problem 1.1-2, Problem 1.1-3, Problem 1.1-4

Sorting: almost everything uses sorting. If a program is not using sorting then it just adds two number and returns 0; 

Matrix multiplication
  uhh, here's the hurt. I don't know off the top of  my head. Let me walk through the thinking. 

Matrices contain tabular data; data that is usually (who am I kidding?), always numbers. Numbers get multiplicated (yeah, I know) by a strange way of multiplication. What can numbers in sets represent? Points, 3D points, financial data ($$),  quantities of consumption (wild guess). And where can matrix multiplication be implemented? Graphic rendering, video rendering, stock exchange? 

Finding convex hull:

what the f is a convex hull?

In mathematics, the convex hull or convex envelope of a set X of points in the Euclidean plane or Euclidean space is the smallestconvex set that contains X. For instance, when X is a bounded subset of the plane, the convex hull may be visualized as the shape formed by a rubber band stretched around X

Problem 1.1.4 .How are the shortest-path and traveling-salesman problems given above similar? How are they different?

Problem 1.1.1, Problem 1.1.2, and Problem 1.1.3 
Here's the third installment of the questionnaire I came across in this wonderful book about algorithms, introduction to algorithms, 2nd edition, MIT press.


How are the shortest-path and traveling-salesman problems given above similar? How are
they different?
What is shortest path problem?
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
And what is travelling salesman problem?
The travelling salesman problem (TSP) is an NP-hard problem in combinatorial optimization studied in operations research and theoretical computer science. Given a list of cities and their pairwise distances, the task is to find the shortest possible route that visits each city exactly once and returns to the origin city. It is a special case of the travelling purchaser problem.

 Before going to wiki I guessed this:

Shortest path problem is about finding shortest path through bunch of the nodes or points or towns.

Travelling salesman problem is about finding shortest path and come back home like boomerang. 

Well, I don't know much about it. What ever. Will read upon it.

Monday 29 July 2013

C Program for printing Indian Map using " ! " symbol

C Program for  printing  Indian Map using " ! " symbol



Code in "C"

#include<stdio.h>
void main()
{
int a,b,c;
printf("Map of India");
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )

{
for(; a-- > 64 ; )
{
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
}
}

getch();
}


Output:










Tuesday 23 July 2013

Sorting

A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:
  • The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order)
  • The output is a permutation (reordering) of the input.
New sorting algorithms are still being invented (for example, library sort). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and upper and lower bounds.


Sorting algorithms are often classified by:
  • Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n). For typical serial sorting algorithms good behavior is O(n log n), with parallel sort in O(log^2 n), and bad behavior is O(n^2). Ideal behavior for a serial sort is O(n), but this is not possible in the average case, optimal parallel sorting is O(log n). Comparison-based sorting algorithms, which evaluate the elements of the list via an abstract key comparison operation, need at least O(n log n) comparisons for most inputs.
  • Computational complexity of swaps (for "in place" algorithms).
  • Memory usage (and use of other computer resources). In particular, some sorting algorithms are "in place". Strictly, an in place sort needs only O(1) memory beyond the items being sorted; sometimes O(log(n)) additional memory is considered "in place".
  • Recursion. Some algorithms are either recursive or non-recursive, while others may be both (e.g., merge sort).
  • Stability: stable sorting algorithms maintain the relative order of records with equal keys (i.e., values).
  • Whether or not they are a comparison sort. A comparison sort examines the data only by comparing two elements with a comparison operator.
  • General method: insertion, exchange, selection, merging, etc. Exchange sorts include bubble sort and quicksort. Selection sorts include shaker sort and heapsort. Also whether the algorithm is serial or parallel. The remainder of this discussion almost exclusively concentrates upon serial algorithms and assumes serial operation.
  • Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive