We haven’t had a string question for a while (5 days to be precise), so
let’s go for one.
文字列 s
と正の整数 n
が与えられたら、 s
のすべての n
> n 回、それを s
に戻します。
For example, if n = 3
and s = "Hello,
, every third character is
World!"Hl r!
. You
then repeat each character n
times to produce
HHHlll rrr!!!
. You then replace the original letters
with the repeated versions to produce the final product of
HHHellllo, Worrrld!!!
可能な限り最短のコードでこのタスクを実行する必要があります。
ルール
- This is a code-golf so the shortest code in bytes wins
-
n
is guaranteed to be smaller than the length of
s
and greater than 0 - The first character of
s
is where the
n
th characters are taken from, and is always repeated
n
times -
s
will only consist of printable ASCII (code
points0x20 (space)
to0x7E (~)
)
テストケース
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"
ベストアンサー
Jelly, 6 5 bytes
漏洩Nunのおかげで-1バイト(Pythonの文字列の乗算を使用します)。
×Jm¥¦
2つのコマンドライン引数、文字列と数値を受け取り、結果を出力する完全なプログラム。
どうやって?
×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