ทำซ้ำองค์ประกอบ Nth


18

เราไม่ได้มีคำถามเกี่ยวกับสักระยะหนึ่ง (เพื่อให้แม่นยำ 5 วัน) ดังนั้นไปกันเลย

กำหนดสตริงsและจำนวนเต็มบวกnใช้ทุกnองค์ประกอบของ TH s, ทำซ้ำครั้งและนำมันกลับเข้ามาns

ตัวอย่างเช่นถ้าn = 3และทุกตัวละครที่สามคือs = "Hello, World!" Hl r!แล้วคุณทำซ้ำตัวละครแต่ละตัวครั้งเพื่อการผลิตn HHHlll rrr!!!จากนั้นคุณแทนที่ตัวอักษรต้นฉบับด้วยเวอร์ชันที่ทำซ้ำเพื่อผลิตผลิตภัณฑ์ขั้นสุดท้ายของHHHellllo, Worrrld!!!

คุณต้องทำภารกิจนี้ให้สำเร็จด้วยรหัสที่สั้นที่สุดที่เป็นไปได้ในภาษาของคุณ!

กฎระเบียบ

  • นี่คือเพื่อให้โค้ดที่สั้นที่สุดเป็นไบต์ชนะ
  • nมีการรับประกันว่าจะมีขนาดเล็กกว่าความยาวsและมากกว่า 0
  • อักขระตัวแรกsคือตำแหน่งที่ใช้nอักขระ th และนำมาใช้ซ้ำหลายnครั้ง
  • sจะประกอบด้วย ASCII ที่พิมพ์ได้เท่านั้น (รหัสชี้0x20 (space)ไปที่0x7E (~))

กรณีทดสอบ

s, n => output

"Hello, World!", 3 => "HHHellllo,   Worrrld!!!"
"Code golf", 1 => "Code golf"
"abcdefghijklm", 10 => "aaaaaaaaaabcdefghijkkkkkkkkkklm"
"tesTing", 6 => "ttttttesTingggggg"
"very very very long string for you to really make sure that your program works", 4 => "vvvvery    veryyyy verrrry loooong sssstrinnnng foooor yoooou toooo reaaaally    makeeee surrrre thhhhat yyyyour    proggggram    workkkks"

เราสามารถรับอินพุตsเป็นอาร์เรย์อักขระได้หรือไม่
Kevin Cruijssen

2
" และใส่กลับเข้าไปในs " <- นี่เป็นข้อกำหนดที่เข้มงวด (เขียนทับสตริงเดิม) หรือไม่ก็แค่เอาท์พุทผลสุดท้าย
เฟลิกซ์ Palmen

@KevinCruijssen ใช่คุณทำได้
caird coinheringaahing

1
@ FelixPalmen นั่นเป็นเพียงวิธีที่ฉันอธิบาย คุณสามารถใช้วิธีการใดก็ได้ที่คุณต้องการ
caird coinheringaahing

@cairdcoinheringaahing ดีขอบคุณได้ทำไปแล้ว
เฟลิกซ์ Palmen

คำตอบ:


10

เยลลี่ 3 ไบต์

Ḣs×

การป้อนข้อมูลจะมาเป็นs, n

ลองออนไลน์!

มันทำงานอย่างไร

Ḣs×  Main link. Argument: s, n

Ḣ    Head; yield s.
     This pops the list, leaving [n] as the main link's argument.
 s   Split s into chunks of length n.
  ×  Multiply each chunk by [n], repeating its first element n times.

จริง ๆ แล้วมันเป็น 6 ไบต์ในการเข้ารหัส UTF-8 ใช่ไหม E1 B8 A2 73 C3 97
CoDEmanX

5
ใน UTF-8 ใช่ อย่างไรก็ตามเจลลี่ใช้หน้ารหัสที่กำหนดเองซึ่งอักขระแต่ละตัวที่เข้าใจจะใช้เวลาเพียงไบต์เดียว
Dennis

7

เยลลี่ ,  6  5 ไบต์

-1 ไบต์ขอบคุณแม่ชีที่รั่ว (ใช้การคูณสตริงของงูใหญ่)

×Jm¥¦

โปรแกรมเต็มรูปแบบยอมรับสองอาร์กิวเมนต์บรรทัดคำสั่งสตริงและหมายเลขและพิมพ์ผลลัพธ์

ลองออนไลน์!

อย่างไร?

×Jm¥¦ - Main link: list of characters, s; number, n   e.g. ['g','o','l','f','e','r'], 2
    ¦ - sparse application:
   ¥  - ...to indices: last two links as a dyad:
 J    -      range of length of s                          [1,2,3,4,5,6]
  m   -      modulo slicing by n (every nth entry)         [1,3,5]
×    - ...action: multiply  ["gg",'o',"ll",'f',"ee",'r']
      - implicit print                                 >>> ggollfeer


อ๋อพยายามxลืม×; ขอบคุณ
Jonathan Allan

จริง ๆ แล้วมัน 8 ไบต์ในการเข้ารหัส UTF-8 ใช่ไหม C3 97 4A 6D C2 A5 C2 A6
CoDEmanX


@ MDXF ขอบคุณสำหรับการฟีดดิง!
Jonathan Allan

4

JavaScript (ES6), 46 ไบต์

(s)(n)จะเข้าในไวยากรณ์ currying

s=>n=>s.replace(/./g,(c,i)=>c.repeat(i%n?1:n))

กรณีทดสอบ



3

C # (. NET Core) , 84 82 ไบต์

n=>m=>{var s="";for(int i=0;i<n.Length;)s+=new string(n[i],i++%m<1?m:1);return s;}

ลองออนไลน์!


คุณสามารถบันทึกไบต์โดยการลบi++และการเปลี่ยนแปลงไปn[i],i%m<1?m:1 n[i],i++%m<1?m:1
Kevin Cruijssen

คุณสามารถบันทึกไบต์อื่นได้ด้วยการปิดอินพุต:n=>m=>...
raznagul

3

05AB1E , 8 7 ไบต์

-1 ไบต์ขอบคุณ @Emigna

ôʒć²×ì?

ลองออนไลน์!

คำอธิบาย

ôʒć²×ì?    Arguments s, n  ("Hello, World!", 3)
ô          Split s into pieces of n  (["Hel", "lo,", ...])
 ʒ         Filter (used as foreach)
  ć          Extract head  ("Hel" -> "el", "H" ...)
   ²×ì       Repeat n times and prepend  ("el", "H" -> "HHHel" ...)
      ?      Print without newline

บันทึกไบต์ด้วยôʒć²×ì?
Emigna

@Emigna ขอบคุณฉันรู้ว่าต้องมีวิธีที่จะกำจัดการปิด}
kalsowerus

การใช้ฟิลเตอร์แปลก ๆ เมื่อมันไม่ได้ใช้ผลลัพธ์ แต่จริงๆแล้วมันสร้างความแตกต่าง ...
Magic Octopus Urn

@MagicOctopusUrn, yep filter ยังคงเป็น foreach ที่ดีกว่าใน 05AB1E
kalsowerus

@kalsowerus vyเป็น foreach หนึ่งεเป็นอีกอันหนึ่ง ผิดปกติพอεไม่ทำงาน
Magic Octopus Urn

2

PowerShell , 51 ไบต์

param($a,$n)-join($a|%{($_,("$_"*$n))[!($i++%$n)]})

ลองออนไลน์!

จะเข้าเป็นchar-array และจำนวน$a วนซ้ำ$nไปมา$aและการวนซ้ำแต่ละครั้งจะเอาท์พุทตัวอักษรปัจจุบัน$_หรือตัวอักษรปัจจุบันคูณด้วยโดย$nอิงดัชนีเข้าสู่แบบหลอก เลือกดัชนีระหว่างสองตามออกของที่เพิ่มขึ้น$iและจากนั้น $nmodulo จากนั้นตัวอักษรเหล่านั้นจะถูกนำ-joinกลับมารวมกันและสตริงจะถูกวางไว้บนไพพ์ไลน์ เอาท์พุทเป็นนัย



2

อลิซ 25 ไบต์

/
KI /!Iw?&.?t&O?&wWOI.h%

ลองออนไลน์!

คำอธิบาย

/         Switch to Ordinal.
I         Read first line of input (i.e. n).
/         Switch to Cardinal.
!         Convert input to its integer value and store it on the tape.
I         Read first character from input string.
w         Push current IP address onto the return address stack. This
          effectively marks the beginning of the main loop.

  ?         Retrieve n.
  &.        Duplicate current character n times (once more than we need,
            but who cares about a clean stack...).
  ?t        Retrieve n and decrement.
  &O        Output n-1 copies of the current character.
  ?         Retrieve n.
  &w        Push the current IP address onto the return address stack n
            times. This marks the beginning of a loop that is executed n 
            times.

    W         Discard one copy of the return address from the stack,
              effectively decrementing the loop counter.
    O         Output the last character. On the first iteration, this is
              the final copy of the repeated character, otherwise it's just
              the single character we read on the last iteration.
    I         Read a character for the next iteration.
    .h%       Compute c % (c+1) on that character, which is a no-op for
              for valid characters, but terminates the program at EOF when
              c becomes -1.

K         Jump to the address on top of the return address stack. As long
          as there are still copies of the address from the inner loop, we
          perform another iteration of that, otherwise we jump back to the
          beginning of the outer loop.

2

R , 82 76 75 ไบต์

function(s,n)cat(rep(S<-el(strsplit(s,'')),c(n,rep(1,n-1))+!seq(S)),sep='')

ลองออนไลน์!

ฟังก์ชั่น; ใช้สตริงsและจำนวนเต็มnและพิมพ์เวอร์ชันซ้ำไปยัง stdout

คำอธิบาย:

function(s,n){
 S <- el(strsplit(s,""))                  # characters
 r     <- c(n,rep(1,n-1))                 # [n, 1, 1,...,1], length n
 repeats <- r+!seq(S)                     # extends R to length of S
 cat(rep(S, repeats), sep="")             # print out
}

R , 55 ไบต์

function(S,n)cat(rep(S,c(n,rep(1,n-1))+!seq(S)),sep="")

ลองออนไลน์!

อัลกอริทึมเหมือนข้างต้น แต่มีSรายการของตัวละครแต่ละตัว


1

Python 2 , 57 ไบต์

lambda s,n:''.join(c*[1,n][i%n<1]for i,c in enumerate(s))

ลองออนไลน์!


การจัดทำดัชนีในสตริงแทนที่จะใช้enumerateสั้นลงหรือไม่
caird coinheringaahing

@cairdcoinheringaahing ฉันจะต้องใช้range(len())ในท้ายที่สุดจะนานขึ้น
Rod



1

Japtet , 8 ไบต์

ËùDV*EvV

ทดสอบออนไลน์!

คำอธิบาย

 Ë    ùDV*EvV
UmDE{DùDV*EvV}   Ungolfed
                 Implicit: U = s, V = n
UmDE{        }   Replace each char D and (0-)index E in U by this function:
          EvV      Take 1 if the index is divisible by V; 0 otherwise.
        V*         Multiply this by V. This gives V for every Vth index; 0 for others.
     DùD           Pad D with itself to this length. This gives V copies of D for every
                   Vth index; 1 copy of D for others.
                 Implicit: output last expression

ผมต้องให้เครดิตคิดที่จะใช้ùในการ@Shaggyคำตอบของที่นี่ ฉันไม่รู้ว่าฉันจะคิดถึงมันเอง ...


ตอนนี้คุณเห็นแล้วว่าทำไมจึงมีความกระตือรือร้นที่จะเห็นการเพิ่มสายอักขระเข้าไป :) การแก้ปัญหาที่ดี ฉันพยายามหาบางสิ่งเพื่อทำงานร่วมกับëคนโง่ & หัวเราะคิกคัก แต่ล้มเหลวอย่างน่าสังเวช!
Shaggy

1

J, 17 ไบต์

(#@]$[,1#~<:@[)#]
  • (...) # ]ทุกอย่างใน parens สร้างสตริงสำหรับ J's ที่สร้างขึ้นในคำกริยา "copy" ดังนั้นเช่นถ้าอาร์กิวเมนต์ซ้ายคือ 3 มันจะสร้างสตริง3 1 1ซ้ำตามที่จำเป็นเพื่อให้เท่ากับจำนวนตัวอักษรใน ARG ขวา]ซึ่งมีสตริง ซึ่งก็คือการพูด#แก้ปัญหาโดยตรงโดยสมมติว่าเราสามารถให้อาร์กิวเมนต์ซ้ายที่ถูกต้อง: 4ควรจะ4 1 1 1ทำซ้ำและอื่น ๆ
  • ตรวจสอบ#@]$[,1#~<:@[ว่าเราเห็นว่ามันใช้กริยารูปร่างของ J $ตรงกลาง - นั่นคือคำกริยาหลักของวลีนี้ ...
  • ไปทางซ้ายของ$คือ#@]หมายถึงความยาวของหาเรื่องที่เหมาะสม #]
  • ไปทางขวาของ$มี[,1#~<:@[, 5 คำกริยารถไฟ รถไฟขบวนแรกที่ดำเนินการคือ ...
  • 1#~<:@[ซึ่งหมายความว่า 1 คัดลอก#~(แบบ passive ของสำเนา) น้อยกว่าหาเรื่องซ้าย<: [ผลลัพธ์นี้ถูกส่งผ่านไปยัง fork สุดท้าย:
  • [, ...หมายถึงหา arg ซ้ายและผนวกผลลัพธ์ที่เราเพิ่งคำนวณซึ่งเป็นสตริงของ1s

ลองออนไลน์!


]#~[^0=(|i.@#)สำหรับ 14 ไบต์
ไมล์

มันค่อนข้างฉลาด การปรับปรุงโพสต์ของฉันเป็นส่วนที่ดีที่สุดของไซต์นี้สำหรับฉัน
โยนาห์


1

Perl 5, 37 , 29 +1 (-p) ไบต์

-8 ไบต์ขอบคุณความคิดเห็นของทอม

$n=<>;s/./"@-"%$n?$&:$&x$n/ge

ลองใช้ออนไลน์


ไม่สามารถคิดวิธีที่ดีกว่าในตอนนี้ แต่ฉันมากับแนวคิดบางอย่าง: $n=<>;แทนที่จะBEGINบล็อกและมีnในบรรทัดถัดไปของการป้อนข้อมูลและแทนที่$-[0]ด้วย"@-"เนื่องจากตัวเลขแรกเท่านั้นที่มีการประเมินในการเปรียบเทียบ นอกจากนี้หากคุณป้อนข้อมูลnผ่าน-iคุณสามารถใช้$^Iแทนการประกาศและใช้$nงานได้ แต่เนื่องจากนี่ไม่ใช่มาตรฐานจึงอาจไม่สามารถบินได้ ... :)
Dom Hastings

1

รหัสประจำเครื่อง 6502 , 50 ไบต์

A0 01 84 97 88 84 9E 84 9F B1 FB F0 20 A4 9F 91 FD C6 97 D0 10 A6 FF CA F0
05 C8 91 FD D0 F8 84 9F A5 FF 85 97 E6 9E A4 9E E6 9F D0 DC A4 9F 91 FD 60

นี่คือ subroutine ตำแหน่งอิสระคาดหวังว่าตัวชี้ไปยังสายเข้าที่ (0 สิ้นสุด aka C-String) ใน$fb/ $fcตัวชี้ไปยังการส่งออกใน buffer $fd/ $feและนับ ( n) $ffใน มันใช้การจัดทำดัชนีอย่างง่ายดังนั้นมันจึงจำกัดความยาวเอาท์พุทสูงสุดที่ 255 อักขระ (+ 0 ไบต์) เนื่องจากสถาปัตยกรรม 8 บิต

คำอธิบาย (ความคิดเห็นการถอดแยกชิ้นส่วน):

 .rep:
A0 01       LDY #$01            ; init counter to next repetition sequence
84 97       STY $97
88          DEY                 ; init read and write index
84 9E       STY $9E             ; (read)
84 9F       STY $9F             ; (write)
 .rep_loop:
B1 FB       LDA ($FB),Y         ; read next character
F0 20       BEQ .rep_done       ; 0 -> finished
A4 9F       LDY $9F             ; load write index
91 FD       STA ($FD),Y         ; write next character
C6 97       DEC $97             ; decrement counter to nex rep. seq.
D0 10       BNE .rep_next       ; not reached yet -> next iteration
A6 FF       LDX $FF             ; load repetition counter
 .rep_seqloop:
CA          DEX                 ; and decrement
F0 05       BEQ .rep_seqdone    ; if 0, no more repetitions
C8          INY                 ; increment write index
91 FD       STA ($FD),Y         ; write character
D0 F8       BNE .rep_seqloop    ; and repeat for this sequence
 .rep_seqdone:
84 9F       STY $9F             ; store back write index
A5 FF       LDA $FF             ; re-init counter to next ...
85 97       STA $97             ; ... repetition sequence
 .rep_next:
E6 9E       INC $9E             ; increment read index
A4 9E       LDY $9E             ; load read index
E6 9F       INC $9F             ; increment write index
D0 DC       BNE .rep_loop       ; jump back (main loop)
 .rep_done:
A4 9F       LDY $9F             ; load write index
91 FD       STA ($FD),Y         ; and write terminating0-byte there
60          RTS                 ; done.

ตัวอย่างโปรแกรมรหัสเครื่อง C64 โดยใช้ :

นี่คือโปรแกรมในตัวประกอบ ca65-styleสำหรับ C64 โดยใช้รูทีนนี้ (อิมพอร์ตเป็นrep):

REP_IN          = $fb
REP_IN_L        = $fb
REP_IN_H        = $fc

REP_OUT         = $fd
REP_OUT_L       = $fd
REP_OUT_H       = $fe

REP_N           = $ff

.import         rep


.segment "LDADDR"
                .word   $c000

.code
                jsr     $aefd           ; consume comma
                jsr     $ad9e           ; evaluate expression
                sta     REP_IN_L        ; store string length
                jsr     $b6a3           ; free string
                ldy     #$00            ; loop over string
readloop:       cpy     REP_IN_L        ; end of string?
                beq     termstr         ; then jump to 0-terminate string
                lda     ($22),y         ; read next character
                sta     in,y            ; store in input buffer
                iny                     ; next
                bne     readloop
termstr:        lda     #$00            ; load 0 byte
                sta     in,y            ; store in input buffer

                jsr     $b79b           ; read 8bit unsigned int
                stx     REP_N           ; store in `n`
                lda     #<in            ; (
                sta     REP_IN_L        ;   store pointer to
                lda     #>in            ;   to input string
                sta     REP_IN_H        ; )
                lda     #<out           ; (
                sta     REP_OUT_L       ;   store pointer to
                lda     #>out           ;   output buffer
                sta     REP_OUT_H       ; )
                jsr     rep             ; call function

                ldy     #$00            ; output result
outloop:        lda     out,y
                beq     done
                jsr     $ffd2
                iny
                bne     outloop
done:           rts


.bss
in:             .res    $100
out:            .res    $100

การสาธิตออนไลน์

การใช้งาน: sys49152,"[s]",[n] , เช่นsys49152,"Hello, World!",3

สำคัญ:หากโปรแกรมโหลดจากดิสก์ (เช่นในการสาธิตออนไลน์) ออกnewคำสั่งก่อน! สิ่งนี้จำเป็นเนื่องจากการโหลดโปรแกรมเครื่องจะทำให้พอยน์เตอร์ C64 พื้นฐานบางส่วนเสียหาย


1

Java 8, 100 76 ไบต์

s->n->{int i,k=0;for(char c:s)for(i=k++%n<1?n:1;i-->0;)System.out.print(c);}

-24 ไบต์ขอบคุณที่@ OliverGrégoire

คำอธิบาย:

ลองที่นี่

s->n->{                    // Method with char-array and int parameters and no return-type
  int i,k=0;               //  Index-integers
  for(char c:s)            //  Loop (1) over the characters of the input array
    for(i=k++%n<1?         //   If `k` is divisible by the input `n`:
         n                 //    Change `i` to `n`
        :                  //   Else:
         1;                //    Change `i` to 1
        i-->0;)            //   Inner loop (2) from `i` down to 0
      System.out.print(c); //    And print the current character that many times
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
}                          // End of method

อ๊ะฉันไม่เห็นว่ามีการส่งไปแล้วดังนั้นฉันจึงลบของฉันออก นี่สั้นลงเหลือ 76 ไบต์: n->s->{int i,k=0;for(char c:s)for(i=k++%n<1?n:1;i-->0;)System.out.print(c);}(พร้อมchar[], แทนที่จะเป็นString)
Olivier Grégoire

กฎของหัวแม่มือถ้าคุณต้องประกาศสตริงเดียวที่จะส่งคืนมันจะสั้นกว่าที่จะพิมพ์ออกมา
Olivier Grégoire

@ OlivierGrégoireโอ๊ะโอใช่ฉันรู้ว่ากฎของหัวแม่มือเพียงลืมที่จะใช้มันในครั้งนี้ .. และขอบคุณสำหรับไบต์ที่บันทึกไว้!
Kevin Cruijssen

1

MATL , 10 7 ไบต์

-3 ไบต์ขอบคุณ Luis Mendo!

tq:ghY"

ลองออนไลน์!

จะเข้าเป็นnแล้วSเป็นสตริง / ถ่านอาร์เรย์

    % (implicit input)
    % stack: n
t   % duplicate
    % stack: n n
q   % decrement
    % stack: n n-1
:   % range
    % stack: n [1 2 ... n-1]
g   % convert to logical (nonzero->1, zero->0)
    % stack: n [1 1 ... 1]
h   % horizontal concatenate
    % stack: [n 1 1 ... 1]
Y"  % run-length decoding, taking the string as first input and recycling 
    % the lengths [n 1 1 ... 1] as needed
    % (implicit output as string)


1

Haskell , 51 46 ไบต์

ขอบคุณ @Laikoni ที่ช่วยฉัน 5 ไบต์!

n&s=do(m,c)<-zip[0..]s;c<$[0..(n-1)*0^mod m n]

ลองออนไลน์!

คำอธิบาย / Ungolfed

ผู้ประกอบการc <$ [a..b]แทนที่องค์ประกอบของแต่ละรายการ[a,a+1...b]โดยc- ดังนั้นจึงเป็นเพียงแข็งแรงเล่นกอล์ฟreplicate:

do(m,c)<-zip[0..]s;                                  -- with all (m,c) in the enumerated ([(0,a),(1,b)..]) input string, replace with
                   replicate (1 + (n-1)*0^mod m n) c -- either n or 1 times the character c (note that the list begins with 0, that's where the 1+ comes from)


0

V , 13 ไบต์

"aDJòylÀpÀll

ลองออนไลน์!

นี่เป็นวิธีแก้ปัญหาที่โง่จริงๆ òlhÀälÀlÀ<M-->lควรจะทำงาน แต่ฉันจะไม่ให้ชีวิตของฉันเข้าใจว่าทำไมโดยเฉพาะอย่างยิ่งตั้งแต่ตนเองทำlhÀälÀlÀ<M-->lซ้ำพวงของครั้งไม่ทำงาน

hexdump:

00000000: 1822 6144 4af2 796c c070 c06c 6c         ."aDJ.yl.p.ll

คำอธิบาย:

<C-x>               " Decrement the number
       D            " Delete that number...
     "a             "   Into register 'a'
        J           " Remove the blank line
         ò          " Recursively...
          yl        "   Yank the letter under the cursor
            Àp      "   And paste it 'a' times
              Àl    "   Move 'a' times to the right ('l' for right)
                l   "   Move to the right one more time
                    " (implicit) end the loop

'l' for right... ฉันเดาว่าเป็นสิ่งที่ค้างไว้เป็นกลุ่ม? มิฉะนั้น ... ทำไม ?
AdmBorkBork

2
@AdmBorkBork ใช่แล้วlเป็นกลุ่ม อาจย้อนกลับแบบออโธกราฟ แต่ก็ถูกต้องทางเรขาคณิต: lเป็นตัวอักษรที่ถูกต้องที่สุดของแถวกลาง
โยนาห์

@DJMcMayhem ถูกต้อง ฉันทำถูกต้องแล้ว
โจนาห์


0

Python 3 , 58 ไบต์

ทำงานเกี่ยวกับการเล่นกอล์ฟ

ฉันรู้ว่ามีคำตอบอื่น ๆ ของ Python อยู่แล้ว แต่ฉันคิดว่าฉันโพสต์สิ่งนี้ด้วยเช่นกันเนื่องจากคะแนนค่อนข้างดีเมื่อเทียบกับคนอื่น ๆ แม้ว่าจะเป็นฟังก์ชั่นที่สมบูรณ์และไม่ใช่แลมบ์ดา

STDOUTจะเข้าเป็นพารามิเตอร์ฟังก์ชั่นและพิมพ์ไป

def f(s,n,i=0):
 for c in s:print(end=[c,c*n][i%n<1]);i+=1

ลองออนไลน์!

สำหรับหนึ่งไบต์น้อยกว่า (57) ฉันเขียนแลมบ์ดา แต่คำตอบที่คล้ายกันได้ถูกโพสต์โดยผู้ใช้รายอื่นแล้ว:

lambda s,n:''.join([c,c*n][i%n<1]for i,c in enumerate(s))

0

Brain-Flak (BrainHack) , 122 + 3 ( -A) = 125 ไบต์

ฉันแน่ใจว่านี่ยาวเกินไป แต่ฉันใช้เวลาสักพักหนึ่งในการตรวจสอบและไม่พบการปรับปรุงใด ๆ

([]){{}([(([{}]<>)<{({}<<>(({})<>)>())}{}{}>)<{({}<<>({}<>)>())}{}>]<>)([][()])}({}{}<>){({}{(<()>)}{}[()])}{}{({}<>)<>}<>

ลองออนไลน์!




0

K (oK) , 23 19 ไบต์

วิธีการแก้:

{,/(1|y*~y!!#x)#'x}

ลองออนไลน์!

ตัวอย่าง:

> {,/(1|y*~y!!#x)#'x}["Hello, World!";3]
"HHHellllo,   Worrrld!!!"
> {,/(1|y*~y!!#x)#'x}["Code golf";1]
"Code golf"
> {,/(1|y*~y!!#x)#'x}["abcdefghijklm";10]
"aaaaaaaaaabcdefghijkkkkkkkkkklm"

คำอธิบาย:

{,/(1|y*~y!!#x)#'x} / the solution
{                 } / lambda function with x and y as implicit parameters
   (          )     / do everything in brackets together
            #x      / count x, #"Hello, World!" -> 13
           !        / til, !13 -> 0 1 2 3 4 5 6 7 8 9 10 11 12
         y!         / y modulo, 3!0 1 2 3 4 5 6 7 8 9 10 11 12 -> 0 1 2 0 1 2 0 1 2 0 1 2 0
        ~           / not, ~0 1 2 0 1 2 0 1 2 0 1 2 0 -> 1 0 0 1 0 0 1 0 0 1 0 0 1
      y*            / multiply by y, 3*1 0 0 1 0 0 1 0 0 1 0 0 1 -> 3 0 0 3 0 0 3 0 0 3 0 0 3
    1|              / min of 1 and, 1|3 0 0 3 0 0 3 0 0 3 0 0 3 -> 3 1 1 3 1 1 3 1 1 3 1 1 3
                #'x / take each parallel, 1 2 3#'"abc" -> "a", "bb", "ccc"
 ,/                 / flatten the list, "a", "bb", "ccc" -> "abbccc"

หมายเหตุ:

  • -4 ไบต์ด้วยวิธีการที่แตกต่างกัน

0

Excel VBA, 71 ไบต์

ฟังก์ชันหน้าต่าง VBE แบบไม่ระบุชื่อทันทีที่รับอินพุตจากช่วง[A1:B1]และเอาต์พุตไปยังหน้าต่างทันที VBE

For i=1To[Len(A1)]:[C1]=i:?[Rept(Mid(A1,C1,1),B1^(Mod(C1,B1)=1))];:Next
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.