문자열 "계단 화"


12

"계단 화 된"문자열을 작성하는 프로그램 또는 함수를 작성해야합니다. 문자열을 "계단 화"하는 방법은 다음과 같습니다.

문자열의 각 문자에 대해 :

  • 문자가 'y'를 포함하지 않는 대문자 또는 소문자 모음이면 출력 한 다음 나머지 문자열 위로 이동하십시오 .

  • 문자가 공백 또는 탭인 경우 출력하여 나머지 문자열을 열 아래 로 이동하십시오 .

  • 문자가 아닌 경우 정상적으로 출력합니다.

IO는 합리적인 형식 일 수 있습니다. 입력에는 개행이 포함되지 않습니다. 원하는 경우 후행 공백을 제거 할 수 있습니다.

문자열을 인쇄하지 않고 반환하기로 선택한 경우 문자열을 시각화하여 시각화 할 수있는 짧은 프로그램도 포함하십시오. 이것은 필수가 아니며 바이트 수를 향하지도 않습니다. 이것은 골프 나 esolangs (예 : 나와 같은)를 이해하지 못하는 사용자가 코드를 사용하여 출력 또는 땜질을 확인할 수있는 편리한 기능입니다.

샘플 IO :

"bcdef ghijkl"에 대한 출력 :

    f    jkl
bcde  ghi

"프로그래밍 퍼즐 및 코드 골프"에 대한 결과 :

                               lf
                            -Go
                  s  nd   de   
         ng   zzle  A   Co       
      mmi   Pu                 
   gra        
Pro

"Abcdefghijklmnopqrstuvwxyz"에 대한 출력 :

                     vwxyz
               pqrstu
         jklmno
     fghi          
 bcde             
A        

평소와 같이 이것은 코드 골프이므로 바이트 단위의 최단 답변이 이깁니다.



선행 / 후행 공백을 제거 할 수 있습니까?
orlp April

@orlp 시각적 표현을 전혀 바꾸지 않기 때문에 왜 그런지 모르겠습니다.
James

문자열을 반환하기로 선택한 경우 인쇄 프로그램이 바이트 수에 포함됩니까?

@PeterPeter 마지막 ​​편집을 참조하십시오.
James

답변:


2

MATL , 38 37 바이트

Oj33<G13Y2m-IL)hYstX<-"@Z"GX@)h]Xh!c!

온라인으로 사용해보십시오!

설명

각 문자에 대해 코드는 위에서 측정 한 수직 위치를 계산합니다 (0이 가장 높음). 그런 다음 출력 문자열을 바꿉니다. 각 문자는 세로 위치가 나타내는만큼 선행 공백이있는 줄에 있습니다. 그런 다음 모든 줄이 2D 문자 배열로 오염되어 최종적으로 바뀌어 표시됩니다.

O       % push a 0
j       % input a string
33<     % array of the same length as the input that contains true for spaces or tabs
G       % push input again
11Y2    % string 'aeiouAEIOU'
m       % array of the same length as the input that contains true for vowels
-       % subtract
IL)     % remove last element
h       % prepend the 0 that is at the bottom of the stack
Ys      % cumulative sum. This gives the vertical position of each char
tX<     % duplicate. Compute minimum
-       % subtract. This sets minimum vertical position to 0
"       % for each vertical position
  @     %   push vertical position of current character
  Z"    %   string with that many spaces
  G     %   push input again
  X@)   %   get the character corresponding to the current iteration index
  h     %   concatenate horizontally
]       % end for each
Xh      % concatenate all lines into a row cell array
!       % transpose into a column cell array
c       % convert into 2D array, padding with spaces if needed
!       % transpose. Implicitly display

7

Pyth, 63 바이트

V_Q aY?}rN0"aeiou"=hZ?}N"     "=tZZ;Jh.mbYKh.MZYjC.b++*d+JNY*dK_YQ
                         ^^^^^
                         |||||
                         |tabs
                        space

가운데의 공백은 실제로 단일 탭 문자이지만 StackExchange는 공백을 4 개의 공백으로 렌더링합니다.

온라인으로 사용해보십시오!


64 바이트를 셉니다.
Conor O'Brien

여기에 탭이 4 개의 공백으로 표시되기 때문입니다.
Leaky Nun

확실히 64 바이트. mothereff.in/…

아니요, @KennyLau는 탭 문자를 네 개의 공백 대신 배치해야 함을 의미했습니다. 온라인 사용해보기 링크를보십시오.
Mama Fun Roll

@MamaFunRoll StackExchange는 탭을 4 개의 공백으로 자동 대체합니다.
orlp

4

파이썬 2 141 137 바이트

def S(s,l=[0]):
 for c in s:l+=[l[-1]-(c in"aeiouAEIOU")+(c<"!")]
 for h in sorted(set(l)):print"".join([" ",c][i==h]for i,c in zip(l,s))

이것은 공간에서 내려 가지 않는 것 같습니다
Score_Under

@Score_Under 내 컴퓨터에서 제대로 작동합니다. Python 2에서 테스트하고 있습니까?
orlp

작동합니다. 어떻게해야할지 모르겠지만 처음 붙여 넣을 때 실수를했을 것입니다.
Score_Under

3

자바 스크립트 (Firefox 30-57), 151 바이트

s=>[...s].map((c,i)=>r[c<'!'?n++:/[AEIOU]/i.test(c)?n--:n][i]=c,n=s.length,r=[for(_ of s+s)[]])&&[for(a of r)if(s=[for(c of a)c||' '].join``)s].join`\n`

어디 \n리터럴 개행 문자를 나타냅니다.


2
템플릿 문자열을 사용하면 문자열에 줄 바꿈을 넣을 수 있으므로 /n`:
Generic User

1
@GenericUser 바이트 수는 이미 수행했다고 가정하여 조정합니다. 내 게시물에 리터럴 줄 바꿈을 사용하고 싶지 않았습니다.
Neil

1

C, 180 바이트

char s[99];i,j,p[99],m,M;main(c){for(gets(s);c=s[i];j+=strchr("aeiou",c|32)!=0,j-=c<33,m>j?m=j:M<j?M=j:0)p[i++]=j;for(;m<=M;putchar(10),M--)for(i=0;c=s[i];)putchar(M^p[i++]?32:c);}

언 골프 드 :

char s[99];i,j,p[99],m,M;
main(c){for(gets(s);c=s[i];
j+=strchr("aeiou",c|32)!=0,j-=c<33,m>j?m=j:M<j?M=j:0)
  //move current height up or down, adjust minimum and maximum height
p[i++]=j;  //record height of character
for(;m<=M;putchar(10),M--)  //from maximum to minimum height
for(i=0;c=s[i];)putchar(M^p[i++]?32:c);}  //print only characters on this height

1

Perl, 110 바이트 (108 바이트 스크립트 + 2 바이트 플래그)

$h=0;map{$h{$h}.=' 'x($p-$p{$h}).$_;$p{$h}=++$p;$h+=/[aeiou]/i-/\s/}split//;print for@h{sort{$b<=>$a}keys%h}

로 실행하고 perl -nl script.pl입력이 stdin에 있고 출력이 stdout에 있습니다.

난독 화

변수의 이름을 더 현명하게 바꾸고 코드 use strictuse warnings호환을 만들었으며 자동으로 많은 마술 펄을 명시 적으로 만들었습니다.

스크립트 내에서 플래그 perl script.pl의 효과를 복제하기 때문에 그냥로 실행됩니다 -nl.

use strict;
use warnings;
use English;

# The effect of -l in perl's flags
$INPUT_RECORD_SEPARATOR = "\n";
$OUTPUT_RECORD_SEPARATOR = "\n";

# These variables are magicked into existence
our $column = 0;
our %line_col = ();
our %lines = ();

# The implicit while-loop is the effect of -n in perl's flags
while (defined(my $line = <>)) {
    # The "chomp" is part of perl's -l flag too
    chomp $line;

    # Here starts the actual script. "$h=0" turns into...
    our $height = 0;
    for my $char (split '', $line) {
        if (!exists $line_col{$height}) {
            # Setting it to 0 is a bit of a white lie, but it might as well be 0.
            # Perl would otherwise have called the value "undef", which is
            # similar to 0 in numeric contexts.
            $line_col{$height} = 0;
        }

        $lines{$height} .= ' ' x ($column - $line_col{$height});
        $lines{$height} .= $char;

        $column++;
        $line_col{$height} = $column;

        $height++ if $char =~ /[aeiou]/i;
        $height-- if $char =~ /\s/;
    }

    # Sort line heights numerically descending (so the greatest is printed first)
    my @heights = sort { $b<=>$a } keys %lines;

    for my $line (@lines{ @heights }) {
        print $line;
    }
}

1

자바 스크립트 (ES6), 133

s=>s.replace(/[^aeiou ]*(.?)/gi,(z,x,c)=>(q=o[r]||'',o[r]=q+=' '.repeat(c-q.length)+z,x<'!'?++r:r?--r:o=[,...o]),o=[],r=0)&&o.join`
`

덜 골프

s=>(
  s.replace(/[^aeiou ]*(.?)/gi,(z,x,c)=>(
    q = o[r] || '',
    o[r] = q += ' '.repeat(c - q.length) + z,
    x == ' ' ? ++r : r ? --r : o = [,...o]
  ), o = [], r = 0),
  o.join`\n`
)

테스트

f=s=>s.replace(/[^aeiou ]*(.?)/gi,(z,x,c)=>(q=o[r]||'',o[r]=q+=' '.repeat(c-q.length)+z,x<'!'?++r:r?--r:o=[,...o]),o=[],r=0)&&o.join`
`

function test() {
  i=I.value
  O.textContent=f(i)
}

test()
#I { width:90%}
<input id=I oninput='test()' value='Programming Puzzles And Code-Golf'>
<pre id=O>


0

Haskell (ANSI 터미널 내), 75 바이트

("\27[2J"++).(h=<<)
h ' '="\27[B "
h c|elem c"aeiouAEIOU"=c:"\27[A"
h c=[c]

사용 예 : putStr $ ("\27[2J"++).(h=<<) $ "bcdef ghijkl"

이것은 ANSI 이스케이프 코드를 사용하여 커서를 위아래로 움직입니다.


0

C, 173 160 156 155 바이트

편집 : @ mIllIbyte에서 strchr을 사용하여 13 바이트를 깎는 것에 대한 차용 아이디어

편집 2 : 최소 / 최대 비교를 간소화, -4 바이트

Edit3 : c는 대신 -1로 시작하여 -1 바이트로 시작할 수있는 값을 가질 수 있습니다

Edit4 : 언 골프 / 설명 추가

p,l,j,m;main(c){char b[99],*s=gets(b);for(;j<m+2;p?putchar(c?l?32:c:10):l<j?j=l:l>m?m=l:0,l-=c?(c&=223)&&c-9?!!strchr("AEIOU",c):-1:(p=s=b,l+j++))c=*s++;}

언 골프하고 설명 :

/* declare and initialize these variables to int and 0 */
p,l,j,m;

/* declares main, but also int c */
main(c)
{

  /* we can handle strings of length 98 (+1 for string-terminating 0) */
  /* we declare and initialize s to point to the beginning of the input
     string for the first pass through the for loop */
  char b[99],*s=gets(b);

  /* the for-loop actually contains nested loops, where the inner loops
     behave differently depending on the outer loop parameter p as follows:
     p attains the values false (0) and true (non-null pointer), in this order.

     p == false:
      the inner loop has the parameter s and passes through all the characters
      in the string until the string is exhausted (*s == 0). l is the vertical
      position of the current character relative to the first character
      (l = 0), smaller number = higher up. The purpose here is simply to find
      the range of vertical positions [j, m] present in the string. The
      commands in execution order are:

      -- loop over s --

      // test does not do anything since j <= m by design
      1. j < m+2

      // puts current char in c and increments string counter
      2. c = *s++          

      // ensures that j (m) equals the min (max) of the vertical positions (l)
         encountered so far. At first step j = l = m = 0.
      3. l<j?j=l:l>m?m=l:0 

      // c != 0, this updates the vertical position for the next character
      // c = SPC or C = TAB -> lower (l increases by 1)
      // c = "aeiouAEIOU" -> higher (l decreases by 1)
      4a. l-=(c&=223)&&c-9?!!strchr("AEIOU",c):-1

      -- loop over s ends --

      // c == 0, this resets the string pointer s and puts p = true, and 
      //         thereby initiates the next phase of the algorithm
      //         see rest of the explanation at p == true)
      4b. p=s=b

    p == true:
     now there are two inner loops. The outer of these has the parameter j,
     which ranges from the smallest vertical position+1 (the value of j after
     the p == false pass) to the largest vertical position+1 (m+2 after the
     p == true pass). The innermost loop has the parameter s and passes through
     all characters in the string until the string is exhausted (*s == 0) just
     as in the p == false inner loop. Here l is now the vertical position
     relative to the current position j-1, so that l == 0 when a character is
     at the current level. Such characters are printed as is, whereas
     characters at other levels are replaced by space. The end-of-string
     marker 0 outputs a newline. The commands in execution order are:

      -- loop over j --

      // at first step increments j to point to be one more than the
      // current vertical position. At other steps moves the current position
      // (j-1) one vertical position downwards. Also, at all steps, this
      // biases the vertical position counter l to be zero at the current
      // vertical position (j-1)
      1. l=-j++

      // compare j to stopping criteria, exit if j > m+1
      2. j < m+2

       -- loop over s --

       // puts current char in c and increments string counter
       3. c = *s++          

       // outputs character as follows:
       // c == 0 (end of string), output newline
       // c != 0 (middle of string)
       //  l == 0 (character at current vertcial position), output c
       //  l != 0 (character not at current vertical position), output space
       4. putchar(c?l?32:c:10)

       // c != 0, this updates the vertical position for the next character
       // c = SPC or C = TAB -> lower (l increases by 1)
       // c = "aeiouAEIOU" -> higher (l decreases by 1)
       5a. l-=(c&=223)&&c-9?!!strchr("AEIOU",c):-1

       -- loop over s ends --

      // c == 0, this resets the string pointer s for next loop over s
      //         algorithm (see rest of the explanation at p == true)
      5b. p=s=b

     -- loop over j ends --
  */

  for(;
      j<m+2;
      p?putchar(c?l?32:c:10):
    l<j?j=l:l>m?m=l:0,
      l-=c?(c&=223)&&c-9?!!strchr("AEIOU",c):-1:
       (p=s=b,l+j++))
    c=*s++;
}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.