L'ho visto venire


19

Scrivi un programma o una funzione che accetta un numero intero maggiore di 1 o inferiore a -1. Cioè, l'input non sarà 0, 1 o -1.

Se l'input è 2, l'output dovrebbe essere:

|\_/|
|___|

Se l'input è 3, l'output dovrebbe essere:

|\_/\_/|
|______|

Se l'input è 4, l'output dovrebbe essere:

|\_/\_/\_/|
|_________|

Il modello continua allo stesso modo esatto per input più grandi. Ad esempio, se l'input è 10, l'output dovrebbe essere:

|\_/\_/\_/\_/\_/\_/\_/\_/\_/|
|___________________________|

Se l'input è -2, l'output dovrebbe essere:

 ____
|    |
|_/\_|

Se l'input è -3, l'output dovrebbe essere:

 _______
|       |
|_/\_/\_|

Se l'input è -4, l'output dovrebbe essere:

 __________
|          |
|_/\_/\_/\_|

Il modello continua allo stesso modo esatto per input più piccoli. Ad esempio, se l'input è -10, l'output dovrebbe essere:

 ____________________________
|                            |
|_/\_/\_/\_/\_/\_/\_/\_/\_/\_|

L'output può essere stampato o restituito come stringa con una nuova riga finale opzionale. L'angolo "vuoto" in alto a destra dell'uscita per gli ingressi negativi può essere uno spazio o può rimanere vuoto.

Vince il codice più breve in byte.


12
Ho visto cosa hai fatto lì.
Alex A.

Risposte:


1

Pyth, 45 byte

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||

Provalo online: Dimostrazione o Test Suite

Spiegazione:

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||  implicit: Q = input number
    J<Q0                                       assign Q < 0 to J
                           [           )       create a list with
                            d                    * the string " "
                             .<"\_/"J            * the string "\_/" rotated to 
                                                   the left by J
                                     \_          * the string "_"
                         .>             J      rotate to the right by J
                                         " ||  the string " ||"
        .b                                     binary mapping, N iterates
                                               over list, Y over string:
           .[YN+h*3t.aQJ                         pad Y with N to reach a string
                                                 of length 3*(abs(Q)-1)+1-J
          +             Y                        and append Y
 tW!J                                           remove the first line if Q > 0
j                                               print each on separate line

4

CJam, 56 50 49 byte

ri_(z"\_/"*'_@0>{\4>W<_,@*SooNoS}|1$,*]${'|\'|N}/

Provalo online nell'interprete CJam.

Come funziona

ri     e# Read an integer from STDIN and push it on the stack.
_(z    e# Push a copy, decrement it and apply absolute value.
       e# For positive n, (n -> n-1) and (-n -> n+1).
"\_/"* e# Repeat the string that many times.
'_     e# Push an underscore.
@0>    e# Check if the original integer is positive.
{      e# If it isn't:
  \    e#   Swap the generated string with the underscore.
  4>W< e#   Discard the string's first 4 and last character.
       e#   This makes the pattern of the bottom row start and end with an
       e#   underscore, truncating it to the correct length in the process.
  _,   e#   Push the length of a copy.
  @*   e#   Repeat the underscore that many times.
  So   e#   Print a space.
  oNo  e#   Print the underscores, then a linefeed.
  S    e#   Push a space.
}|     e#
1$,    e# Retrieve the strings length.
*      e# Repeat the underscore or space that many times.
]$     e# Wrap the two generated strings in an array and sort it.
{      e# For each string:
  '|\  e#   Push a vertical bar and swap the string on top of it.
  '|N  e#   Push a vertical bar and a linefeed.
}/     e#

3

Pyth, 56 54 byte

Sto giocando a golf con Pyth su un telefono con l'interprete online. È un'idea assolutamente fantastica.

Aggiornamento 2015-10-15: ho riscritto la cosa (sempre sul mio telefono, lol) e salvato 2 byte, di cui uno avrebbe potuto essere fatto anche con l'originale.

J<Q0Ljb"||"jPW!J_WJ[y<>*K+J*3t.aQ"\_/"JKy*K?Jd\_+d*K\_

Provalo online.


2

Minkolang 0,8 , 100 byte

"|"nd0`u!vbd3*["_"]"|"25*"|"1g["\_/"]"|"(O).
"[d~g1"_"<.)O(" "D*3R~1"_"*52"|"D*3R1dg2"| "*52"|"]"\/_

Crea la pila e poi stampa tutto in una volta. Sono sicuro che questo potrebbe essere giocato a golf ma ho già trascorso molto tempo su questo ...


1

JavaScript (ES6), 111 98 byte

Scoperta tecnica ottimale! Risulta che la rimozione di tutti quegli interpolatori dalle stringhe del modello consente di risparmiare molti byte. Forse potrebbe ancora essere ridotto, forse no. In ogni caso, le stringhe di modello ES6 (e le funzioni freccia) sono fantastiche. :)

x=>(x>0?`|\\_/A|
|___A|`:` ___A_
|   A |
|_/\\A_|`).replace(/(...)A/g,(_,y)=>y.repeat(x>0?x-1:~x))

0

Python 2.7, 144 byte

Questo ha richiesto più byte del previsto. Ecco il codice.

c=int(input())
p,w,n,u=list('| \n_')
a=abs(c)-1
d=3*a
if c>0:
 s=p+"\\_/"*a+p+n+p+u*d+p
else:
 d=d+1
 s=w+u*d+n+p+w*d+p+n+p+"_/\\"*a+u+p
print s

0

Java, 272 byte

String f(int i) {
String p = i>0?"\\_/":"_/\\_",x = "|"+new String(new char[(i<0?-i:i)-1]).replace("\0",p)+"|",
l=new String(new char[x.length()-2]).replace("\0","_");
return i>0?x+"\n|"+l+"|":" "+l+" \n|"+new String(new char[x.length()-2]).replace("\0"," ")+"|\n"+x;
}

0

SpecBAS - 167 byte

1 INPUT n: DIM s$="\_/","_/\": LET t$=s$(2-(n>0))*(ABS n-1)+("_"*(n<0)),u$="_"*LEN t$
2 TEXT IIF$(n>0,"|"+t$+"|"#13"|"+u$+"|"," "+u$+#13"|"+" "*LEN t$+"|"#13"|"+t$+"|")

IIF$è un'istruzione inline IF, #13è un modo per incorporare le nuove righe in una stringa (e non ha sempre bisogno di un "+" se si trova tra stringhe codificate).

Da qualche release fa, SpecBAS ti consente di assegnare più assegnazioni a LETun'istruzione, il che aiuta a salvare alcuni caratteri.


0

Python 2.7, 118 byte

n=input()*3-3
a=-n-6
s=' %s_\n| %s|\n|%s_|'%(a*'_',a*' ',a/3*'_/\\')
if n>0:s='|%s|\n|%s|'%(n/3*'\\_/',n*'_')
print s

Scendere da 120 a 118 è stato divertente!


0

Rubino - 113 byte

Sembra troppo lungo Proverò a giocare un po 'di più.

n=gets.to_i;p,h=n.abs-1,?|;n>0 ? (puts h+'\\_/'*p+h,h+'___'*p+h):(k=p*3+1;puts' '+?_*k,h+' '*k+h,'|_'+'/\\_'*p+h)

0

C #, 185 byte

C # lotta con il golf ripetendo le stringhe.

Completamente golf:

string S(int n){int m=n>0?n:-n;return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}","|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),"".PadLeft(m=m*3-(n>0?3:2),'_'),"".PadLeft(m));}

Rientro e nuove righe aggiunte per maggiore chiarezza:

string S(int n){
    int m=n>0?n:-n;
    return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}",
        "|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),
        "".PadLeft(m=m*3-(n>0?3:2),'_'),
        "".PadLeft(m)
    );
}

0

Powershell - 200 190 186 168 154

Golfato l'equazione (4 - (($ n-2) 3)) a (3 $ n-6) insieme ad alcune parentesi e punti e virgola estranei.

Trovato che `n è l'equivalente di [Environment]::NewLinee che $s -f [args]è l'equivalente di [String]::Format:

$n=$args;if($n-gt0){$s="|{1}|{0}|{2}|";$a=$n;$b=$n*3}else{$n*=-1;$s=" {2}{0}|{3}|{0}|_/{1}\_|";$a=$n-2;$b=$c=3*$n-2};$s-f"`n",("\_/"*$a),("_"*$b),(" "*$c)

La spiegazione mantiene le parentesi chiarificanti:

$n=$args;

// Basically a way of coming up with a string to format and the 
// necessary counts of repeated characters
if($n-gt0){
  // Placeholder format
  $s="|{1}|{0}|{2}|{3}";
  // Number of repeated "\_/" instances
  $a=$n;
  // Number of repeated "_" instances
  $b=$n*3
} else { 
  $n*=-1;
  $s=" {2}{0}|{3}|{0}|_/{1}\_|";
  $a=($n-2);
  $b=(4+(($n-2)*3));
  // Number of repeated " " instances .. not needed for "positive" saw
  $c=$b;
};
[String]::Format($s,[Environment]::NewLine,"\_/"*$a,"_"*$b," "*$c)
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.