WeixiangYan/CodeTransOcean huggingface dataset:
Task: Dijkstra's algorithm







Python: from collections import namedtuple, deque
from pprint import pprint as pp
 
 
inf = float('inf')
Edge = namedtuple('Edge', ['start', 'end', 'cost'])
 
class Graph():
    def __init__(self, edges):
        self.edges = [Edge(*edge) for edge in edges]
        
        self.vertices = {e.start for e in self.edges} | {e.end for e in self.edges}
 
    def dijkstra(self, source, dest):
        assert source in self.vertices
        dist = {vertex: inf for vertex in self.vertices}
        previous = {vertex: None for vertex in self.vertices}
        dist[source] = 0
        q = self.vertices.copy()
        neighbours = {vertex: set() for vertex in self.vertices}
        for start, end, cost in self.edges:
            neighbours[start].add((end, cost))
            neighbours[end].add((start, cost))

        
 
        while q:
            
            u = min(q, key=lambda vertex: dist[vertex])
            q.remove(u)
            if dist[u] == inf or u == dest:
                break
            for v, cost in neighbours[u]:
                alt = dist[u] + cost
                if alt < dist[v]:                                  
                    dist[v] = alt
                    previous[v] = u
        
        s, u = deque(), dest
        while previous[u]:
            s.appendleft(u)
            u = previous[u]
        s.appendleft(u)
        return s
 
 
graph = Graph([("a", "b", 7),  ("a", "c", 9),  ("a", "f", 14), ("b", "c", 10),
               ("b", "d", 15), ("c", "d", 11), ("c", "f", 2),  ("d", "e", 6),
               ("e", "f", 9)])
pp(graph.dijkstra("a", "e"))





Ada: private with Ada.Containers.Ordered_Maps;
generic
   type t_Vertex is (<>);
package Dijkstra is
   
   type t_Graph is limited private;
   
   
   type t_Edge is record
      From, To : t_Vertex;
      Weight   : Positive;
   end record;
   type t_Edges is array (Integer range <>) of t_Edge;
   function Build (Edges : in t_Edges; Oriented : in Boolean := True) return t_Graph;
   
   
   type t_Path is array (Integer range <>) of t_Vertex;
   function Shortest_Path (Graph    : in out t_Graph;
                           From, To : in t_Vertex) return t_Path;
   function Distance      (Graph    : in out t_Graph;
                           From, To : in t_Vertex) return Natural;
   
private
   package Neighbor_Lists is new Ada.Containers.Ordered_Maps (Key_Type => t_Vertex, Element_Type => Positive);
   type t_Vertex_Data is record
      Neighbors : Neighbor_Lists.Map; 
      
      Previous  : t_Vertex;
      Distance  : Natural;
   end record;
   type t_Graph is array (t_Vertex) of t_Vertex_Data;
end Dijkstra;
Task: Dijkstra's algorithm







Python: from collections import namedtuple, deque
from pprint import pprint as pp
 
 
inf = float('inf')
Edge = namedtuple('Edge', ['start', 'end', 'cost'])
 
class Graph():
    def __init__(self, edges):
        self.edges = [Edge(*edge) for edge in edges]
        
        self.vertices = {e.start for e in self.edges} | {e.end for e in self.edges}
 
    def dijkstra(self, source, dest):
        assert source in self.vertices
        dist = {vertex: inf for vertex in self.vertices}
        previous = {vertex: None for vertex in self.vertices}
        dist[source] = 0
        q = self.vertices.copy()
        neighbours = {vertex: set() for vertex in self.vertices}
        for start, end, cost in self.edges:
            neighbours[start].add((end, cost))
            neighbours[end].add((start, cost))

        
 
        while q:
            
            u = min(q, key=lambda vertex: dist[vertex])
            q.remove(u)
            if dist[u] == inf or u == dest:
                break
            for v, cost in neighbours[u]:
                alt = dist[u] + cost
                if alt < dist[v]:                                  
                    dist[v] = alt
                    previous[v] = u
        
        s, u = deque(), dest
        while previous[u]:
            s.appendleft(u)
            u = previous[u]
        s.appendleft(u)
        return s
 
 
graph = Graph([("a", "b", 7),  ("a", "c", 9),  ("a", "f", 14), ("b", "c", 10),
               ("b", "d", 15), ("c", "d", 11), ("c", "f", 2),  ("d", "e", 6),
               ("e", "f", 9)])
pp(graph.dijkstra("a", "e"))
Task: Dijkstra's algorithm






PHP: <?php
function dijkstra($graph_array, $source, $target) {
    $vertices = array();
    $neighbours = array();
    foreach ($graph_array as $edge) {
        array_push($vertices, $edge[0], $edge[1]);
        $neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
        $neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
    }
    $vertices = array_unique($vertices);

    foreach ($vertices as $vertex) {
        $dist[$vertex] = INF;
        $previous[$vertex] = NULL;
    }

    $dist[$source] = 0;
    $Q = $vertices;
    while (count($Q) > 0) {

        $min = INF;
        foreach ($Q as $vertex){
            if ($dist[$vertex] < $min) {
                $min = $dist[$vertex];
                $u = $vertex;
            }
        }
        
        $Q = array_diff($Q, array($u));
        if ($dist[$u] == INF or $u == $target) {
            break;
        }

        if (isset($neighbours[$u])) {
            foreach ($neighbours[$u] as $arr) {
                $alt = $dist[$u] + $arr["cost"];
                if ($alt < $dist[$arr["end"]]) {
                    $dist[$arr["end"]] = $alt;
                    $previous[$arr["end"]] = $u;
                }
            }
        }
    }
    $path = array();
    $u = $target;
    while (isset($previous[$u])) {
        array_unshift($path, $u);
        $u = $previous[$u];
    }
    array_unshift($path, $u);
    return $path;
}

$graph_array = array(
                    array("a", "b", 7),
                    array("a", "c", 9),
                    array("a", "f", 14),
                    array("b", "c", 10),
                    array("b", "d", 15),
                    array("c", "d", 11),
                    array("c", "f", 2),
                    array("d", "e", 6),
                    array("e", "f", 9)
               );

$path = dijkstra($graph_array, "a", "e");

echo "path is: ".implode(", ", $path)."\n";
Task: Dijkstra's algorithm






PHP: <?php
function dijkstra($graph_array, $source, $target) {
    $vertices = array();
    $neighbours = array();
    foreach ($graph_array as $edge) {
        array_push($vertices, $edge[0], $edge[1]);
        $neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
        $neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
    }
    $vertices = array_unique($vertices);

    foreach ($vertices as $vertex) {
        $dist[$vertex] = INF;
        $previous[$vertex] = NULL;
    }

    $dist[$source] = 0;
    $Q = $vertices;
    while (count($Q) > 0) {

        $min = INF;
        foreach ($Q as $vertex){
            if ($dist[$vertex] < $min) {
                $min = $dist[$vertex];
                $u = $vertex;
            }
        }
        
        $Q = array_diff($Q, array($u));
        if ($dist[$u] == INF or $u == $target) {
            break;
        }

        if (isset($neighbours[$u])) {
            foreach ($neighbours[$u] as $arr) {
                $alt = $dist[$u] + $arr["cost"];
                if ($alt < $dist[$arr["end"]]) {
                    $dist[$arr["end"]] = $alt;
                    $previous[$arr["end"]] = $u;
                }
            }
        }
    }
    $path = array();
    $u = $target;
    while (isset($previous[$u])) {
        array_unshift($path, $u);
        $u = $previous[$u];
    }
    array_unshift($path, $u);
    return $path;
}

$graph_array = array(
                    array("a", "b", 7),
                    array("a", "c", 9),
                    array("a", "f", 14),
                    array("b", "c", 10),
                    array("b", "d", 15),
                    array("c", "d", 11),
                    array("c", "f", 2),
                    array("d", "e", 6),
                    array("e", "f", 9)
               );

$path = dijkstra($graph_array, "a", "e");

echo "path is: ".implode(", ", $path)."\n";






Ada: private with Ada.Containers.Ordered_Maps;
generic
   type t_Vertex is (<>);
package Dijkstra is
   
   type t_Graph is limited private;
   
   
   type t_Edge is record
      From, To : t_Vertex;
      Weight   : Positive;
   end record;
   type t_Edges is array (Integer range <>) of t_Edge;
   function Build (Edges : in t_Edges; Oriented : in Boolean := True) return t_Graph;
   
   
   type t_Path is array (Integer range <>) of t_Vertex;
   function Shortest_Path (Graph    : in out t_Graph;
                           From, To : in t_Vertex) return t_Path;
   function Distance      (Graph    : in out t_Graph;
                           From, To : in t_Vertex) return Natural;
   
private
   package Neighbor_Lists is new Ada.Containers.Ordered_Maps (Key_Type => t_Vertex, Element_Type => Positive);
   type t_Vertex_Data is record
      Neighbors : Neighbor_Lists.Map; 
      
      Previous  : t_Vertex;
      Distance  : Natural;
   end record;
   type t_Graph is array (t_Vertex) of t_Vertex_Data;
end Dijkstra;
Task: Create an HTML table



C#: using System;
using System.Text;

namespace prog
{
	class MainClass
	{		
		public static void Main (string[] args)
		{
			StringBuilder s = new StringBuilder();
			Random rnd = new Random();
			
			s.AppendLine("<table>");
			s.AppendLine("<thead align = \"right\">");
			s.Append("<tr><th></th>");
			for(int i=0; i<3; i++)
				s.Append("<td>" + "XYZ"[i] + "</td>");
			s.AppendLine("</tr>");
			s.AppendLine("</thead>");
			s.AppendLine("<tbody align = \"right\">");
			for( int i=0; i<3; i++ )
			{
				s.Append("<tr><td>"+i+"</td>");
				for( int j=0; j<3; j++ )
					s.Append("<td>"+rnd.Next(10000)+"</td>");				
				s.AppendLine("</tr>");
			}
			s.AppendLine("</tbody>");
			s.AppendLine("</table>");
			
			Console.WriteLine( s );
		}
	}
}









Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table

C: #include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	printf("<table style=\"text-align:center; border: 1px solid\"><th></th>"
		"<th>X</th><th>Y</th><th>Z</th>");
	for (i = 0; i < 4; i++) {
		printf("<tr><th>%d</th><td>%d</td><td>%d</td><td>%d</td></tr>", i,
			rand() % 10000, rand() % 10000, rand() % 10000);
	}
	printf("</table>");

	return 0;
}











Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table


C++: #include <fstream>
#include <boost/array.hpp>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>

void makeGap( int gap , std::string & text ) {
   for ( int i = 0 ; i < gap ; i++ ) 
      text.append( " " ) ;
}

int main( ) {
   boost::array<char , 3> chars = { 'X' , 'Y' , 'Z' } ;
   int headgap = 3 ;
   int bodygap = 3 ;
   int tablegap = 6 ;
   int rowgap = 9 ;
   std::string tabletext( "<html>\n" ) ;
   makeGap( headgap , tabletext ) ;
   tabletext += "<head></head>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "<body>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "<table>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<thead align=\"right\">\n" ;
   makeGap( tablegap, tabletext ) ;
   tabletext += "<tr><th></th>" ;
   for ( int i = 0 ; i < 3 ; i++ ) {
      tabletext += "<td>" ;
      tabletext += *(chars.begin( ) + i ) ;
      tabletext += "</td>" ;
   }
   tabletext += "</tr>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</thead>" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<tbody align=\"right\">\n" ;
   srand( time( 0 ) ) ;
   for ( int row = 0 ; row < 5 ; row++ ) {
      makeGap( rowgap , tabletext ) ;
      std::ostringstream oss ;
      tabletext += "<tr><td>" ;
      oss << row ;
      tabletext += oss.str( ) ;
      for ( int col = 0 ; col < 3 ; col++ ) {
	 oss.str( "" ) ;
	 int randnumber = rand( ) % 10000 ;
	 oss << randnumber ;
	 tabletext += "<td>" ;
	 tabletext.append( oss.str( ) ) ;
	 tabletext += "</td>" ;
      }
      tabletext += "</tr>\n" ;
   }
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</tbody>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "</table>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "</body>\n" ;
   tabletext += "</html>\n" ;
   std::ofstream htmltable( "testtable.html" , std::ios::out | std::ios::trunc ) ;
   htmltable << tabletext ;
   htmltable.close( ) ;
   return 0 ;
}










Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table




Go: package main

import (
    "fmt"
    "html/template"
    "os"
)

type row struct {
    X, Y, Z int
}

var tmpl = `<table>
    <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
{{range $ix, $row := .}}    <tr><td>{{$ix}}</td>
        <td>{{$row.X}}</td>
        <td>{{$row.Y}}</td>
        <td>{{$row.Z}}</td></tr>
{{end}}</table>
`

func main() {
    
    ct := template.Must(template.New("").Parse(tmpl))

    
    data := make([]row, 4)
    for r := range data {
        data[r] = row{r*3, r*3+1, r*3+2}
    }

    
    if err := ct.Execute(os.Stdout, data); err != nil {
        fmt.Println(err)
    }
}








Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table





Java: public class HTML {

	public static String array2HTML(Object[][] array){
		StringBuilder html = new StringBuilder(
				"<table>");
		for(Object elem:array[0]){
			html.append("<th>" + elem.toString() + "</th>");
		}
		for(int i = 1; i < array.length; i++){
			Object[] row = array[i];
			html.append("<tr>");
			for(Object elem:row){
				html.append("<td>" + elem.toString() + "</td>");
			}
			html.append("</tr>");
		}
		html.append("</table>");
		return html.toString();
	}
	
	public static void main(String[] args){
		Object[][] ints = {{"","X","Y","Z"},{1,1,2,3},{2,4,5,6},{3,7,8,9},{4,10,11,12}};
		System.out.println(array2HTML(ints));
	}
}







Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table







Python: import random

def rand9999():
    return random.randint(1000, 9999)

def tag(attr='', **kwargs):
    for tag, txt in kwargs.items():
        return '<{tag}{attr}>{txt}</{tag}>'.format(**locals())

if __name__ == '__main__':
    header = tag(tr=''.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
    rows = '\n'.join(tag(tr=tag(' style="font-weight: bold;"', td=i)
                                    + ''.join(tag(td=rand9999())
                                              for j in range(3)))
                     for i in range(1, 6))
    table = tag(table='\n' + header + rows + '\n')
    print(table)





Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table








VB: Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
	"\in.csv",1)
	

Set objOutHTML = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
	"\out.html",2,True)


objOutHTML.Write "<html><head></head><body>" & vbCrLf


objOutHTML.Write "<table border=1 cellpadding=10 cellspacing=0>" & vbCrLf


objOutHTML.Write "<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>" & vbCrLf


n = 1
Do Until objInFile.AtEndOfStream
	line = objInFile.ReadLine
	If Len(line) > 0 Then
		token = Split(line,",")
		objOutHTML.Write "<tr align=""right""><td>" & n & "</td>"
		For i = 0 To UBound(token)
			objOutHTML.Write "<td>" & token(i) & "</td>"
		Next
		objOutHTML.Write "</tr>" & vbCrLf
	End If
	n = n + 1
Loop


objOutHTML.Write "</table></body></html>"

objInFile.Close
objOutHTML.Close
Set objFSO = Nothing




Ada: with Ada.Strings.Unbounded;

generic
   type Item_Type is private;
   with function To_String(Item: Item_Type) return String is <>;
   with procedure Put(S: String) is <>;
   with procedure Put_Line(Line: String) is <>;
package HTML_Table is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   function Convert(S: String) return U_String renames
     Ada.Strings.Unbounded.To_Unbounded_String;

   type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
   type Header_Array is array(Positive range <>) of U_String;

   procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;
Task: Create an HTML table

C: #include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	printf("<table style=\"text-align:center; border: 1px solid\"><th></th>"
		"<th>X</th><th>Y</th><th>Z</th>");
	for (i = 0; i < 4; i++) {
		printf("<tr><th>%d</th><td>%d</td><td>%d</td><td>%d</td></tr>", i,
			rand() % 10000, rand() % 10000, rand() % 10000);
	}
	printf("</table>");

	return 0;
}
Task: Create an HTML table



C#: using System;
using System.Text;

namespace prog
{
	class MainClass
	{		
		public static void Main (string[] args)
		{
			StringBuilder s = new StringBuilder();
			Random rnd = new Random();
			
			s.AppendLine("<table>");
			s.AppendLine("<thead align = \"right\">");
			s.Append("<tr><th></th>");
			for(int i=0; i<3; i++)
				s.Append("<td>" + "XYZ"[i] + "</td>");
			s.AppendLine("</tr>");
			s.AppendLine("</thead>");
			s.AppendLine("<tbody align = \"right\">");
			for( int i=0; i<3; i++ )
			{
				s.Append("<tr><td>"+i+"</td>");
				for( int j=0; j<3; j++ )
					s.Append("<td>"+rnd.Next(10000)+"</td>");				
				s.AppendLine("</tr>");
			}
			s.AppendLine("</tbody>");
			s.AppendLine("</table>");
			
			Console.WriteLine( s );
		}
	}
}
Task: Create an HTML table


C++: #include <fstream>
#include <boost/array.hpp>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>

void makeGap( int gap , std::string & text ) {
   for ( int i = 0 ; i < gap ; i++ ) 
      text.append( " " ) ;
}

int main( ) {
   boost::array<char , 3> chars = { 'X' , 'Y' , 'Z' } ;
   int headgap = 3 ;
   int bodygap = 3 ;
   int tablegap = 6 ;
   int rowgap = 9 ;
   std::string tabletext( "<html>\n" ) ;
   makeGap( headgap , tabletext ) ;
   tabletext += "<head></head>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "<body>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "<table>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<thead align=\"right\">\n" ;
   makeGap( tablegap, tabletext ) ;
   tabletext += "<tr><th></th>" ;
   for ( int i = 0 ; i < 3 ; i++ ) {
      tabletext += "<td>" ;
      tabletext += *(chars.begin( ) + i ) ;
      tabletext += "</td>" ;
   }
   tabletext += "</tr>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</thead>" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<tbody align=\"right\">\n" ;
   srand( time( 0 ) ) ;
   for ( int row = 0 ; row < 5 ; row++ ) {
      makeGap( rowgap , tabletext ) ;
      std::ostringstream oss ;
      tabletext += "<tr><td>" ;
      oss << row ;
      tabletext += oss.str( ) ;
      for ( int col = 0 ; col < 3 ; col++ ) {
	 oss.str( "" ) ;
	 int randnumber = rand( ) % 10000 ;
	 oss << randnumber ;
	 tabletext += "<td>" ;
	 tabletext.append( oss.str( ) ) ;
	 tabletext += "</td>" ;
      }
      tabletext += "</tr>\n" ;
   }
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</tbody>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "</table>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "</body>\n" ;
   tabletext += "</html>\n" ;
   std::ofstream htmltable( "testtable.html" , std::ios::out | std::ios::trunc ) ;
   htmltable << tabletext ;
   htmltable.close( ) ;
   return 0 ;
}
Task: Create an HTML table





Java: public class HTML {

	public static String array2HTML(Object[][] array){
		StringBuilder html = new StringBuilder(
				"<table>");
		for(Object elem:array[0]){
			html.append("<th>" + elem.toString() + "</th>");
		}
		for(int i = 1; i < array.length; i++){
			Object[] row = array[i];
			html.append("<tr>");
			for(Object elem:row){
				html.append("<td>" + elem.toString() + "</td>");
			}
			html.append("</tr>");
		}
		html.append("</table>");
		return html.toString();
	}
	
	public static void main(String[] args){
		Object[][] ints = {{"","X","Y","Z"},{1,1,2,3},{2,4,5,6},{3,7,8,9},{4,10,11,12}};
		System.out.println(array2HTML(ints));
	}
}
Task: Create an HTML table







Python: import random

def rand9999():
    return random.randint(1000, 9999)

def tag(attr='', **kwargs):
    for tag, txt in kwargs.items():
        return '<{tag}{attr}>{txt}</{tag}>'.format(**locals())

if __name__ == '__main__':
    header = tag(tr=''.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
    rows = '\n'.join(tag(tr=tag(' style="font-weight: bold;"', td=i)
                                    + ''.join(tag(td=rand9999())
                                              for j in range(3)))
                     for i in range(1, 6))
    table = tag(table='\n' + header + rows + '\n')
    print(table)