顶绳梯


12

给定一个字符串s和一个正整数N,逐渐将每个字符逐渐重复,直到N重复为止,然后停留在N重复位置,直到N字符远离结尾,然后再次下移。

例如,给定abalone3

a    we start with 1 copy
bb   then 2 copies
aaa  then 3 copies, which is our second parameter
lll  so we continue using 3 copies
ooo  until we reach the end
nn   where we use 2 copies
e    and then finally 1 copy

结果将是abbaaalllooonne

保证字符串的长度大于2N并且仅包含字符从az

更多测试用例:

N string       output
2 aaaaa        aaaaaaaa
3 abcdabcdabcd abbcccdddaaabbbcccdddaaabbbccd

这是。以字节为单位的最短答案将获胜。有标准漏洞

Answers:


11

果冻,6个字节

JṡFṢị⁸

在线尝试!

怎么运行的

JṡFṢị⁸  Main link. Arguments: s (string), n (integer)

J       Get the indices of s.
 ṡ      Split the indices into overlapping chunks of length n.
  F     Flatten the array of chunks.
   Ṣ    Sort the resulting array of indices.
    ị⁸   Get the characters of s at these indices.

样品运行

JṡFṢị⁸  "abalone", 3

J       [1, 2, 3, 4, 5, 6, 7].
 ṡ      [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
  F     [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7]
   Ṣ    [1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7]
    ị⁸  "abbaaalllooonne"

3
那种split + flatten + sort方法是天才。真好!:)
HyperNeutrino

7

Python 2,57字节

f=lambda s,n,i=1:s and s[0]*len(s[:i][:n])+f(s[1:],n,i+1)

在线尝试!

也57:

Python 2,57字节

f=lambda s,n,i=1:s and s[0]*len(s[:i])+f(s[1:],n,i+(i<n))

在线尝试!


您能解释一下背后的逻辑len(s[:i][:n])吗?我相信有一个更短的方法来获取这个数字,但是我不确定如何。
musicman523

没关系,我明白了!但是比短一字节min(len(s),i,n)。做得好!
musicman523

6

JavaScript(ES6), 67 65字节

-2个字节,这归功于Chas Brown使用的较短方法min()

s=>n=>s.replace(/./g,(c,i)=>c.repeat(Math.min(i+1,s.length-i,n)))

以currying语法输入: f("abalone")(3)

测试片段

f=
s=>n=>s.replace(/./g,(c,i)=>c.repeat(Math.min(i+1,s.length-i,n)))
<div oninput="O.value=f(S.value)(+N.value)">String: <input id=S> N: <input id=N size=3></div>Out: <input id=O size=50 disabled>


6

果冻8 7字节

J««U$⁸x

在线尝试!

怎么运行的

J««U$⁸x - main link, input e.g. abalone
J        - range of length of letters: [1,2,3,4,5,6,7]
 «       - minimum of each term with second input: [1,2,3,3,3,3,3]
  «U$    - termwise minimum with the reverse: 
                    min([1,2,3,3,3,3,3],[3,3,3,3,3,2,1])=[1,2,3,3,3,2,1]
     ⁸x  - repeat each character of the input a number of times corresponding to elements:
                    a*1;b*2;a*3...e*1 = abbaaalllooonne

-1字节感谢@LeakyNun


好发现@LeakyNun!我在那个方向上得到的最接近的是J«¥@«U$x@9个字节。
fireflame241

请解释一下?
“ SparklePony同志” 17年

@ fireflame241 x@⁸通常相当于⁸x(我在这里使用)
Leaky Nun

2

Haskell61 60字节

感谢@Laikoni帮助剃除1个字节

n#s=do(i,c)<-zip[1..]s;replicate(minimum[n,i,length s-i+1])c

在线尝试!

取消高尔夫:

(#) n string = do
    (i, char) <- zip [1..] string
    replicate (minimum [n, i, length(string)-i+1]) char

大量使用do块!通过将括号放在中来保存一个字节length(s)
Laikoni'7

1

Haskell(Lambdabot),74个字节

r=replicate
f x n=join$zipWith r([1..n]++r(length x-2*n)n++reverse[1..n])x

在线尝试!


进口计入分数!您可能会更好>>=id
bartavelle

是的,我之前也这样做过,然后我看到(这就是为什么Lambdabot放在括号中)的原因。正确的方法是什么?
ბიმო

我站得住脚,我认为这还好!
bartavelle

很高兴知道,该清单中有很多非常方便的进口商品。
ბიმო

1

J,24个字节

(<.&n<./(|.,:[)>:i.#s)#s

parens中的位(<.&n<./(|.,:[)>:i.#s)--创建1 2 ... n n n ... 2 1数组,如下所示:

                   #s    length of s, call it L
                 i.      numbers 0 1 ... L-1
               >:        increment by 1, now 1 2 ... L
        (|.,:[)          fork: |. = reverse, ,: = stack, [ = identity
                         resulting in  L ... 2 1
                                       1 2 ... L 
     <./                 min of each element of the top and bottom row
 <.&n                    min of each resulting elm and n

一旦有了,J #运算符就会自动执行要求操作,将每个元素重复指定的次数。

很好奇看到J专家对此有所改进...


23个字节,采用完全不同的方法[#~#@[$([:>:<:,&:i.-)@](可能在其中捕获了一个杂散空间)。我不知道为什么钩子没有用,x但没有太多需要照顾的地方。
科尔

1

PHP> = 7.1,75字节

for([,$a,$n]=$argv;--$z?:($x=$a[$i]).$z=min($n,strlen($a)-$i,++$i);)echo$x;

PHP沙盒在线

PHP> = 7.1,78字节

for([,$a,$n]=$argv;~$x=$a[$i];)for($z=min($n,strlen($a)-$i,++$i);$z--;)echo$x;

PHP沙盒在线

PHP> = 7.1,80字节

for([,$a,$n]=$argv;$i<$l=strlen($a);)echo str_repeat($a[$i],min($n,$l-$i,++$i));

PHP沙盒在线


1

Japt11个10字节

ËpVm°TEnUÊ

测试一下


说明

字符串U和整数的隐式输入V

Ë

映射U并替换每个字符。

Vm

获得的最小值V,...

°T

T(最初0)增加1,...

EnUÊ

E从(n)的长度(Ê)中减去当前字符()的索引U

p

重复当前字符多次。

隐式输出最终字符串。



0

Python 2 68字节

f=lambda s,n:''.join(s[i]*min(i+1,len(s)-i,n)for i in range(len(s)))

您不需要f=答案;该函数可以是匿名的。考虑到这一点,您可以使用删除3个字节lambda s,n:''.join(c*min(i+1,len(s)-i,n)for i,c in enumerate(s))
notjagan

0

外壳10 9字节

₁₁ṀR
↔z↑N

在线尝试!

第一行是main函数,它将每个字母重复n次,然后调用第二行两次。

第二行从每个重复字母组中获取最多N个字母,其中N是该组中从1开始的索引,然后反转该列表。



0

APL(Dyalog),15字节

{⍵/⍨⍺⌊i⌊⌽i←⍳≢⍵}

{} 函数,其中左参数(cap)为,右参数(string)为

≢⍵ 计算字符串中的字符数

 产生许多ɩ ntegers

i← 存放在

 相反

i⌊ 与i成对最小

⍺⌊ 成对最小

⍵/⍨ 使用这些数字复制字符串的字母

在线尝试!




0

鲍鱼是鱼的一种(嗯,贝类),因此…

> <>,79字节

&i1\
0(?\:1+:&::&@)?$~i:@
&~}\&~1
0(?\:&::1+&@)?$~}}:
 ~r\
?!v>l?!;:o$1-:@
~~<^

在线尝试,或在鱼游乐场观看

从STDIN读取字符串,并假定该数字已在堆栈中。

说明:第二,第四和第六行是主循环。细节是一些丑陋的堆栈操作,但粗略地讲,首先,第二行填充输入字符和mini,  n)字符之间的交替,其中n是长度上限,而i是该字符在索引中的索引输入:对于“鲍鱼”,3,堆栈看起来像

"a", 1, "b", 2, "a", 3, "l", 3, "o", 3, "n", 3, "e", 3, -1=EOF, 3

接下来,第4行以相反的方式以相同的方式通过堆栈,以使右端正确地被封顶:

"a", 1, "b", 2, "a", 3, "l", 3, "o", 3, "n", 2, "e", 1, -1

然后,第六行获取每个字符-数字对,并打印该字符与数字相同的次数。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.