这些是ASCII蒲公英:
\|/ \ / |
/|\ | \|/ |
| | | _\|/_
| | | /|\
ASCII蒲公英具有三个参数:茎的长度(正数在1到256之间,种子数(正数在0到7之间)和方向(^或v)。以上蒲公英的长度,种子和方向( 3,5,^),(3,2,^),(2,3,^)和(3,7,v)。
按照以下顺序填充种子(倒置蒲公英时倒置),长度为2:
seeds: 0 1 2 3 4 5 6 7
| \ / \|/ \ / \|/ _\ /_ _\|/_
| | | | /|\ /|\ /|\ /|\
| | | | | | | |
挑战:
编写一个程序/函数,当输入ASCII蒲公英作为输入时,返回其长度,种子数和方向的格式与上述示例类似,并且以该格式给出的参数返回具有这些参数的ASCII蒲公英。您可以忽略括号并假定输入/输出将是数字,逗号,数字,逗号以及^
或v
。您可以用^
/ 代替其他字符,v
只要它们仍然可以容易地解释为'up'/'down'(例如u
/ d
)即可。您无需区分外观相同的蒲公英,例如(2,1,^)和(3,0,^)或(2,1,^)和(2,1,v)。在给定ASCII艺术的情况下,任何一组参数都是可接受的输出,并且两组参数都可以给出相同的ASCII艺术。
这是代码高尔夫球,因此以字节为单位的最短代码获胜。
一个用C#编写的示例程序(甚至没有打高尔夫球):
string Dandelion(string s)
{
if (s.Contains(','))
{
//got parameters as input
string[] p = s.Split(',');
//depth and width (number of seeds)
int d = int.Parse(p[0]);
int w = int.Parse(p[1]);
//draw stem
string art = " |";
while (d > 2)
{
d--;
art += "\n |";
}
//draw head
string uhead = (w % 2 == 1 ? "|" : " ");
string dhead = uhead;
if (w > 1)
{
uhead = "\\" + uhead + "/";
dhead = "/" + dhead + "\\";
if (w > 5)
{
uhead = "_" + uhead + "_\n /|\\";
dhead = "_\\|/_\n " + dhead;
}
else if (w > 3)
{
uhead = " " + uhead + " \n /|\\";
dhead = " \\|/ \n " + dhead;
}
else
{
uhead = " " + uhead + " \n |";
dhead = " |\n " + dhead;
}
}
else
{
uhead = " " + uhead + "\n |";
dhead = " |\n " + dhead;
}
//add head to body
if (p[2] == "^")
{
return uhead + "\n" + art;
}
return art + "\n" + dhead;
}
else
{
//ASCII input
string[] p = s.Split('\n');
int l = p.Length - 1;
int offset = 0;
//find first non-' ' character in art
while (p[0][offset] == ' ')
{
offset++;
}
int w = 0;
if (p[0][offset] == '|')
{
//if '|', either head-down or no head.
if (offset == 0 || p[l][offset - 1] == ' ')
{
//if no space for a head to the left or no head at the bottom, no head.
return l.ToString() + ",1,^";
}
//head must have at least size 2, or else indistinguishable from no head case
w = 6;
if (p[l][offset] == '|')
{
//odd sized head
w = 7;
}
if (offset == 1 || p[l - 1][offset - 2] == ' ')
{
//not size 6 or 7
w -= 2;
if (p[l - 1][offset - 1] == ' ')
{
//not size 4 or 5
w -= 2;
}
}
return l.ToString() + "," + w.ToString() + ",v";
}
else if (p[0][offset] == '\\')
{
//head at least size 2 and not 6/7, or indistinguishable from no head.
w = 4;
if (p[0][offset + 1] == '|')
{
w = 5;
}
if (p[1][offset] == ' ')
{
w -= 2;
}
}
else
{
w = 6;
if (p[0][offset + 2] == '|')
{
w = 7;
}
}
return l.ToString() + "," + w.ToString() + ",^";
}
}
^
和v
吗?