ย้อนกลับสตริงโดยชิ้น


34

งานของคุณคือการเขียนโปรแกรมที่ให้ตัวเลขและสตริงแยกสตริงออกเป็นส่วน ๆ ของขนาดนั้นและย้อนกลับ

กฎระเบียบ

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

กรณีทดสอบ

n   s           Output

2   abcdefgh    ghefcdab
3   foobarbaz   bazbarfoo
3   abcdefgh    ghdefabc
2   a           a
1   abcdefgh    hgfedcba
2   aaaaaa      aaaaaa
2   baaaab      abaaba
50  abcdefgh    abcdefgh
6   abcdefghi   ghiabcdef

นี่คือดังนั้นคุณควรตั้งเป้าหมายให้น้อยที่สุดเท่าที่จะเป็นไปได้


ที่เกี่ยวข้อง
AdmBorkBork

คำตอบ:


29

เยลลี่ 2 ไบต์

sṚ

โปรแกรมเต็มรูปแบบที่พิมพ์ผลลัพธ์

ลองออนไลน์!

อย่างไร?

sṚ - Main link: string, number                                   e.g. 'abcdefg', 3
s  - split string into chunks of length number (keeping any overflow) ["abc","def","g"]
 Ṛ - reverse the resulting list                                       ["g","def","abc"]
   - implicit print                                                   gdefabc

28
ฉันชอบวิธีที่สองไบต์สร้างคำอธิบาย 4 บรรทัด
Pavel



8

JavaScript (ES6), 37 ไบต์

n=>F=s=>s&&F(s.slice(n))+s.slice(0,n)

จะเข้าโดย currying: f(2)("abcdefgh")หมายเลขแรกแล้วสตริงเช่น






4

Röda , 36 ไบต์

f n{[[_]..[try head n-1]]|reverse|_}

ลองออนไลน์!

มันเป็นฟังก์ชั่นที่รับอาร์กิวเมนต์หนึ่งตัว อักขระของสตริงต้องอยู่ในสตรีม

tryใช้เพื่อทิ้งข้อผิดพลาดในกรณีที่headฟังก์ชั่นไม่สามารถอ่านn-1ค่า

คำอธิบาย:

f n{[[_]..[try head n-1]]|reverse|_}
f n{                               } /* Function declaration */
                                     /* In a loop: */
      _                              /*   Pull one value */
           try head n-1              /*   Pull n-1 values (or less) */
     [ ]..[            ]             /*   Make an array */
    [                   ]            /*   Push it to the stream */
                         |reverse    /* Reverse all values in the stream */
                                 |_  /* Flat all arrays in the stream */
                                     /* Characters in the stream are printed */

ไม่งงเหมือนปกติ ฉันคิดว่ามันค่อนข้างสวยงาม :)


5
คุณจัดการเพื่อให้โปรแกรมอ่านได้น้อยกว่าโซลูชันเยลลี่
Pavel

ทำไมไม่[[try head n]]ทำงานแทน[[_]..[try head n-1]]?
Kritixi Lithos

@KritixiLithos เพราะ_ลูปนิพจน์ [[try head n]]จะใช้ค่า n ครั้งเดียวแต่[[_]..[try head n-1]]รับค่า n ตราบใดที่ยังมีค่าเหลืออยู่
fergusq

4

CJam , 5 ไบต์

q~/W%

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

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบทั้งหมด

คำอธิบาย

q~   e# Read all input and evaluate: pushes a number and a string
/    e# Split string into chunks of that size. Last chunk may be
     e# smaller. Gives an array of strings
W%   e# Reverse the array. Implicitly display

4

แบตช์ 74 ไบต์

@if %2=="" (echo %~3)else set s=%~2&call %0 %1 "%%s:~%1%%" "%%s:~,%1%%%~3"

ค่อนข้างหงุดหงิดนี้จบลงด้วยการเรียกซ้ำมากกว่าหางซ้ำ


4

V , 13 10 ไบต์

òÀ|lDÏpòÍî

ลองออนไลน์!

ò      ò    ' Recursively
 À|         ' Go to the "nth" column
   l        ' Move one character right (breaks loop when no more chunks)
    D       ' Delete from here to the end of the line
     Ï      ' Add a line above the current line (now contains one chunk)
      p     ' Paste the remainder of the line that was deleted
        Íî  ' Remove all newlines

ในการดำเนินการ:

abcdefghijkl

กลายเป็น

efghijkl
abcd

ซึ่งกลายเป็น

ijkl
efgh
abcd

ก่อนที่จะขึ้นบรรทัดใหม่ทั้งหมด


4

brainfuck , 78 ไบต์

,<<<+[[>]>+>[[>],<[<]>+>-]<-[->>[>]>>+<<<[<]<]>>]<<<<[[<]>[-[+.[-]]+>]<[<]<<<]

ไบต์แรกของอินพุตคือขนาดก้อนที่กำหนดโดยค่าไบต์ ไบต์ที่เหลือถือว่าเป็นสตริง

ลองออนไลน์!

ขยายตัวและแสดงความคิดเห็น

Read the chunk size byte
This cell will become a counter cell
,

Move left a few cells an increment; 
this is to make the starting position 
line up with the relative positioning
needed to fit in with the loop
<<<+

While the current cell is nonzero:
[

 Move right to the first zero cell
 [>]

 Move right once and increment and then move right to the counter cell
 The increment is required because of "move to zero cell" loops
 >+>

 This loop will store one chunk of the input in consecutive memory cells
 [
  [>]   Move right until a zero cell is hit
  ,     Store 1 byte of input there
  <[<]  Move back left until a zero cell (other than the current one) is hit
  >+>-  Increment the temporary cell by 1 and decrement the counter
 ] (end loop once the counter hits zero)

 Decrement the temp cell (because we needed to have 1 there initially to make the cell location work)
 <-

 Move the temp cell to three cells after the end of the chunk
 This is the new counter cell for the next chunk
 [->>[>]>>+<<<[<]<]

 Move two cells right from where the temp cell was
 This is the first cell of the chunk; if it's 0
 then the input is finished and the loop should end
 >>
]

Due to the way the counter is kept track of the tape head
will always be four cells to the right of the last input cell
when the loops breaks
<<<<

Now the chunks are printed one by one
At the start of an iteration the tape head is at the end of a chunk
[
 Locate the start of the last chunk
 [<]>

 Print the chunk:
 [
  Print the byte held in the current cell if it isn't 1
  This is necessary because we left a stray 1 in a cell at
  the start which shouldn't be printed
  -[+.[-]]+

  Move to the next cell
  >
 ]

 Move to just left of the chunk
 <[<]

 Move three cells over to the end of the next chunk
 <<<
]

4

PowerShell, 56 49 ไบต์

-7 bytes ขอบคุณที่mazzy

param($n,$s)$s-split"(.{$n})"-ne''|%{$r=$_+$r};$r

ลองออนไลน์!


1) 49 ไบต์ 2) โปรดโพสต์โปรแกรมแบบเต็มไม่ใช่โค้ดนิพจน์ วิธีตรวจสอบ แยกรหัสของคุณในไฟล์แยกต่างหากด้วยนามสกุล.ps1และลองเรียกใช้สคริปต์นี้แทนรหัสของคุณ หากใช้งานได้ผลการทดสอบก็สำเร็จ
mazzy

3

Mathematica ขนาด 46 ไบต์

""<>Reverse@Partition[Characters@#2,#,#,1,{}]&

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


3

Javascript - 54 47 46 bytes

เหล็กไหล:

(s,n)=>s.match(eval(`/.{1,${n}}/g`)).reverse()

ใช้เป็น

f=(s,n)=>s.match(eval(`/.{1,${n}}/g`)).reverse()
alert(f("abcdefgh",2));

ขอบคุณ @ETHproductions สำหรับการเร่งด่วน RegEx บางอย่างขอขอบคุณ @Shaggy สำหรับไบต์พิเศษในการทดสอบ!

เดิม:

(s,n)=>s.match(new RegExp('.{1,'+n+'}','g')).reverse()

1
คำตอบที่ดี! ฉันเชื่อว่าคุณสามารถบันทึกสองสามไบต์โดยสร้าง regex ด้วยeval('/.{1,'+n+'}/g')
ETHproductions

@ETHproductions อ่าใช่ นั่นคือสิ่งที่ฉันพยายามทำ ฉันไม่คุ้นเคยเพียงพอกับ regex ที่จะทำ!
Blue Okiris

ฉันคิดว่าคุณสามารถบันทึกไบต์ด้วยการแกงs=>n=> ...
Pavel

บันทึกไบต์ด้วยeval("/.{1,${n}}/g")โดยใช้ backticks แทนเครื่องหมายคำพูด
Shaggy


3

เรติน่า , 38 ไบต์

บันทึก 1 ไบต์ขอบคุณ @LeakyNun

^

+`(.* (1)+¶)((?<-2>.)+)
$3$1
 1+¶

(หมายเหตุช่องว่างในบรรทัดที่สองและพื้นที่ต่อท้าย)

โปรแกรมนี้รับอินพุตเป็นเอกภาพในบรรทัดแรกและสตริงที่สอง

ลองออนไลน์!

ชุดทดสอบ! (แก้ไขเล็กน้อย)

คำอธิบาย

ขั้นตอนแรกคือการเพิ่มช่องว่าง (จะกลายเป็นสิ่งสำคัญในภายหลัง)

^
 

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

+`(.* (1)+¶)                       Capture the unary number in group 2
             ((?<-2>.)+)           Balancing group for substrings
$3$1                               Reverse

และในที่สุดก็ลบหมายเลข unary และขึ้นบรรทัดใหม่

 1+¶


ฉันคิดว่ามันเป็นที่ยอมรับได้ที่จะใช้หมายเลขเป็นเอกภาพ
Leun Nun

อย่างไรก็ตามคุณสามารถแทนที่\dด้วย.เพื่อบันทึกไบต์
Leun Nun

ประการที่สอง^ยังซ้ำซ้อน
Leun Nun

@LeakyNun ตอนนี้โปรแกรมรับอินพุตเป็นเอกดังนั้นฉันไม่จำเป็นต้องทำ\dอีกต่อไป และขอขอบคุณที่เล่นกอล์ฟ
คาเร็ต

33 ไบต์โดยใช้การจับคู่ขี้เกียจ (ไม่โลภ)
Leun Nun

3

Java, 147 138 ไบต์

String r(String s,int n){String r="";int l=s.length();for(int i=l/n*n;i>=0;i-=n)if(!(i>=l))r+=(i+n)>=l?s.substring(i):s.substring(i,i+n);return r;}

บันทึกแล้ว 9 ไบต์ขอบคุณ Kevin Cruijssen!

String r(String s,int n){String r="";int l=s.length(),i=l/n*n;for(;i>=0;i-=n)if(i<l)r+=i+n>=l?s.substring(i):s.substring(i,i+n);return r;}

ในรูปแบบขยาย:

String r(String s,int n){
    String r="";
    int l=s.length(),i=l/n*n;
    for(;i>=0;i-=n)
        if(i<l)
            r+=i+n>=l?s.substring(i):s.substring(i,i+n);
    return r;
}

นี่เป็นความพยายามครั้งแรกของฉันที่จะ codegolf ดังนั้นข้อเสนอแนะใด ๆ ยินดีต้อนรับ!


ยินดีต้อนรับสู่ PPCG!
Pavel

1
สวัสดียินดีต้อนรับสู่ PPCG! นี่เป็นสิ่งที่ค่อนข้างดีอยู่แล้ว แต่ยังมีบางสิ่งที่ต้องตีกอล์ฟอีก: int l=s.length();for(int i=l/n*n;สามารถint l=s.length(),i=l/n*n;for(;ให้คุณมีเพียงint ครั้งเดียว และสามารถif(!(i>=l)) if(l<i)และสามารถเป็นได้โดยไม่ต้องวงเล็บ:r+=(i+n)>=l? r+=i+n>=l?นอกจากนี้หากคุณยังไม่เคยเห็นฉันสามารถแนะนำให้ลองดูเคล็ดลับสำหรับการเล่นกอล์ฟใน Javaสำหรับเคล็ดลับการเล่นกอล์ฟที่น่าสนใจ ยินดีต้อนรับอีกครั้ง :)
Kevin Cruijssen

3

Perl 5 , 25 ไบต์

ใช้-lnM5.010ธง

say reverse<>=~/.{1,$_}/g

ลองออนไลน์!

ตะโกนไปที่ Grinnz เพื่อบอกฉันเกี่ยวกับ =~ m/.{1,$n}/g

-M5.010ช่วยให้การใช้sayฟังก์ชั่นซึ่งสำหรับวัตถุประสงค์ของเราพิมพ์ด้วยชื่อที่สั้นกว่า

-nใส่บรรทัดแรกของการป้อนข้อมูล$_และ-lchomps ปิดบรรทัดใหม่ต่อท้าย

จากนั้นเราจะได้รับบรรทัดที่สองของการป้อนข้อมูลที่ใช้<>และนำไปใช้กับ regex .{1,$_}: ตัวละครใด ๆ ระหว่าง 1 ถึง $ _ (อินพุตแรก) ครั้ง เนื่องจากนี่เป็นโลภโดยค่าเริ่มต้นจึงพยายามจับคู่ $ _ ตัวอักษรเสมอ 1,เป็นสิ่งจำเป็นสำหรับก้อนที่เหลือไปได้ในตอนท้าย

โมดิ/gฟายเออร์ให้เราทุกการจับคู่ของ regex นั้นในสตริงอินพุตเป็นรายการซึ่งจะถูกย้อนกลับและพิมพ์ ใน Perl ให้ส่งรายการเพื่อsayรวมโดยไม่มีตัวคั่นใด ๆ เป็นค่าเริ่มต้น


3

Dyalog APL Extended , 16 15 ไบต์

{∊⌽⍵⊂⍨(≢⍵)⍴=⍳⍺}

ลองออนไลน์!


คุณไม่จำเป็นต้องf←
Pavel

ทำไม,/
อดัม

@ Adámโอ้เฮ้ที่ใช้กับคำตอบของฉันด้วยขอบคุณ
Pavel

Pavel: ใช่แน่นอน .. @ Adámขอบคุณ!
dzaima

14:∊∘⌽⊢⊂⍨≢⍤⊢⍴1↑⍨⊣
Ven



2

QBICขนาด 24 ไบต์

:;[1,_lA|,a|Z=_sA,b,a|+Z

สิ่งนี้ทำให้การใช้งานที่ยอดเยี่ยมของฟังก์ชั่นย่อยใหม่ที่ฉันเพิ่งเพิ่มไปยัง QBIC:

:;          Read in the cmd line params a (number) and A$ (text)
[1,_lA|,a|  Set up a FOR loop: FOR b = 1; b <= A$.length; b += a
Z=          Modify Z$; Z$ is autoprinted at the end of QBIC code
_sA,b,a|    SUBSTRING: _s is the function followed by the string 
               to take from, the starting pos and the # of chars
+Z          Take chunks from further into A$, put them before Z$



2

C, 69 ไบต์

i;f(s,n)char*s;{i=strlen(s);for(i-=i%n;printf("%.*s",n,s+i),i;i-=n);}

ผลลัพธ์จะถูกพิมพ์ออกไปยังเอาต์พุตมาตรฐาน


2

สกาลา, 57 55 ไบต์

(n:Int,s:String)=>(""/:s.grouped(n).toSeq.reverse)(_+_)

ขอบคุณยาโคบ! ลองมันนี่

หมายเหตุ: ด้วยการใช้รูปแบบสัญลักษณ์ของ foldLeft ("/:") ฉันสามารถถอดไบต์ได้อีกสองสามไบต์


ทำให้ฟังก์ชั่นที่ไม่ระบุชื่อและใช้mkStringแทนreduceLeftและโกนออก 7 bytes:(n:Int,s:String)=>s.grouped(n).toSeq.reverse.mkString("")
Jacob


2

R , 69 60 ไบต์

function(s,n)cat(substring(s,(x=nchar(s):0*n)+1,x+n),sep="")

ลองออนไลน์!

ขอขอบคุณที่คิริลล์ลิตรseqสำหรับข้อเสนอแนะในการลบ


ดูเหมือนว่างานนี้จะเกินไปสำหรับ 66
Kirill L.

@KirillL เราสามารถไปถึง 60 ไบต์ถ้าเรากลับคำสั่งของการขัดแย้งการและการจัดการบางอย่างจะช่วยให้เราได้รับการกำจัดต่อท้าย: -1
Giuseppe

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