Given a string s
and a positive integer N
, gradually duplicate each character more and more until N
duplicates, and then staying at N
duplicates until N
characters away from the end, then step down again.
For example, given abalone
and 3
:
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
and the result would be abbaaalllooonne
.
It is guaranteed that the string has length greater than 2N
and only has characters from a
to z
.
More testcases:
N string output
2 aaaaa aaaaaaaa
3 abcdabcdabcd abbcccdddaaabbbcccdddaaabbbccd
This is code-golf. Shortest answer in bytes wins. Standard loopholes apply.
18 Answers 18
Jelly, 6 bytes
JṡFṢị8
How it works
JṡFṢị8 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.
ị8 Get the characters of s at these indices.
Sample run
JṡFṢị8 "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]
ị8 "abbaaalllooonne"
-
3\$\begingroup\$ That split+flatten+sort method is pure genius. Nice! :) \$\endgroup\$2017年07月04日 19:04:31 +00:00Commented Jul 4, 2017 at 19:04
Python 2, 57 bytes
f=lambda s,n,i=1:s and s[0]*len(s[:i][:n])+f(s[1:],n,i+1)
Also 57:
Python 2, 57 bytes
f=lambda s,n,i=1:s and s[0]*len(s[:i])+f(s[1:],n,i+(i<n))
-
\$\begingroup\$ Can you explain your logic behind
len(s[:i][:n])
? I'm convinced there's a shorter way to get that number but I'm not sure how. \$\endgroup\$musicman523– musicman5232017年07月04日 06:49:53 +00:00Commented Jul 4, 2017 at 6:49 -
\$\begingroup\$ Never mind, I got it! But it's one byte shorter than
min(len(s),i,n)
. Great job! \$\endgroup\$musicman523– musicman5232017年07月04日 06:53:38 +00:00Commented Jul 4, 2017 at 6:53
JavaScript (ES6), (削除) 67 (削除ここまで) 65 bytes
-2 bytes thanks to Chas Brown's shorter method using min()
.
s=>n=>s.replace(/./g,(c,i)=>c.repeat(Math.min(i+1,s.length-i,n)))
Takes input in currying syntax: f("abalone")(3)
.
Test Snippet
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>
Jelly, (削除) 8 (削除ここまで) 7 bytes
J««U8ドルx
How it Works
J««U8ドル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]
8x - repeat each character of the input a number of times corresponding to elements:
a*1;b*2;a*3...e*1 = abbaaalllooonne
-1 byte thanks to @LeakyNun
-
\$\begingroup\$ Good find @LeakyNun! The closest I got in that direction was
J«¥@«U$x@
for 9 bytes. \$\endgroup\$fireflame241– fireflame2412017年07月04日 05:18:37 +00:00Commented Jul 4, 2017 at 5:18 -
\$\begingroup\$ Explanation please? \$\endgroup\$sporkl– sporkl2017年07月04日 05:20:09 +00:00Commented Jul 4, 2017 at 5:20
-
\$\begingroup\$ @fireflame241 genenrally,
x@8
is equivallent to8x
(I used8
here) \$\endgroup\$Leaky Nun– Leaky Nun2017年07月04日 05:38:31 +00:00Commented Jul 4, 2017 at 5:38
Haskell, (削除) 61 (削除ここまで) 60 bytes
Thanks to @Laikoni for helping to shave off 1 byte
n#s=do(i,c)<-zip[1..]s;replicate(minimum[n,i,length s-i+1])c
Ungolfed:
(#) n string = do
(i, char) <- zip [1..] string
replicate (minimum [n, i, length(string)-i+1]) char
-
\$\begingroup\$ Great use of a
do
block! Save a byte by dropping the parenthesis inlength(s)
. \$\endgroup\$Laikoni– Laikoni2017年07月05日 05:28:25 +00:00Commented Jul 5, 2017 at 5:28
Haskell (Lambdabot), 74 bytes
r=replicate
f x n=join$zipWith r([1..n]++r(length x-2*n)n++reverse[1..n])x
-
\$\begingroup\$ Imports count in the score! You would be better of with
>>=id
\$\endgroup\$bartavelle– bartavelle2017年07月04日 06:57:32 +00:00Commented Jul 4, 2017 at 6:57 -
-
\$\begingroup\$ I stand corrected, I think this is alright! \$\endgroup\$bartavelle– bartavelle2017年07月04日 08:01:28 +00:00Commented Jul 4, 2017 at 8:01
-
\$\begingroup\$ Good to know, there are a lot of very handy imports in that list. \$\endgroup\$ბიმო– ბიმო2017年07月04日 08:03:10 +00:00Commented Jul 4, 2017 at 8:03
J, 24 bytes
(<.&n<./(|.,:[)>:i.#s)#s
The bit in parens -- (<.&n<./(|.,:[)>:i.#s)
-- creates the 1 2 ... n n n ... 2 1
array, as follows:
#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
once we have that, J's #
operator automatically does exactly what asked for, duplicating each element the number of times specified.
Curious to see a J expert's improvment on this...
-
\$\begingroup\$ 23 bytes with a quite different approach
[#~#@[$([:>:<:,&:i.-)@]
(maybe a stray space got caught in there). I'm at a loss as to why the hook isn't takingx
but not in too much a position to care. \$\endgroup\$cole– cole2017年09月05日 00:36:33 +00:00Commented Sep 5, 2017 at 0:36
PHP>=7.1, 75 bytes
for([,$a,$n]=$argv;--$z?:($x=$a[$i]).$z=min($n,strlen($a)-$i,++$i);)echo$x;
PHP>=7.1, 78 bytes
for([,$a,$n]=$argv;~$x=$a[$i];)for($z=min($n,strlen($a)-$i,++$i);$z--;)echo$x;
PHP>=7.1, 80 bytes
for([,$a,$n]=$argv;$i<$l=strlen($a);)echo str_repeat($a[$i],min($n,$l-$i,++$i));
Japt, (削除) 11 (削除ここまで) 10 bytes
ËpVm°TEnUÊ
Explanation
Implicit input of string U
and integer V
.
Ë
Map over U
and replace every character.
Vm
Get the minimum of V
, ...
°T
T
(initially 0
) incremented by 1, ...
EnUÊ
And the index of the current character (E
) subtracted from (n
) the length (Ê
) of U
.
p
Repeat the current character that many times.
Implicitly output the final string.
R, 87 bytes
function(N,s)paste(rep(strsplit(s,"")[[1]],c(1:N,rep(N,nchar(s)-2*N),N:1)),collapse="")
Python 2 68 bytes
f=lambda s,n:''.join(s[i]*min(i+1,len(s)-i,n)for i in range(len(s)))
-
\$\begingroup\$ You don't need the
f=
in the answer; the function can be anonymous. With that in mind, you can remove 3 bytes withlambda s,n:''.join(c*min(i+1,len(s)-i,n)for i,c in enumerate(s))
. \$\endgroup\$notjagan– notjagan2017年07月04日 04:44:36 +00:00Commented Jul 4, 2017 at 4:44
Husk, (削除) 10 (削除ここまで) 9 bytes
11ṀR
↔z↑N
The first line is the main function, it repeats each letter n times and then calls the second line twice.
The second line takes at most N letters from each group of repeated letters, where N is the 1-based index of the group, then reverses the list.
Haskell, 68 bytes
g 1
g _""_=""
g i(a:b)n=(a<$[1..min(length b+1)$min i n])++g(i+1)b n
APL (Dyalog), 15 bytes
{⍵/⍨⍺⌊i⌊⌽i←⍳≢⍵}
{
...}
function where left argument (cap) is ⍺ and right argument (string) is ⍵:
≢⍵
count the number of characters in the string
⍳
generate that many ɩntegers
i←
store in i
⌽
reverse
i⌊
pairwise minimum with i
⍺⌊
pairwise minimum with the cap
⍵/⍨
use those numbers to replicate the letters of the string
F#, 96 bytes
let f(s:string)n=Seq.mapi(fun i c->System.String(c,Seq.min[i+1;s.Length-i;n]))s|>String.concat""
A port of Justin Mariners javascript answer
Java (OpenJDK 8), (削除) 101 (削除ここまで) 97 bytes
n->s->{int i=0,j;for(char c:s)for(j=s.length-i++,j=j<i?j:i,j=j<n?j:n;j-->0;)System.out.print(c);}
So much playing with indices...
Abalone is a type of fish (well, a shellfish), therefore...
><>, 79 bytes
&i1\
0(?\:1+:&::&@)?$~i:@
&~}\&~1
0(?\:&::1+&@)?$~}}:
~r\
?!v>l?!;:o1ドル-:@
~~<^
Try it online, or watch it at the fish playground!
Reads the string from STDIN, and assumes the number is already on the stack.
Explanation: The second, fourth and sixth lines are the main loops. The details are some ugly stack manipulation, but in broad strokes, first, the second line fills the stack alternating between a character of input and min(i, n), where n is the length cap and i is the index of the character in the input: for "abalone", 3, the stack looks like
"a", 1, "b", 2, "a", 3, "l", 3, "o", 3, "n", 3, "e", 3, -1=EOF, 3
Next, line 4 goes through the stack in the same way in reverse, to get the right hand end capped properly:
"a", 1, "b", 2, "a", 3, "l", 3, "o", 3, "n", 2, "e", 1, -1
Then the sixth line takes each character–number pair and prints the character as many times as the number.
Ruby, 59 bytes
Port of xnor’s Python 2 answer.
F=->s,n,i=1{s[0]?s[0]*s[...i][...n].size+F[s[1..],n,i+1]:s}