Herausforderung am Tag des Waffenstillstands


28

Der heutige 11. November ist als Gedenktag , Waffenstillstandstag oder Veteranentag (je nach Land) bekannt und ein Tag der Besinnung und Dankbarkeit für die Mitglieder des Militärs und ihren Dienst, der speziell das Ende der europäischen Feindseligkeiten widerspiegelt im ersten Weltkrieg. Lassen Sie uns dies mit einer einfachen ASCII-Ausgabe von reflektieren 11/11.

Bei einer Eingabe nwird eine ASCII-artige Wiedergabe ausgegeben 11/11, die nEinheiten groß ist. Insbesondere besteht jedes 1aus vertikalen Rohren ||, der Schrägstrich besteht aus Schrägstrichen //und jedes Zeichen ist zwei Leerzeichen voneinander entfernt. Beachten Sie, dass dies unterschiedliche Ausgabebreiten bedeutet. n=3Sehen Sie beispielsweise unten, wie der "Grund" des Schrägstrichs zwei Leerzeichen von 1links, aber vier Leerzeichen von 1rechts entfernt ist, sodass der obere Rand des Schrägstrichs angezeigt wird Richtet sich passend aus und ist zwei Felder von 1rechts entfernt.

n = 1
||  ||  //  ||  ||


n = 2
||  ||   //  ||  ||
||  ||  //   ||  ||


n = 3
||  ||    //  ||  ||
||  ||   //   ||  ||
||  ||  //    ||  ||


n = 4
||  ||     //  ||  ||
||  ||    //   ||  ||
||  ||   //    ||  ||
||  ||  //     ||  ||


n = 5
||  ||      //  ||  ||
||  ||     //   ||  ||
||  ||    //    ||  ||
||  ||   //     ||  ||
||  ||  //      ||  ||

und so weiter.

Eingang

Eine einzelne positive ganze Zahl in jedem geeigneten Format , n > 0.

Ausgabe

Eine ASCII-artige Darstellung 11/11gemäß den obigen Regeln und Beispielen. Führende / nachfolgende Zeilenumbrüche oder andere Leerzeichen sind optional, sofern die Zeichen richtig ausgerichtet sind.

Regeln

  • Es ist entweder ein vollständiges Programm oder eine Funktion zulässig. Bei einer Funktion können Sie die Ausgabe zurückgeben, anstatt sie zu drucken.
  • Standard-Schlupflöcher sind verboten.
  • Dies ist daher gelten alle üblichen Golfregeln, und der kürzeste Code (in Byte) gewinnt.

Antworten:


4

Jelly, 24 bytes

⁾| Ḥẋ2µ,Ṛðj
⁶ẋṖ;⁾//ṙḶÇ€Y

TryItOnline!

How?

⁾| Ḥẋ2µ,Ṛðj - Link 1, join two "11"s with: middle
⁾|          - string literal: "| "
   Ḥ        - double          "||  "
    ẋ2      - repeat twice    "||  ||  "
      µ     - monadic chain separation
       ,    - pair with
        Ṛ   - reversed       ["||  ||  ","  ||  ||"]
         ð  - dyadic chain separation
          j - join with input "||  ||  middle  ||  ||"

⁶ẋṖ;⁾//ṙḶÇ€Y - Main link: n       e.g. 5
⁶            - literal ' '        ' '
 ẋ           - repeat n times     "     "
  Ṗ          - remove last entry  "    "
   ;         - concatenate with
    ⁾//      - literal "//"       "    //"
        Ḷ    - lowered range(n)   [0,1,2,3,4]
       ṙ     - rotate left        ["    //","   // ","  //  "," //   ","//    "]
         Ç€  - call last link (1) as a monad for €ach
           Y - join with line feeds

21

JavaScript (ES6), 57 bytes

n=>" ".repeat(n).replace(/./g,"||  ||  $'//$`  ||  ||\n")

2
What the... forget my answer, this is genius. I should stop going the conventional route every single time :P
ETHproductions

That's slick. Nice answer.
AdmBorkBork

Can someone explain the $' and the $` in the regex? I've never seen that before and would love to understand it better.
Robert Hickman

1
@RobertHickman They refer to the part of the string after and before the match ($& would be the match itself).
Neil

@Neil, thanks! You learn something new everyday :)
Robert Hickman

7

05AB1E, 24 bytes

<ðׄ//J¹FD"  ||"2׊««,À

Try it online!

Explanation

                          # implicit input n
<ð×                       # push n-1 spaces
   „//J                   # join with "//"
       ¹F                 # input times do:
         D                # duplicate top of stack
          "  ||"2×        # push "  ||  ||"
                  Â       # push "||  ||  "
                   Š      # move the top of the stack down 2 places on the stack
                    ««    # concatenate the top 3 elements of the stack
                      ,   # print with newline
                       À  # rotate top of stack left

Previous 26 byte version

F"||  "2שð¹N-<ׄ//ðN×®RJ,

Try it online!


2
That was fast!!
AdmBorkBork

" "×"//"«.s¦R"|| || "s«vyû}» , turns out palendromize isn't a good fit, for reasons more obvious now... and you beat my bytecount faster anyhow heh.
Magic Octopus Urn

6

Perl, 45 bytes

-9 bytes thanks to @Gabriel Benamy

47 bytes of code + 1 byte for -n flag.

say$@="||  "x2,$"x$_,"//",$"x++$.,$@while$_--

Run with -nE flags :

perl -nE 'say$@="||  "x2,$"x$_,"//",$"x++$.,$@while$_--' <<< 5

Save 4 bytes by changing "|| ||" to "|| "x2 and then turning (2+$_) into just $_
Gabriel Benamy

I think you can also drop the +( .. ) around the $@ assignment. It works on my computer, at least.
Gabriel Benamy

@GabrielBenamy I can indeed drop the +( .. ), thanks. However I can't change "|| ||" to "|| "x2 because I need two spaces between the ||.
Dada

"|| " has two spaces after the pipes (it's just not rendering correctly here for some reason), and you're duplicating that string into "|| || " which takes care of the extra 2 spaces from $"x(2+$_)
Gabriel Benamy

@GabrielBenamy Oh thanks, that would be SE formatting showing only one space in || when there were two them.
Dada

5

JavaScript (ES6), 88 77 bytes

f=(n,i=n)=>i--?`||  ||  ${" ".repeat(i)}//${" ".repeat(n+~i)}  ||  ||
`+f(n,i):""

The recursive approach may not be could not possibly be the shortest.


.map version (88 bytes):

n=>[...Array(n)].map((_,i)=>`||  ||  ${" ".repeat(n+~i)}//${" ".repeat(i)}  ||  ||`).join`
`

Array comprehension (86 bytes):

n=>[for(_ of Array(i=n))`||  ||  ${" ".repeat(--i)}//${" ".repeat(n+~i)}  ||  ||`].join`
`

for loop version (89 bytes):

n=>{for(a="",i=n;i--;a+=s+" ".repeat(i+2)+`//${" ".repeat(n-i+1)+s}
`)s="||  ||";return a}

.replace version (85 bytes):

n=>`||  ||  q  ||  ||
`[r="repeat"](n).replace(/q/g,_=>" "[r](--i)+"//"+" "[r](n+~i),i=n)

4

Retina, 29 bytes

.+
$* 
.
xx$'//$`  xx¶
x
||  

Port of my JavaScript solution. Note the space after $* and two spaces after ||.


3

V, 30 bytes

4i||  2Bi//  Àé XÀ«ñÄf/é Elx

Try it online!

As usual, here is a hexdump:

0000000: 3469 7c7c 2020 1b32 4269 2f2f 2020 1bc0  4i||  .2Bi//  ..
0000010: e920 58c0 abf1 c466 2fe9 2045 6c78       . X....f/. Elx

I think you could change the initial part to 5i|| <esc>3b2r/. You'll be at a slightly different place though, and I can't read V so I'm not sure if that matters.
nmjcman101

3

Batch, 130 bytes

@set s=xx//  xx
@set l=@for /l %%i in (2,1,%1)do @call
%l% set s=%%s://= //%%
%l%:l
:l
@echo %s:x=^|^|  %
@set s=%s: //=// %

Not a port of my JavaScript solution. Since |s are hard to manipulate in Batch, I use xs as placeholders and replace them on output, this conveniently also reduces my code size. Starts by setting s to the desired output for n=1 (n is passed on the command line), then inserts spaces as necessary to obtain the first line for the actual value of n, then loops through printing the string and shifting the slash left by one character each time.


3

BaCon, 71 bytes

A complete BASIC program in one line.

INPUT n:FOR x=1 TO n:?"||  ||",SPC$(n-x+2),"//",SPC$(x+1),"||  ||":NEXT

Nice! Is it possible to remove the space in 1 TO?
DLosc

3

Common Lisp, 216 bytes

I'm going to state right off the bat that this is an awful solution to the challenge. Nevertheless, it works, and I'm tired.

(defun arm (n) (format t "~{||  || ~v,,,vA//~v,,,vA ||  ||~%~}" (butlast (butlast (butlast (butlast (butlast (butlast (loop for i from 1 to (+ n 1) append `(,(- (+ n 1) i) #\Space #\Space ,i #\Space #\Space))))))))))

Usage:

* (arm 4)
||  ||     //  ||  ||
||  ||    //   ||  ||
||  ||   //    ||  ||
||  ||  //     ||  ||

For some reason, instead of doing anything sane, I decided to approach this with a loop inside a format call. This loop iterates through the contents returned by the other actual loop construct at the very end, with the last six elements removed (thus the repeated butlasts). The contents of the value returned by this loop construct consist of a padding count for the front of the slashes, the padding characters (spaces), the padding count for the back of the slashes, and finally the same padding characters.

I'm rather new to Lisp, and I understand that there is definitely a lot of room for improvement here.


You should try golfing your solution (eliminating uneeded whitespace, shortening identifiers)
cat

Welcome to PPCG!
AdmBorkBork

3

Python 2, 76 75 71 Bytes

Still working on a shorter version, not too bad though.

n=input()
k='||  ||'
for i in range(n):print k,(n-i)*' '+'//'+' '*-~i,k

thanks mbomb007 for catching an error!


1 byte shorter: x='|| '*2;print x+(n-i)*' '+'//'+' '*i+x[::-1]
mbomb007

@mbomb007 You need two spaces between the 11s, not 1. That would make it equal.
Kade

That's just SE messing it up. It's still a byte shorter. repl.it/EViJ
mbomb007

@mbomb007 According to that repl.it page there are three spaces before // on the last row and two spaces after // on the first row. It should be two spaces in both cases.
Kade

Then your current program is wrong, because that's what yours does.
mbomb007

2

R, 86 bytes

Just a simple for loop approach:

x="||  ||";n=scan();for(i in 1:n)cat(x,rep(" ",2+n-i),"//",rep(" ",1+i),x,"\n",sep="")

2

Retina, 40 bytes

.+
x $&$* //  x
;{:`x
||  ||
   //
  // 

Try it online!

Explanation

.+
x $&$* //  x

This turns the input N into

x S//  x

Where S corresponds to N spaces.

;{:`x
||  ||

There are two things happening here. ;{: indicates that this stage and the last one should be run in a loop until they fail to change the string. : indicates that the result of this stage should be printed after each iteration and ; indicates that the final result of the loop (and therefore of the entire program) should not be printed. The stage itself just replaces the xs with || || on the first iteration (and does nothing afterwards), so that we now have the first line of the required output (and print it).

   //
  // 

Finally, this shifts the // one character to the left, provided there are still at least three spaces left of the //. Afterwards we return to the previous stage (which now only prints the current line, since there are no more xs) and then repeat.


2

Ruby, 60 bytes

->n{n.times{|i|puts (a='||  || ')+' '*(n-i)+'//  '+' '*i+a}}


1

Ruby, 76 74 73 bytes

x="||  ||";n=gets.to_i;puts (1..n).map{|i|x+" "*(n-i+2)+"//"+" "*(i+1)+x}

As a function it takes 73 72 bytes, counting the definition:

def f n,x="||  ||";(1..n).map{|i|x+" "*(n-i+2)+"//"+" "*(i+1)+x}*?\n;end

1

Powershell, 66 bytes

$a=read-host;1..$a|%{$s="||  ";$s*2+" "*($a-$_)+"// "+" "*$_+$s*2}

Welcome to the site!
DJMcMayhem

@DrMcMoylex Just wondering, I saw this answer and was going to do the same thing you did, but it said that I did not change enough in my edit. How were you able to do this?
nedla2004

@nedla2004 Once you have 1,000 reputation, (or 2,000 for fully graduated sites) you'll get full editing priveleges. Until then, all your edits have to be at least 6 characters and will be reviewed by other users with more rep. Since I have over 1,000 rep, I am allowed to submit small edits immediately.
DJMcMayhem

Welcome to PPCG! You can save a couple bytes by taking command-line input instead of read-host -- param($a)1..$a|%{$s="|| ";$s*2+" "*($a-$_)+"// "+" "*$_+$s*2}
AdmBorkBork

Thanks for the welcomes, the edit, and the command line suggestions all!
ben

1

C#, 150 Bytes

Golfed:

string A(int n){string a="",b= "||  ||";for(int i=0;i<n;i++)a+=b+"  //  ".PadLeft(5+n-i,'\0')+string.Concat(Enumerable.Repeat(" ",i))+b+"\n";return a;

Ungolfed:

public string A(int n)
{
  string a = "", b = "||  ||";
  for (int i = 0; i < n; i++)
    a += b + "  //  ".PadLeft(5 + n - i, '\0') + string.Concat(Enumerable.Repeat(" ", i)) + b + "\n";
  return a;
}

Testing:

Console.WriteLine(new ArmisticeDayChallenge().A(11));

Output:

||  ||            //  ||  ||
||  ||           //   ||  ||
||  ||          //    ||  ||
||  ||         //     ||  ||
||  ||        //      ||  ||
||  ||       //       ||  ||
||  ||      //        ||  ||
||  ||     //         ||  ||
||  ||    //          ||  ||
||  ||   //           ||  ||
||  ||  //            ||  ||

1

Groovy, 63 chars/bytes

Here's my attempt with Groovy, using an anonymous closure and simple loops to print the ASCII art to standard output:

{n->n.times{println'|| '*2+' '*(n-it-1)+'//'+' '*it+' ||'*2}}

You can try it online here. Just click "Edit in console" and then "Execute script".

Trying to do the same and returning a string instead of printing, I couldn't get below 71 bytes:

{n->a='';n.times{a+='|| '*2+' '*(n-it-1)+'//'+' '*it+' ||'*2+'\n'};a}


0

Python 3, 78 bytes

a="||  ||"
def m(n):
    for e in range(n):print(a," "*(n-e),"//"," "*(e+1),a)

Still trying to shorten...


Welcome to PPCG! Might you be able to move the for onto the same line as def? (Like this: def m(n):for e in range(n):print(a," "*(n-e),"//"," "*(e+1),a)) Also, you can save two bytes by replacing (e+1) with -~e.
ETHproductions
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.