Tuesday, August 23, 2011

C# code to Arrange array values Columnwise

Question :
Format any given string array "columnwise" into 3 compact html columns 
(notice the example output is ordered by column, not by row) 
last row is only row that can be short (i.e. have one or two empty cells, see example below)
sample test case (your solution should pass any test case)  
string[] testArray = { "item 1", "item 2", "item 3", "item 4", "item 5", "item 6", "item 7", "item 8", "item 9", "item 10", "item 11" };
write your code here to create your html output from testArray[] as a string
Response.Write(@"<table>
                            <tr><td>item 1</td><td>item 5<td>item 9</td><td></td></tr>
                            <tr><td>item 2</td><td>item 6<td>item 10</td><td></td></tr>
                            <tr><td>item 3</td><td>item 7<td>item 11</td><td></td></tr>
                            <tr><td>item 4</td><td>item 8<td></td><td></td></tr>
                            </table>"); 
Answer
using System;
using System.Collections.Generic;
using System.Text;
/*
 * step 1 : Store array in arraylist
 * step 2 : Find out no of blank cells in table and assign blank value for those cells

 * step 3 : Shuffle the array for matrix
 * step 4 : Display in required formate
 * */
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
           
 string[] testArray = { "item 1", "item 2", "item 3", "item 4", "item 
5", "item 6", "item 7", "item 8", "item 9", "item 10", "item 11" };


            int noOfColumns = 3;
            StringBuilder sb = new StringBuilder();
            
            System.Collections.ArrayList tlist = new System.Collections.ArrayList()
;
            // Calculate no blankcells
            int blankArray = noOfColumns - (testArray.Length % noOfColumns);
           
            //populate the arrayvalues in ArrayList with blankcells
            for (int x = 0; x < testArray.Length + blankArray; x++)
            {
                if (testArray.Length > x)
                {
                    tlist.Add(testArray[x]);
                }
                else
                {
                    tlist.Add("");
                }
            }

            //Shuffle the array for matrix
            System.Collections.ArrayList list = new System.Collections.ArrayList()
;
            for (int i = 0; i < tlist.Count / 3; i++)
            {
                list.Add(tlist[i].ToString());
                list.Add(tlist[i + noOfColumns + 1].ToString());
                list.Add(tlist[i + (noOfColumns + 1) * 2].ToString());
            }
            // Add HTML tags and display the array
            sb.Append("<table>");
            for (int j = 0; j < 12; j = j + 3)
            {              
                sb.Append("<tr>");
                sb.Append("<td>" + list[j].ToString() + "</td><td>" + list[j + 1].ToString() + "</td><td>" + list[j + 2].ToString() + "</td>");
                sb.Append("</tr>");
               
            }
            sb.Append("</table>");
            Console.WriteLine(sb.ToString(
));
            Console.ReadLine();
        }

    }
}

No comments:

Post a Comment