Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 将降价转换为HTML算法面试问题-最佳方法?_Algorithm_Text Processing_Text Parsing_Converters - Fatal编程技术网

Algorithm 将降价转换为HTML算法面试问题-最佳方法?

Algorithm 将降价转换为HTML算法面试问题-最佳方法?,algorithm,text-processing,text-parsing,converters,Algorithm,Text Processing,Text Parsing,Converters,我最近在一次技术面试中得到了这个问题,时间不多了 任务是编写一个标记到HTML转换器。考虑到以下输入: This is a paragraph with a soft line break. This is another paragraph that has > Some text that > is in a > block quote. This is another paragraph with a ~~strikethrough~~ word. 产生以下输出:

我最近在一次技术面试中得到了这个问题,时间不多了

任务是编写一个标记到HTML转换器。考虑到以下输入:

This is a paragraph with a soft
line break.

This is another paragraph that has
> Some text that
> is in a
> block quote.

This is another paragraph with a ~~strikethrough~~ word.
产生以下输出:

<p>This is a paragraph with a soft<br />line break.</p>

<p>This is another paragraph that has <br />
  <blockquote>Some text that<br />is in a<br />block quote.</blockquote>
</p>

<p>This is another paragraph with a <del>strikethrough</del> word.</p>
这是一段带有软换行符的段落

这是另一段有

位于
块引号中的某些文本。

这是另一段带有删除线的文字

输出的格式并不重要,只是必须是有效的HTML

我想不出一个好办法来做这件事。我能想到的唯一一件事是用两个换行符分割得到段落,然后迭代每个段落以检查每个特殊符号的存在,并替换为它的HTML等价物。不过,这感觉有点骇人听闻,我相信有一种更结构化的方式来处理这类问题,记住这是一个时间限制约为30分钟的面试环境


我欢迎任何建议。

这是我在一小时内的尝试

如果可能,代码会反复尝试合并相邻的行。 为了提高可读性和性能,需要花费额外的时间来调整它。 一些额外的错误检查就可以了

using System;
using System.Collections.Generic;

namespace akMdConverter
{
    class Program
    {
        const string Break = "<br />";

        static void Main(string[] args)
        {
            var md = new string[]
            {
                "This is a paragraph with a soft",
                "line break.",
                "",
                "This is another paragraph that has",
                "> Some text that",
                "> is in a",
                "> block quote.",
                "",
                "This is another paragraph with a ~~strikethrough~~ word."
            };

            md = MergeBlockQuotes(md);
            md = MergeSoftLineBreaks(md);
            md = MergeParagraphs(md);
            ReplaceStrikeThroughs(md);

            foreach (var m in md)
            {
                Console.WriteLine(m);
            }
        }

        static string[] MergeBlockQuotes(string[] md)
        {
            var lines = new List<string>();
            string previousLine = "";

            foreach(string line in md)
            {
                if (line.StartsWith(">"))
                {
                    if (previousLine != "")
                    {
                        previousLine = previousLine + Break + 
                                       line.Substring(1).Trim();
                    }
                    else
                    {
                        previousLine = line.Trim();
                    }
                }
                else
                {
                    if (previousLine != "")
                    {
                        lines.Add(previousLine);
                        previousLine = "";
                    }
                    lines.Add(line);
                }
            }

            return lines.ToArray();
        }

        static string[] MergeSoftLineBreaks(string[] md)
        {
            var lines = new List<string>();
            string previousLine = "";

            foreach (string line in md)
            {
                if ((line == "") || line.StartsWith(">"))
                {
                    if (previousLine != "")
                    {
                        lines.Add(previousLine);
                        lines.Add(line);
                        previousLine = "";
                    }
                    else
                    {
                       lines.Add(line);
                    }
                }
                else
                {
                    if (previousLine != "")
                    {
                        previousLine += Break;
                    }

                    previousLine += line;
                }
            }

            if (previousLine != "")
            {
                lines.Add(previousLine);
            }

            return lines.ToArray();
        }

        static string[] MergeParagraphs(string[] md)
        {
            var lines = new List<string>();
            string previousLine = "";

            foreach (string line in md)
            {
                if (line == "")
                {
                    if (previousLine != "")
                    {
                        lines.Add(Bracket(previousLine, "p"));
                        previousLine = "";
                    }
                    lines.Add("");
                }
                else if (line.StartsWith(">"))
                {
                    if (previousLine != "")
                    {
                        previousLine += Break + "\n";
                    }

                    previousLine += Bracket(line.Substring(1), "blockquote");
                }
                else
                {
                    if (previousLine != "")
                    {
                        previousLine += Break;
                    }

                    previousLine += line;
                }
            }

            if (previousLine != "")
            {
                lines.Add(Bracket(previousLine, "p"));
            }

            return lines.ToArray();
        }

        static string Bracket(string s, string bracket)
        {
            return "<" + bracket + ">" + s.Trim() + "<" + bracket + "/>";
        }

        static void ReplaceStrikeThroughs(string[] md)
        {
            for(int i = 0; i < md.Length; i++)
            {
                string s = md[i];

                int tilde1 = s.IndexOf("~~");
                while (tilde1 >= 0)
                {
                    int tilde2 = s.IndexOf("~~", tilde1 + 2);

                    if (tilde2 > tilde1)
                    {
                        s = s.Substring(0, tilde1) + "<del>" + 
                            s.Substring(tilde1+2, tilde2 - tilde1 - 2) + "</del>" + 
                            s.Substring(tilde2 + 2);
                        tilde1 = s.IndexOf("~~");
                    }
                    else
                    {
                        tilde1 = -1;
                    }
                }

                md[i] = s;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间akMdConverter
{
班级计划
{
常量字符串中断=“
”; 静态void Main(字符串[]参数) { var md=新字符串[] { “这是一段带有软文的段落”, “换行。”, "", “这是另一段有”, “>一些文本, “>处于一种状态”, “>整体报价。”, "", “这是另一个带有~~删除线~~单词的段落。” }; md=合并块报价(md); md=合并软换行符(md); md=合并段落(md); 置换筛毛(md); foreach(md中的var m) { 控制台写入线(m); } } 静态字符串[]合并块引号(字符串[]md) { 变量行=新列表(); 字符串previousLine=“”; foreach(md中的字符串行) { if(第行开始时带(“>”) { 如果(上一行!=“”) { 前一行=前一行+中断+ line.Substring(1.Trim(); } 其他的 { previousLine=line.Trim(); } } 其他的 { 如果(上一行!=“”) { 行。添加(上一行); 前一行=”; } 行。添加(行); } } 返回行。ToArray(); } 静态字符串[]合并软换行符(字符串[]md) { 变量行=新列表(); 字符串previousLine=“”; foreach(md中的字符串行) { if((line==“”)| | line.StartsWith(“>”) { 如果(上一行!=“”) { 行。添加(上一行); 行。添加(行); 前一行=”; } 其他的 { 行。添加(行); } } 其他的 { 如果(上一行!=“”) { 前一行+=中断; } 前一行+=前一行; } } 如果(上一行!=“”) { 行。添加(上一行); } 返回行。ToArray(); } 静态字符串[]合并段落(字符串[]md) { 变量行=新列表(); 字符串previousLine=“”; foreach(md中的字符串行) { 如果(行==“”) { 如果(上一行!=“”) { 增加(括号(前一行,“p”); 前一行=”; } 行。添加(“”); } else if(第行开始时带(“>”) { 如果(上一行!=“”) { 前一行+=中断+“\n”; } 前一行+=括号(第行子字符串(1),“块引号”); } 其他的 { 如果(上一行!=“”) { 前一行+=中断; } 前一行+=前一行; } } 如果(上一行!=“”) { 增加(括号(前一行,“p”); } 返回行。ToArray(); } 静态字符串括号(字符串s、字符串括号) { 返回“+s.Trim()+”; } 静态void replacethraws(字符串[]md) { 对于(int i=0;i=0) { int tilde2=s.IndexOf(“~~”,tilde1+2); 如果(tilde2>tilde1) { s=s.子字符串(0,tilde1)+” s、 子字符串(tilde1+2,tilde2-tilde1-2)+“” s