“ Stair-ify” สตริง


12

คุณต้องเขียนโปรแกรมหรือฟังก์ชั่นที่สร้างสตริง "stair-ified" นี่คือวิธีที่คุณ "stair-ify" สตริง:

สำหรับอักขระแต่ละตัวในสตริง:

  • หากตัวละครเป็นสระเสียงตัวใหญ่หรือตัวเล็กไม่รวม '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        

ตามปกตินี่คือ code-golf ดังนั้นคำตอบที่สั้นที่สุดในหน่วยไบต์ชนะ


ที่เกี่ยวข้อง: codegolf.stackexchange.com/q/55593/31716
James

เราขอตัดช่องว่างนำหน้า / ต่อท้ายได้ไหม?
orlp

@ หรือไม่เพราะมันจะไม่เปลี่ยนการแสดงภาพเลยฉันไม่เห็นว่าทำไม
James

หากเราเลือกที่จะส่งคืนสตริงโปรแกรมที่จะพิมพ์จะรวมอยู่ในจำนวนไบต์หรือไม่

@PeterPeter ดูการแก้ไขล่าสุดของฉัน
James

คำตอบ:


2

MATL , 38 37 ไบต์

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

ลองออนไลน์!

คำอธิบาย

สำหรับอักขระแต่ละตัวรหัสจะคำนวณตำแหน่งแนวตั้งโดยวัดจากด้านบน (0 คือสูงสุด) จากนั้นสร้างสตริงเอาต์พุต transposed: อักขระแต่ละตัวจะอยู่บนบรรทัดที่มีช่องว่างนำหน้าตามที่ระบุในแนวตั้ง จากนั้นทุกบรรทัดจะถูกรวมเข้าด้วยกันเป็นอาร์เรย์ถ่าน 2 มิติซึ่งในที่สุดจะถูกเปลี่ยนและแสดงผล

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 วาทกรรมดังกล่าวเป็นช่องว่างสี่

ลองออนไลน์!


ฉันนับ 64 ไบต์
Conor O'Brien

เนื่องจากมีแท็บที่แสดงเป็นช่องว่างสี่ที่นี่
แม่ชีที่รั่ว

อย่างแน่นอน 64 ไบต์ mothereff.in/…

ไม่ @KennyLau หมายความว่าควรวางอักขระแท็บแทนที่ช่องว่างทั้งสี่ ดูที่ลิงค์ลองออนไลน์
Mama Fun Roll

@MamaFunRoll StackExchange จะแทนที่แท็บโดยอัตโนมัติด้วยช่องว่าง 4 ช่อง
orlp

4

Python 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 มันทำงานได้ดีบนเครื่องของฉัน คุณกำลังทดสอบ Python 2 หรือไม่
orlp

มันใช้งานได้ดี ฉันไม่รู้ว่าจะทำอย่างไร แต่ฉันต้องทำผิดพลาดเมื่อวางมันเป็นครั้งแรก
Score_Under

3

JavaScript (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ด้วย ``
ผู้ใช้ทั่วไป

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);}

Ungolfed:

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

Deobfuscated

ฉันได้เปลี่ยนชื่อตัวแปรอย่างสมเหตุสมผลมากขึ้นสร้างรหัสuse strictและuse warningsสอดคล้องและทำให้ชัดเจนมาก perl เวทมนตร์ทำโดยอัตโนมัติ

นี่เป็นเพียงการทำงาน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

JavaScript (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`
`

น้อย golfed

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 ไบต์

แก้ไข: แนวคิดที่ยืมมาจากการใช้ strchr จาก @mIllIbyte เพื่อกำจัด 13 ไบต์

แก้ไข 2: ปรับปรุงการเปรียบเทียบขั้นต่ำ / สูงสุด -4 ไบต์

Edit3: c สามารถมีค่าใด ๆ ที่ขึ้นต้นด้วย -> เป็น main (c) แทน, -1 byte

แก้ไข 4: เพิ่มคำไม่สุภาพ / คำอธิบาย

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++;}

Ungolfed และอธิบาย:

/* 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.