Questi sono i denti di leone ASCII:
\|/ \ / |
/|\ | \|/ |
| | | _\|/_
| | | /|\
I denti di leone ASCII hanno tre parametri: Lunghezza dello stelo (numero positivo compreso tra 1 e 256, numero di semi (numero positivo tra 0 e 7) e orientamento (^ o v). I denti di leone sopra indicati hanno lunghezza, semi e orientamento ( 3,5, ^), (3,2, ^), (2,3, ^) e (3,7, v) rispettivamente.
I semi vengono riempiti nel seguente ordine (capovolto per i denti di leone a testa in giù), illustrato su un dente di leone della lunghezza 2:
seeds: 0 1 2 3 4 5 6 7
| \ / \|/ \ / \|/ _\ /_ _\|/_
| | | | /|\ /|\ /|\ /|\
| | | | | | | |
La sfida:
Scrivere un programma / funzione che quando viene fornito un dente di leone ASCII come input, restituisce la sua lunghezza, il conteggio dei semi e l'orientamento formattati in modo simile agli esempi precedenti e quando dati parametri in quel formato restituiscono un dente di leone ASCII con tali parametri. È possibile ignorare la parentesi e supporre che l'input / output sarà un numero, una virgola, un numero, una virgola e uno ^o v. È possibile sostituire altri caratteri per ^/ vpurché possano essere facilmente interpretati come "su" / "giù" (ad esempio u/ d). Non è necessario distinguere tra denti di leone che sembrano uguali, ad esempio (2,1, ^) e (3,0, ^) o (2,1, ^) e (2,1, v). Data l'arte ASCII, entrambi i set di parametri sarebbero un output accettabile ed entrambi i set di parametri possono dare la stessa arte ASCII.
Questo è code-golf , quindi vince il codice più breve in byte.
Un programma di esempio in C # (nemmeno leggermente golfato):
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() + ",^";
}
}
^ev?