พีระมิดแห่งสายขาด


10

รับสตริง n สร้างปิรามิดของสตริงแยกเป็นส่วน ๆ เทียบกับแถวปัจจุบัน

แถวแรกมีสตริงที่ไม่ได้แก้ไข

แถวที่สองมีสตริงที่แบ่งออกเป็นครึ่งโดยไพพ์

แถวที่สามแยกเป็นสาม ...

และอื่น ๆ ความยาวของแต่ละสตริงย่อยโดยที่ l คือความยาวของสตริง n เท่ากับ

ชั้น (L / N)

อักขระที่เหลือจะใส่ในสตริงย่อยของตนเอง แถวสุดท้ายที่ใช้คือแถวแรกที่มีความยาว 2

กรณีทดสอบ:

อินพุต: สวัสดีโลก

เอาท์พุท:

Hello, world.

Hello,| world|.

Hell|o, w|orld|.

Hel|lo,| wo|rld|.

He|ll|o,| w|or|ld|.

อินพุต: abcdefghij

เอาท์พุท:

abcdefghij

abcde|fghij

abc|def|ghi|j

ab|cd|ef|gh|ij

อินพุต: 01234567890abcdef

เอาท์พุท:

01234567890abcdef

01234567|890abcde|f

01234|56789|0abcd|ef

0123|4567|890a|bcde|f

012|345|678|90a|bcd|ef

01|23|45|67|89|0a|bc|de|f

กฎพิเศษ:

  • คุณสามารถเขียนโปรแกรมเต็มรูปแบบหรือฟังก์ชั่นใดก็ตามที่ใช้รหัสน้อย

  • ข้อมูลที่ป้อนจะมีความยาวอย่างน้อย 4 ตัวอักษร

  • คุณต้องใช้ตัวแบ่งบรรทัดหากภาษาของคุณรองรับ หากไม่สามารถทำได้ให้เปลี่ยนการขึ้นบรรทัดใหม่ด้วย:

  • การป้อนข้อมูลจะสามารถพิมพ์ได้ ASCII เสมอ

  • ลบ 100% ถ้าโปรแกรมของคุณแก้ P กับ NP


ลีดเดอร์บอร์ด:


0 bytes:return: false
Gabriel Benamy

3
ความท้าทายแรกที่ดี! คำถามชี้แจงสองสามข้อ - ใส่ ASCII ที่พิมพ์ได้เท่านั้นหรือไม่ "การแบ่งบรรทัดมีความจำเป็นเมื่อเป็นไปได้" หมายความว่าอย่างไร
AdmBorkBork

3
มันเป็นเรื่องตลก P vs NP เป็นปัญหาที่ไม่สามารถแก้ได้ในการคำนวณ เรื่องตลกคือถ้าคุณสามารถแก้มันได้ฉันจะหยุดใส่ใจเกี่ยวกับความจริงที่ว่าโปรแกรมของคุณไม่สามารถแก้ปัญหาได้
Julian Lachniet

3
ปัญหาที่เกิดขึ้นจริงในการคำนวณคือ "แท็บหรือช่องว่าง" ...
FlipTack

3
ไม่ปัญหาที่แท้จริงคือ Internet Explorer
Julian Lachniet

คำตอบ:


0

JavaScript (ES6), 103 101 91 84 ไบต์

แก้ไขเพื่อเคารพข้อกำหนดท้าทาย

f=(s,n=0,p=s.length/++n|0)=>p>1?s.match(eval('/.{1,'+p+'}/g')).join`|`+'\n'+f(s,n):''

แลมบ์ดาfที่รับสตริงป้อนเข้าเป็นพารามิเตอร์ตัวแรกsและพิมพ์ซ้ำเพื่อเล่นชุดอักขระแยกสตริง ตรงไปตรงมาสวย: ตราบใดที่ความยาวซับสตริงp, อยู่เหนือ 1, พิมพ์สตริงแยกโดย '|' ทุกpตัวละครจากนั้นดำเนินการต่อในระดับต่อไป จากนั้นจะเรียกฟังก์ชันนี้อีกครั้งโดยมีpการt / nปูพื้นซึ่งtเป็นความยาวสตริงเดิมและnเป็นตัวแบ่งที่เพิ่มขึ้น


ฉันไม่คิดว่าการหารnด้วย 2 ในแต่ละครั้งนั้นถูกต้อง
Neil

@Neil คุณถูกต้องผิดพลาดในส่วนของฉัน ฉันแก้ไขปัญหาและบันทึก 2 ไบต์ในกระบวนการ
XavCo7

@ ETHproductions ฉันคิดว่า แต่ฉันไม่ทราบว่าจะนับเป็น STDOUT ... ฉันเดาว่าฉันจะต้องทำalert(f(s))หลังจากขวา?
XavCo7

4

Perl, 46 + 1 = 47 ไบต์

วิ่งด้วย-nธง

say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2

ลองออนไลน์!

การวิเคราะห์รหัส

-n                                              #Reads input into the $_ variable
say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2
                                 y///c          #Transliteration.  Implicitly operates on $_, replacing every character with itself and counting replacements
                                                #y///c effectively returns the length of $_
                                      /++$,     #Increments $, (which starts off at 0) and divides the length of $_ by $,
                              $==               #Stores the result of this division into $=
                                                #$= forces its contents to be an integer, so it truncates any decimal
                             (             )-2  #Returns 0 if $= is equal to 2
                        while                   #Evaluates its RHS as the condition.  If truthy, evaluates its LHS.
    s/          /   /gr                         #Substitution.  Implicitly operates on $_.
                                                #Searches for its first argument and replaces it with its second argument, repeating until it's done, and returns the new string.  $_ is not modified.
      .{$=}                                     #Looks for a string of $= characters...
           (?=.)                                #...that is followed by at least one non-newline character, but does not include this character in the match...
                 $&|                            #...and replaces it with itself followed by a pipe character.
say                                             #Output the result of the substitution.

ดูเหมือนจะไม่สามารถใช้งานได้กับอินพุตที่ยาวขึ้น
Neil

2

Pyth, 16 ไบต์

Vh/lQ3j\|cQ/lQhN

V                # For N in range(1, \/ )
 h/lQ3           # 1+lenght(input)/3
      j\|        # join with '|'
         cQ      # chop input in
           /lQhN # lenght(input)/(N+1) pieces

ลองที่นี่


1
สิ่งนี้อาจใช้ได้กับกรณีทดสอบ แต่ฉันไม่คิดว่ามันจะใช้งานได้กับอินพุตที่ยาวขึ้น
Neil

2

C, 145 131 128 125 ไบต์

l,n,i=1,j;f(char*s){l=strlen(s);puts(s);do{n=l/++i;for(j=0;j<l;)j&&(j%n||putchar('|')),putchar(s[j++]);puts("");}while(n>2);}

นี่คือฟังก์ชันที่รับสตริงเป็นอาร์กิวเมนต์และพิมพ์เอาต์พุตไปที่ STDOUT

l,n,i=1,j;       // declare some variables
f(char*s){       // declare the function
l=strlen(s);     // get the length of the string
puts(s);         // output the initial version, with trailing newline
do{n=l/++i;      // n is the number of characters per "section",
                 //  and we'll do-while n>2 to stop at the right time
for(j=0;j<l;)    // loop through the characters of the string
j&&(             // if j != 0,
j%n||            // and j % n == 0,
putchar('|')),   // insert a | before this character
putchar(s[j++]); // print the character
puts("");        // print a newline after the loop
}while(n>2);}

มันทำงานครั้งเดียวได้i*i>lอย่างไร ดูเหมือนว่ามันจะเริ่มทำซ้ำส่วน
Neil

@ ไม่มีฉันไม่แน่ใจว่าคุณหมายถึงอะไร คุณยกตัวอย่างได้ไหม
Doorknob

@ Neil Ah ไม่เป็นไรฉันเห็นสิ่งที่คุณพูด ที่ดูเหมือนจะเป็นรูในสเปคซึ่งระบุอย่างชัดเจนว่าความยาวของแต่ละสตริงย่อยคือfloor(l/n); ฉันไม่แน่ใจว่าพฤติกรรมที่ตั้งใจไว้มีไว้เพื่อการป้อนข้อมูลที่นานขึ้นหรือถ้า OP คาดว่า
Doorknob

1

Pyth, 17 ไบต์

jmj\|cQ/lQdSh/lQ3

คำอธิบาย

     cQ/lQ         Divide into equal pieces (with the last shorter)
  j\|              Join with pipes
 m        d        Map to each row index...
           Sh/lQ3  ... up to the first row with substrings of length 2
j                  Join with newlines

1

Javascript, 98 ไบต์

a=>{for(b=1;2<=a.length/b;)eval("console.log(a.match(/.{1,"+(a.length/b|0)+"}/g).join('|'))"),b++}

x(a)ฟังก์ชัน โทรโดยใช้

console.log(x("ABCDEF"))



0

Python 3, 123 ไบต์

f=lambda s:print(*['|'.join(s[i:i+n]for i in range(0,len(s),n))for n in[len(s)//i for i in range(1,len(s)//2+1)]],sep='\n')

floor(l/n)ในตอนสายอีกต่อไปบางส่วนจะต้องทำซ้ำเป็นสูตรสำหรับความยาวของสตริงย่อยคือ ตัวอย่างเช่นกับสตริงที่มีความยาว 13 ตัวอักษรสตริงที่แยกออกเป็น 5 จะเหมือนกับสตริงที่แบ่งเป็น 6 floor(13/5)==floor(13/6)ตัว ฉันไม่แน่ใจว่าผู้ปฏิบัติการคาดหวังสิ่งนี้หรือไม่

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