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.