String parsing based on semicolan, dolon and commas

Sourabh Agrawal 41 Reputation points
2023-09-12T14:43:39.7633333+00:00

Hi,

I am trying to solve a hacker rank problem below, If someone can help. I would really appreciate it.

Problem: parse the below string based on semicolon, comma, and colon, the output should be an HTML table (Matrix)

String: "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20"

Desired output : <table border="1"><tr><th>Row Label</th><th>L10</th><th>L20</th></tr><tr><td>5.0</td><td>15</td><td>18</td></tr><tr><td>4.0</td><td>25</td><td>42</td></tr><tr><td>3.0</td><td>10</td><td>12</td></tr></table>

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,822 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Thomas Wycichowski 4lhome.org 965 Reputation points
    2023-09-12T20:03:21.2566667+00:00
    public record LData(string L, string[] Value);
    
    public static void Parse1() {     
    	string text = "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20";      
    	List<LData> datas = new();     
    	StringBuilder sb = new();     
    	List<string> list = new();     
    	bool readL = false;      
    
    	void AddDatas()     
    	{         
    		readL = false;         
    		datas.Add(new(sb.ToString(), list.ToArray()));         
    		sb.Clear();         
    		list.Clear();     
    	}      
    
    	foreach (var item in text)     
    	{         
    		if (readL)         
    		{             
    			if (item != ';')                 
    				sb.Append(item);             
    		else                 
    		AddDatas();         
    		}         
    		else         
    		{             
    			switch (item)             
    			{                 
    				case ',':                     
    				list.Add(sb.ToString());                     
    				sb.Clear();                     
    				break;                 
    				case ':':                     
    					sb.Append(item);                     
    					list.Add(sb.ToString());                     
    					sb.Clear();                     
    					readL = true;                     
    					break;                 
    				default:                     
    				sb.Append(item);                     
    				break;             
    			}         
    		}     
    	}      
    	AddDatas(); 
    }
    

    or

    public static void Parse2() {     
    	string text = "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20";     
    	List<LData> datas2 = new();     
    	string[] records = text.Split(';');     
    	//5.0,15,4.0,25,3.0,10:L10     
    	//5.0,18,4.0,42,3.0,12:L20      
    
    	foreach (var item in records)     
    	{         
    		var d = item.Split(':');         
    		//0 = 5.0,15,4.0,25,3.0,10         
    		//1 = L10          
    
    		datas2.Add(new(d[1], d[0].Split(",")));     
    	} 
    }
    

  2. Bruce (SqlWork.com) 63,746 Reputation points
    2023-09-12T20:42:23.3366667+00:00

    you can do the split in one shot:

    var text = "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20";
    var list = text.Split(new char[] {',',':',';'});
    

    but I don't understand the desired sort order. its not the original sequence, not order by decimal value, just seems random.

    0 comments No comments

  3. Bryan Tubbs 20 Reputation points
    2023-09-12T20:51:43.4633333+00:00
    namespace hacker_rank
    {
    	internal class Program
    	{
    
    		static void Main(string[] args)
    		{
    			string inputString = "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20";
    			var tokens = inputString.Split(new char[] { ',', ';', ':' });
    			
    			var outputString = $"<table border='1'><tr><th>Row Label</th><td>{string.Join("</td><td>", tokens)}</td></table>";
    		}
    	}
    }
    
    
    

    Value of outputString:

    <table border='1'><tr><th>Row Label</th><td>5.0</td><td>15</td><td>4.0</td><td>25</td><td>3.0</td><td>10</td><td>L10</td><td>5.0</td><td>18</td><td>4.0</td><td>42</td><td>3.0</td><td>12</td><td>L20</td></table>

    0 comments No comments

  4. Bryan Tubbs 20 Reputation points
    2023-09-12T20:54:15.7+00:00
    namespace hacker_rank
    {
    	internal class Program
    	{
    
    		static void Main(string[] args)
    		{
    			string inputString = "5.0,15,4.0,25,3.0,10:L10;5.0,18,4.0,42,3.0,12:L20";
    			var tokens = inputString.Split(new char[] { ',', ';', ':' });
    			
    			var outputString = $"<table border='1'><tr><th>Row Label</th><td>{string.Join("</td><td>", tokens)}</td></table>";
    		}
    	}
    }
    
    

    Value of outputString:

    <table border='1'><tr><th>Row Label</th><td>5.0</td><td>15</td><td>4.0</td><td>25</td><td>3.0</td><td>10</td><td>L10</td><td>5.0</td><td>18</td><td>4.0</td><td>42</td><td>3.0</td><td>12</td><td>L20</td></table>


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.