เพิ่มเพชรให้เป็นสองเท่า


25

ปัญหา

รับจำนวนเต็มบวกnที่ไหนn < 100

เอาท์พุทรูปแบบเพชรดังนี้:

อินพุต n=1

/\/\
\/\/

อินพุตn=2:

 /\      /\
//\\/\/\//\\
\\//\/\/\\//
 \/      \/

อินพุตn=3:

  /\                /\
 //\\  /\      /\  //\\
///\\\//\\/\/\//\\///\\\
\\\///\\//\/\/\\//\\\///
 \\//  \/      \/  \\//
  \/                \/

อินพุตn=4:

   /\                              /\
  //\\    /\                /\    //\\
 ///\\\  //\\  /\      /\  //\\  ///\\\
////\\\\///\\\//\\/\/\//\\///\\\////\\\\
\\\\////\\\///\\//\/\/\\//\\\///\\\\////
 \\\///  \\//  \/      \/  \\//  \\\///
  \\//    \/                \/    \\//
   \/                              \/

และอื่น ๆ

กฎระเบียบ

  • อนุญาตให้ใช้โปรแกรมและฟังก์ชั่น
  • อนุญาตช่องว่างต่อท้าย
  • นำช่องว่างบนบรรทัดที่ไม่มี/หรือ\อนุญาต
  • ขึ้นบรรทัดใหม่และนำขึ้นบรรทัดใหม่
  • รหัสที่สั้นที่สุดในหน่วยไบต์ชนะ

นี่อาจจะเกี่ยวข้องกัน


2
@carusocomputing ตอนนี้คุณกำลังประสาทหลอนอยู่ ...
Erik the Outgolfer


1
@dzaima ไปที่ sandbox ด้วย!
Magic Octopus Urn

1
@carusocomputing แน่นอน แต่ก่อนอื่นฉันต้องคิดว่าทำไมและมันเกิดขึ้นได้อย่างไร: p
dzaima

คำตอบ:


12

SOGL V0.12 , 24 ไบต์

ā.∫ā;∫ \*+}ø┼↔±╬¡;øΚ┼}╬³

ลองที่นี่!

คำอธิบาย:

ā                         push an empty array (the main canvas)
 .∫                  }    iterate over the input, pushing 1-indexed iteration
   ā;                       push an empty array below the iteration
     ∫    }                 iterate over the iteration counter
       \*                     push current iteration amount of slashes
         +                    append those to the 2nd array
           ø┼               append nothing (so it'd space the array to a square)
             ↔±             reverse horizontally (swapping slashes)
               έ           quad-palindromize with 0 overlap and swapping characters as required
                 ;          get the canvas ontop
                  øΚ        prepend to it an empty line (so the now bigger romb would be one up)
                    ┼       append horizontally the canvas to the current romb
                      ╬³  palindromize horizontally with no overlap and swapping characters

2
ว้าวนั่นเป็นคำสั่งที่ป่วย
Magic Octopus Urn

@carusocomputing การเพิ่มล่าสุดเช่นกัน ไฟล์ที่เกี่ยวข้อง ยังต้องคิดว่าจะทำอย่างไรกับตัวละครที่เหลืออีก 190 ตัว
dzaima

ว้าวดังนั้นคุณมี 190 คำสั่งฟรีใน SOGOL และคุณสามารถเล่นกอล์ฟนี้ได้อย่างมีประสิทธิภาพหรือไม่?
Magic Octopus Urn

1
@carusocomputing ฉันหมายถึง 190 คำสั่งฟรีสำหรับlol
dzaima

2
@carusocomputing แต่ตามความจริงแล้วความสนุก (โดยประมาณ) 90/256 chars ไม่ได้ถูกนำมาใช้และ 61/256 ไม่มีเอกสารใด ๆ
dzaima

7

ถ่าน , 30 27 ไบต์

F⁺¹N«Fι«F⁴«↙⁻ικ↑⟲T»←»Mι←»‖M

ลองออนไลน์! การเชื่อมโยงคือการใช้รหัสเวอร์ชันอย่างละเอียด คำอธิบาย: การวาดแบบดั้งเดิมของ Charcoal ไม่สามารถวาดเพชรได้มากนักเนื่องจากการเคลื่อนไหวในแนวทแยงยังคงอยู่บนกำลังสองของความเท่าเทียมกัน แก้ไข: วิธีแก้ปัญหาใหม่คือการวาดด้านหนึ่งของเพชรแล้วหมุนผืนผ้าใบทั้งหมดพร้อมที่จะวาดด้านต่อไปทำให้เพชรถูกวาดเป็นวง วงนี้จะถูกบรรจุไว้ในวงเพื่อวาดเพชรภายในสำหรับแต่ละเพชร วงนอกสุดจะดึงเพชรทั้งหมดที่อยู่ติดกัน ในที่สุดภาพสะท้อน

Incrementโปรดทราบว่าถ่านได้ถูกขยายและไบต์อื่นจะถูกบันทึกไว้โดยใช้


การเคลื่อนไหวของตัวละคร 0.5 นั้นอยู่ที่ไหนเมื่อคุณต้องการ :(
เครื่องคิดเลข

6

APL (Dyalog) , 70 69 66 ไบต์

B←{'/\ '['\/'⍳⍺⍺⍵]}
C←⊢,⌽B
C(⊢⍪⊖B)⊃,/{C⊖A↑⊖' /'[⍵≤∘.+⍨⍳⍵+1]}¨⌽⍳A←⎕

ลองออนไลน์!

ถือว่า⎕IO←0ซึ่งเป็นมาตรฐานในหลาย ๆ ระบบดังนั้นโปรแกรมจะถูกจัดทำดัชนี 0

นี่คือ tradfn ที่รับอินพุตผ่าน STDIN

คำอธิบาย

(ล้าสมัยเล็กน้อย)

โปรดทราบว่าเป็นอาร์กิวเมนต์ซ้ายเป็นอาร์กิวเมนต์ที่ถูกต้องและ⍺⍺เป็นผู้ประกอบการด้านซ้าย

Bเป็นฟังก์ชั่นที่ช่วยในการสะท้อนเพชร มันต้องใช้สตริงเป็นอาร์กิวเมนต์ที่ถูกต้องและฟังก์ชั่นย้อนกลับเป็นซ้าย (เช่นBเป็นผู้ประกอบการ)

B←{'/\ '['\/'⍳⍺⍺⍵]}
              ⍺⍺⍵            Apply ⍺⍺ on 
         '\/'               Find the index of the reflected string in '\/' (if the character is not found in `'\/'`, then return an index out of the bounds of the string, ie `2` if the character is a space)
   '/\ '[        ]           Use these indexes on '/\ ' to reflect the '/\' characters

และตอนนี้เราไปที่ส่วนหลักของโปรแกรม

A←⎕              Assign the input to variable A
                Create a range 0 .. A-1
                Reverse it so that it becomes A-1 .. 0
¨                For each element do (the right argument is the element):
 ⍳⍵+1             Create a range 0 .. 
 ∘.+⍨             Create an addition table using the range to result in a matrix like so:
                   0+0 0+1 0+2 .. 0+⍵
                   1+0 1+1 1+2 .. 1+⍵
                   2+0 2+1 2+2 .. 2+⍵
                   ...
                   ⍵+0 ⍵+1 ⍵+2 .. ⍵+⍵
 ⍵≤              The elements of the matrix that are greater than or equal to the ⍵,
                 this creates a triangle matrix that looks like this:
                   0 0 .. 0 1
                   0 0 .. 1 1
                   ..
                   1 1 .. 1 1
 ' /'[...]       Index it in ' /' to get a character matrix
                 (ie replace 0s with spaces and 1s with '/'s)
                Flip this vertically
 A              Pad the top spaces

นี่เป็นสิ่งจำเป็นเพื่อให้แน่ใจว่าสามเหลี่ยมทั้งหมดที่สร้างขึ้นสำหรับทุกองค์ประกอบในช่วง⌽⍳Aนั้นมีความสูงเท่ากันเพื่อให้สามารถต่อกันได้ในภายหลัง

                Flip the matrix vertically again to go back to the original state
 (⊢,  )          Concatenate it with
    B           itself, but flipped horizontally
,/              Concatenate all triangles formed by the range operator
               The resulting matrix is nested, so this operator "un-nests" it

ตอนนี้ส่วนด้านซ้ายบนของลวดลายเสร็จสมบูรณ์แล้ว สิ่งที่เหลืออยู่คือพลิกมันในแนวตั้งและแนวนอน

(⊢⍪⊖B)          Concatenate the resulting matrix with itself but flipped vertically
                (the vertically flipped matrix is concatenated below of the original matrix)
                Now the left part of the pattern is complete
(⊢,⌽B)         Concatenate the resulting matrix with itself flipped horizontally

และนั่นมัน! เอาต์พุตเป็นเมทริกซ์อักขระที่มี/\s และมีช่องว่าง


6

05AB1E , 47 43 41 35 34 33 32 ไบต์

'/×ηηvy∞.C.Bø€∞¹NαGð.ø}})øíJ.B»∞

ลองออนไลน์!

(-4 ไบต์ขอขอบคุณ @Emigna ที่แนะนำการปรับปรุง 3 ครั้ง)


คำอธิบายนี้สำหรับรุ่นก่อนหน้ามีการทำซ้ำสองสามครั้งตั้งแต่นั้นมา

>                                          # [2]
 '/×                                       # ['//']
    η                                      # ['/','//']
     €η                                    # [['/'], ['/', '//']]
       vy                    }             # {For each element...}
         ∞                                 # Mirror horizontally.
          ¶¡                               # Split mirror on newlines.
            N£                             # Shave each diamond down a layer.
              .C                           # Horizontal center.
                .B                         # Pad for the transpose.
                  ø                        # Transpose.
                   €∞                      # Mirror each (vertically).
                     ¹NαFð.ø}              # Pad each list for transpose (verticaly).
                              )            # Wrap back to list...
                               €.B         # Pad each horizontally.
                                  ¦        # Remove the random numbers?
                                   ø       # Back to horizontal...
                                    €R     # Reverse to get correct order.
                                      J    # Join, no spaces.
                                       »   # Join newlines.
                                        ∞  # Final horizontal mirror.

มีช่องว่างระหว่างเพชรของคุณ
LiefdeWen

@LiefdeWen เป็นนี้โอเค? ด้วยการขึ้นบรรทัดใหม่และการขึ้นบรรทัดใหม่
Magic Octopus Urn

คุณสามารถใช้คำนำหน้าηแทนคำต่อท้ายเพราะพวกเขาจะเหมือนกันสำหรับสายนี้
Emigna

เป็นเช่นเดียวกับ¨ที่นี่และเป็น€R í
Emigna

@Emigna ฉันเล่นกอล์ฟบางส่วน แต่ขอขอบคุณ! คุณจะลองคำตอบ 33 ไบต์ที่ต่างกัน 100%: P
Magic Octopus Urn

5

CJam , 65 63 ไบต์

q~_,:)_W%\+f{_2*S*a@2$-*\_,f{)'/*\Se[_W%'/'\er+}_W%Wf%+1$++}zN*

ลองออนไลน์!

คำอธิบาย

nในการอธิบายนี้ผมจะหมายถึงจำนวนการป้อนข้อมูลที่เป็น

q~        e# Read and eval the input (push n to the stack).
_,        e# Copy it an get the range [0 .. n-1].
:)        e# Increment each element to get [1 .. n].
_W%       e# Copy it and reverse it.
\+        e# Prepend the reverse to the original range, resulting in [n n-1 .. 1 1 .. n-1 n].
f{        e# Map over each number x in the range using n as an extra parameter:
 _2*S*a   e#  Push a string containing n*2 spaces, and wrap it in an array.
 @2$-     e#  Push n-x.
 *        e#  Repeat the space string from before n-x times.
 \        e#  Bring x back to the top.
 _,       e#  Copy it and get the range [0 .. x-1].
 f{       e#  Map over each number y in this range, using x as an extra parameter:
  )       e#   Increment y.
  '/*     e#   Repeat '/' y times.
  \Se[    e#   Pad the resulting string to length x by adding spaces to the left.
  _W%     e#   Copy the result and reverse it.
  '/'\er  e#   Replace '/' with '\' in that.
  +       e#   Concatenate to the other string. This forms one row of one diamond.
 }        e#  (end map, now we have the top half of a diamond of size x)
 _W%      e#  Copy the half-diamond and reverse it.
 Wf%      e#  Reverse each row.
 +        e#  Concatenate to the top half. Now we have a full diamond of size x.
 1$++     e#  Put the spaces from before at the beginning and end. This is to pad the top
          e#  and bottom of the smaller diamonds.
}         e# (end map)
z         e# Transpose.
N*        e# Join with newlines. Implicit output.

จากความอยากรู้อยากเห็นทำไมe#ในคำอธิบาย?
Magic Octopus Urn

1
@carusocomputing มันเป็นความคิดเห็นเพื่อให้คุณสามารถเรียกใช้คำอธิบายของตัวเอง ไม่จำเป็นจริงๆ แต่¯ \ _ (ツ) _ / ¯
กิจการ Cat

1
@carusocomputing #ไม่ได้เป็นความคิดเห็นใน CJam - sourceforge.net/p/cjam/wiki/Basic%20operators/#number-sign-ถึงแม้ว่ามันจะเป็นภาษาอื่น ๆ อีกมากมาย เนื่องจาก CJam เป็นภาษากอล์ฟคำสั่งตัวเดียวทั้งหมดจึงใช้สำหรับการทำงานที่เหมาะสมกับการเล่นกอล์ฟ ความคิดเห็นมีประโยชน์สำหรับโค้ดที่ไม่ได้รับการดัดแปลงเท่านั้นดังนั้นจึงใช้ลำดับ 2 ตัวอักษรดังนั้นจึงเป็นการเพิ่มลำดับตัวอักษรหนึ่งตัวสำหรับสิ่งอื่น
Joe

3

Python 2 , 152 147 143 140 ไบต์

-1 ไบต์ขอบคุณ musicman523

n=input()
r=range(n)
r+=r[::-1]
for x,i in enumerate(r):a,b='/\\\/'[i<x::2];s=' '*(n+~i);print''.join((s+a*n)[:n-j]+(b*-~i+s)[j:]for j in r)

ลองออนไลน์!

มันทำงานได้โดยการตัดคอลัมน์ด้านในของเพชรที่ใหญ่ที่สุดเพื่อทำให้ชิ้นเล็กลงใช้[0,..,n,n,..,0]สำหรับควบคุมจำนวนคอลัมน์ที่จะลบ


คุณสามารถรับหนึ่งไบต์ราคาถูกโดยการเปลี่ยนr=r+เป็นr+=
musicman523


3

Dyalog APL, 46

{⊃,/⍵∘{'/ \'[2+((-⍪⊖)⌽,-)(-⍺)↑∘.≥⍨⍳⍵]}¨(⌽,⊢)⍳⍵}

ยินดีต้อนรับสู่ PPCG และคำตอบแรกที่ดี! เมื่อดูว่านี่เป็น dfn อย่างไรฉันได้เพิ่ม{}คำตอบของคุณลงไปเนื่องจากต้องรวมไว้ด้วย
Kritixi Lithos


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.