깨진 문자열의 피라미드


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입니다.

  • 프로그램이 P 대 NP를 풀면 마이너스 100 %입니다.


리더 보드 :


0 바이트 :return: false
Gabriel Benamy

3
좋은 첫 도전! 몇 가지 설명이 필요한 질문-입력 만 인쇄 가능한 ASCII입니까? "가능한 경우 줄 바꿈이 필요하다"는 것은 무엇을 의미합니까?
AdmBorkBork

3
그것은 농담. P 대 NP는 컴퓨팅에서 해결되지 않은 문제입니다. 농담은 당신이 그것을 해결할 수 있다면, 당신의 프로그램이 도전을 해결하지 못한다는 사실에 대해서는 신경 쓰지 않을 것입니다.
Julian Lachniet

3
컴퓨팅에서 실제로 해결되지 않은 문제는 "탭 또는 스페이스"입니다.
FlipTack

3
실제 문제는 Internet Explorer입니다.
Julian Lachniet

답변:


0

자바 스크립트 (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하고 분할 문자열을 콘솔에 재귀 적으로 인쇄하는 Lambda . 매우 간단합니다. 부분 문자열 길이 p``가 1보다 크면 문자열을 '|'로 나눕니다. 모든 p문자를 입력 한 후 다음 레벨을 추가하십시오. 이 후, 다시 함수를 호출 p되는 t / n경우, 낭패 t일본어 문자열 길이이고 n증분되는 분배기.


n매번 2로 나누는 것이 맞다고 생각하지 않습니다 .
Neil

@ Neil 당신은 정확하고 내 실수입니다. 문제를 해결하고 프로세스에서 2 바이트를 저장했습니다.
XavCo7

@ETHproductions 나는 그것을 생각했지만 그것이 STDOUT으로 계산되는지 모르겠다 ... 나는 alert(f(s))바로 직후에 해야 할 것 같아 ?
XavCo7

4

펄, 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

자바 스크립트, 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

루비 60 + 1 = 61 바이트

-n플래그의 경우 +1 바이트

z= ~/$/
(z/3+1).times{|n|puts $_.scan(/.{1,#{z/(n+1)}}/)*?|}

Ideone에서 참조하십시오 : http://ideone.com/RtoReG


0

파이썬 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). OP가 이것을 예상했는지 아니면 그것이 감독인지 확실하지 않습니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.