Basit Bir Patttern


17

girişler:

Girdiğiniz giriş biçiminde iki tek basamak (diyelim mve diyelim n) ve iki karakter (diyelim ave çağıralım b).

Çıktı:

İzlenecek yol için rol yap m=2, n=5, a='a', b='b'.

Çıktınız dört girişinizden oluşturulan bir dize olacaktır. Dizeyi resultdeğerle çağıralım "". İlk olarak, concatenate aüzerine result mzamanlarda, BİRLEŞTİR böylece aüzerine result 2zamanlarda. resultşimdi eşittir aa. İkincisi, concatenate büzerine result mzamanlarda, BİRLEŞTİR böylece büzerine result 2zamanlarda. resultşimdi eşittir aabb. Sonuç zaten daha uzun olması durumunda Son olarak, n, kesmek resulto uzunluğa sahiptir böylece n. Aksi takdirde, dönüşümlü şekilde devam muzunluğu ishal ave bkadar resultuzunluğa sahiptir n. Final result, aabbauzunluğu olan 5.

Test Durumları:

Input: m = 2, n = 4, a = A, b = B

Output: AABB

Input: m = 3, n = 8, a = A, b = B

Output: AAABBBAA

Input: m = 4, n = 3, a = A, b = B

Output: AAA

Input: m = 2, n = 10, a = A, b = B

Output: AABBAABBAA 

Herkesin bildiği gibi, daha az kişi dünyaya hükmeder, bu yüzden bayt cinsinden en küçük programlar kazanır! :)


"Çıktıdaki toplam karakter" n "olacak ve" dünyayı daha az yönetecek "ile ne demek istiyorsun?
Outgolfer Erik

Asıl amacın olduğuna inandığım şeyi koruyarak, meydan okumayı tekrar yazdım. İsterseniz geri alabilirsiniz, ancak orijinal durumunda yeniden açılmayacaktır.
Stephen

Günümü kurtardın: p gracias :)
Durga

@Durga sorun değil :) Hala ne istediğini söylediğine sevindim.
Stephen

2
@Durga test senaryosu önerdi:m=2,n=10,a=A,b=B
Rod

Yanıtlar:


8

Python , 32 bayt

lambda m,n,a,b:((a*m+b*m)*n)[:n]

Çevrimiçi deneyin!


f=-2 bayt kaldırabilmeniz için son olarak anonim işlevlere izin verilir .
SparklePony Yoldaş

@ComradeSparklePony: Uyarı için teşekkürler. Bu TiO'dan arta kaldı; Aslında bayt sayısından zaten kaldırmıştım.
Julian Wolf

2
f=TIO'nun başlık bölümüne koyabilirsiniz , böylece manuel olarak kaldırmanız gerekmez. TIO
ovs

Ah, her zaman ters eğik çizgileri unuturum. Teşekkürler.
Julian Wolf

1
Düzenlemeyi öneren herkese (a*m+b*m)-> (a+b)*m: bu işe yaramaz.
Julian Wolf

6

MATL , 5 bayt

Y"i:)

Girdiler iki karakterden oluşan bir dizedir m, sonra n.

Çevrimiçi deneyin!

açıklama

Y"   % Implicit inputs: string and number m. Apply run-length decoding.
     % The second input is reused for each char in the first. Gives a
     % string
i    % Input number n
:    % Push vector [1 2 ... n]
)    % Index the string with the numbers in that vector. Indexing is
     % modular, so the chars are reused if necessary. Implicit display


5

Ruby, 29 karakter

->m,n,a,b{((a*m+b*m)*n)[0,n]}

Örnek çalışma:

irb(main):001:0> ->m,n,a,b{((a*m+b*m)*n)[0,n]}[3, 8, 'A', 'B']
=> "AAABBBAA"

Çevrimiçi deneyin!


5

Japt , 10 bayt

VîUçW +UçX

Önce bir golf dili kullanmayı deneyin. Çevrimiçi deneyin!

açıklama

Vî          // repeat the following until it reaches length V (second input)
  UçW       // third input repeated U (first input) times
      +UçX  // plus the fourth input, repeated U times

Japt kullandığınız için teşekkürler ve aferin :-) Sen de yapabilirsin VîWpU +XpU, ama her ikisi de aynı şeyi yapar. bu meydan okuma için mükemmeldir.
ETHproductions

@ETHproductions Teşekkürler ve yaptığınız için teşekkürler! Gerçekten nasıl her şey JS koduna güzel aktarır zevk.
Justin Mariner



3

V, 13 bytes

ÀäjÀäêÍî
À|lD

Try it online!

a and b are taken on separate lines in the input, m and n are taken as argument, reversed (so n is the first argument and m is the second)

Explanation

Àäj      ' duplicate the inputs [arg 1] times
a -> a
b    b
     a
     b
     ...
   Àäê   ' duplicate everything straight down [arg 2] times - À cycles arguments
a -> aaa
b    bbb
a    aaa
b    bbb
...  ...
      Íî ' remove all newlines
-> aaabbbaaabbb...
À|lD     ' delete from the [arg 1] + 1 column onwards
-> aaabbbaa

3

Haskell, 36 35 29 bytes

Yet another Haskell solution (expects the characters given as a list):

(m#n)c=take n$cycle$c<*[1..m]

Try it online!

Thanks @Laikoni for -1 byte.


1
You can save a byte with (m#n)a b=.
Laikoni

3

R, 41 39 bytes

function(d,m,n)cat(d[gl(2,m,n)],sep='')

An anonymous function; prints the result to stdout. Takes the characters as a vector d=c(a,b). gl generates factors (integers) of (in this case) 2 levels of run length m with total length n! cat concatenates and prints them as a string.

Try it online!


I think function(d,m,n)rep(d,e=m,l=n) would be a valid submission.
ovs

@ovs unfortunately rep will result in a vector of characters rather than a single string
Giuseppe

2

Javascript, 55 bytes

(m,n,a,b)=>(a[r='repeat'](m)+b[r](m))[r](n).substr(0,n)

Example code snippet:

f=

(m,n,a,b)=>(a[r='repeat'](m)+b[r](m))[r](n).substr(0,n)

console.log(f(2, 4, 'A', 'B'))
console.log(f(3, 8, 'A', 'B'))
console.log(f(4, 3, 'A', 'B'))
console.log(f(2, 9, 'A', 'B'))


2

Javascript, 53 bytes

(m,n,a,b)=>a.repeat(n).replace(/./g,(i,j)=>j/m&1?b:i)



1

QBIC, 37 27 bytes

[:|G=;+G+;][:|G=G+G]?_sG,d

Explanation

          This takes its arguments as frequency m, A, B, length n
          For example: 2, A, B, 8
 :        Read a cmd line arg as number 'b' ('a' is used by the FOR declaration as loop counter)
[ |       Start a FOR loop, from 1 to b
G=  G     Set G to hold itself
  ;+      prepended by a cmd line arg read as strig and assigned to A$
     +;   and followed by a cmd line arg read as strig and assigned to B$
]         At the end of the FOR loop, G has had A added to the front twice, and B t the end x2: G$ = AABB
[:|       FOR c = 1 to n
G=G+G]      Add G to itself          G$ = AABBAABBAABBAABBAABBAABBAABBAABB
?_sG,d    PRINT the first n chars of G$   AABBAABB

Previous attempt:

(37b)  {Z=Z+;┘_LZ|~a=:|_X]~a%:|\C=A┘A=;┘B=C
Takes its arguments as `A, length n, frequency m, B`.
Basically adds A to Z until length % freq = 0, then swaps A for B. Loops until lengtn = n



1

Cubix, 63 58 bytes

.rr.@u:s?.\.sw).i|>v:.\nB;?(q:Is...;rr/s.uw/....sIB/\/?(qo

Try it online!

watch the interpreter

Takes input like ab*m*n where the * can be any non-digit character.

Cube version:

        . r r .
        @ u : s
        ? . \ .
        s w ) .
i | > v : . \ n B ; ? ( q : I s
. . . ; r r / s . u w / . . . .
s I B / \ / ? ( q o . . . . . .
. . . . . . . . . . . . . . . .
        . . . .
        . . . .
        . . . .
        . . . .
  • i|is : read in the chars, and swap them (so a is on top)
  • I:q : read in m, dup, and push to bottom (stack is now m,b,a,m)
  • ) : decrement
  • ? : turn right if positive, go straight if zero (duplicates a)
  • positive branch (loop)
    • s:rur(/w : swap, dup, move m-i to the top of the stack, decrement m-i
  • zero branch
    • B : reverse stack (which now has m copies of a: a... b m)
    • n : negate m (so we can use ? to turn left)
    • ) : increment
    • ? : go straight if zero, turn left if negative
  • negative branch (duplicates b)
    • s:r\/rw)\ basically the same as the positive branch but with increment and left turns.
  • zero branch (prints the output)
    • >v; : pop the 0 off the stack (looks like a...b...)
    • /B : reverse the stack
    • I : read n
    • s : swap print loop:
  • oq : print and push to bottom of stack now looks like: ab...a...n
  • ( decrement n
  • ? : turn right if positive, go straight if zero
  • If right, : /su : swap top of stack and continue the loop
  • if zero, / reflects down and the code evaluated is Iru@; @ terminates the program.

0

Charcoal, 10 bytes

…⁺×ζIθ×εNN

Try it online! Link is to verbose version of code and includes fourth example. (Annoyingly the deverbosifer won't remove the separator if I add one before the last InputNumber().)


What do you mean by the separator? (Can you give an example)
ASCII-only

@ASCII-only With the comma before the last InputNumber(), notice that the generated code has an unnecessary separator: Try it online!
Neil

0

Mathematica , 61 bytes

T=Table;StringTake[""<>Flatten@T[{#3~T~#,#4~T~#},⌈#2/#⌉],#2]&

input

[2,10,"A","B"]


0

Mathematica, 44 bytes

StringPadRight[x={##3}~Table~#<>"",#2,x]&

Explanation

is the three byte private use character U+F3C7, representing the postfix \[Transpose] operator in Mathematica. No TIO link because Mathics does not support , \[Transpose] has the wrong operator precedence, the second argument to Table is required to be a list, and most importantly, StringPadRight is not implemented.

                                         & (* Function *)
                 {##3}                     (* which takes the third and fourth arguments *)
                      ~Table~#             (* repeats them a number of times equal to the first argument *)
                                          (* takes the tranpose *)
                               <>""        (* then joins the strings with the empty string *)
               x=                          (* sets x equal to that string *)
StringPadRight[                            (* then pads x *)
                                   ,#2     (* to a length equal to the second argument *)
                                      ,x]  (* with x. *)

0

APL (Dyalog), 5 bytes

⎕⍴⎕/⎕

Try it online!

Takes the two chars in a string as the first input, followed by m and then n.

Explanation

Let the example input be 'ab', 2, 10.

⎕/⎕                 Replicate the two-char string `m` times
                    2/'ab' => 'aabb'
⎕⍴                  Shape it so that its length is `n`
                    10'aabb' => 'aabbaabbaa'

0

Pyth, 13 bytes

KE<*+*EQ*EQKK

Try it online!

Explanation

                 # Implicitly store m to Q
KE               # Store n to K
     *EQ         # Perform a * m
        *EQ      # Perform b * m
    +            # Concatenate the two strings
   *       K     # Multiply by n
  <         K    # Take the first n characters of the string


0

Chip, 588 bytes

*Z~vZ.*ZZZs  z. z. z. z. z. z. z. z.
  ,'|`-. ZZ--#<,#<,#<,#<,#<,#<,#<,#<
a/mAM/a| `~S `x'`x'`x'`x'`x'`x'`x'`x.
b/mBM/b|  *.)/')/')/')/')/')/')/')/'|
c/mCM/cZv--x^x-^x-^x-^x-^x-^x-^x-^x-'
d/mDM/d||A~#M',-',-',-',-',-',-',-'
e/mEM/e||B~#M-',-',-',-',-',-',-'
f/mFM/f||C~#M--',-',-',-',-',-'
g/mGM/g||D~#M---',-',-',-',-'
h/mHM/h||E~#M----',-',-',-'
 `v~v' ||F~#M-----',-',-'
* `mz  ||G~#M------',-'
Z  `---x'H~#M-------'
Z,--z--^----'
Z|z. z. z. z. z. z. z. z.
Zx#<,#<,#<,#<,#<,#<,#<,#<
 |`x'`x'`x'`x'`x'`x'`x'`xT
 |A| B| C| D| E| F| G| H|
 )\')\')\')\')\')\')\')\'
 `--^--^--^--^--^--^--'

Try it online!

Takes input as a 4-character string. The first two are the characters a and b, followed by the byte value m, and then the byte value n. For example, the TIO includes input ab<tab>2, this corresponds to 'a', 'b', 9, 50. (Since the codes for <tab> and 2 are 9 and 50.

How?

This answer is a bit of a behemoth, but here's the highlights:

The upper left block, with the lowercase a-h, is the storage mechanism for the characters a and b, one line per bit. At its bottom, with the v~v and mz is the switching mechanism, to swap between the two.

In the middle is a column with a bunch of ~#M's. This reads in m and stores its negative. The big triangle to the right is just wires to bring this value into the upper accumulator.

The upper right block is the accumulator for m. It increments every cycle (starting from -m) until it reaches zero. When this happens, the output character is swapped, and counting restarts from -m.

Meanwhile, there is the lower block, which is the n accumulator. Since n is only read once, we don't need a bank of memory (M and m) to store this value. We simply negate it and start counting. When this value reaches zero, the whole shebang is simply terminated.

All the other guff is delays (Z and z), wiring (-, |, ...), and other miscellany.

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.