| Bytes | Lang | Time | Link |
|---|---|---|---|
| 3216 | Nibbles | 240525T225325Z | DLosc |
| 086 | Python 3 | 181231T234643Z | Erik the |
| 041 | Pip | 151106T122051Z | DLosc |
| 131 | Python 2 | 151030T083957Z | TFeld |
| 181 | Haskell | 151028T140422Z | Xwtek |
| 200 | Haskell | 151027T154949Z | Leif Wil |
| 357 | TSQL | 151028T195127Z | Sam Cohe |
| 087 | JavaScript ES6 | 151027T044437Z | n̴̖̋h̷͉̃ |
| 057 | Gema | 151027T181801Z | manatwor |
| 082 | JavaScriptES6 | 151027T091835Z | edc65 |
| 043 | Retina | 151027T092550Z | Martin E |
Nibbles, 37 32 nibbles (16 bytes)
+.%~@\$n+:@?,$:`($.`\$:^+,\/$@a~
Explanation
Two $s are implicit at the end.
+.%~@\$n+:@?,$:`($.`\$:^+,\/$@a~$$
@ # First line of stdin
%~ # Split into pairs of runs of
\$n # alphanumeric characters
# each with its preceding run of non-alnum chars
. # Map to each (alnum, non-alnum) tuple
+ # and concatenate the results:
?,$ $ # If the alnum run is empty, keep it as-is
`($ # Else, remove the first character
: # and prepend it to this list:
`\$: # Scan the remainder on concatenation (i.e. get
# its prefixes)
. # and then map this function to each prefix:
/$@ # Last character
\ a # is a letter?
, # 1 if so, 0 if not
+ ~ # Plus 1 (2 if letter, 1 if not)
^ $ # Repeat argument that many times
:@ # Prepend the non-alnum run to that list
+ # Concatenate it all together
Python 3, 86 bytes
f=0
m=''
for i in input():g=i.isalnum();m=m*g*f+i*f;print(end=m*-~i.isalpha()or i);f=g
Pip, 41 bytes
aR`[^_\W]+`{Y0a@0.Ya@{1,++y}X2-(_Q+_)MJa}
Takes the text as a command-line argument. We use a regex replacement to replace all alphanumeric runs ([^_\W]+) with a callback function. This function maps another function to each character of the word, generating progressively larger runs of characters (with the global variable y keeping track of the index) and repeating them X2 if the character is not a number. (The logic for which uses + to convert to a number and then Q to check whether this is string-equal to the original character.) This process leaves out the first character, though, so we have to add it on afterward (a@0.).
Python 2, 131 Bytes
i=raw_input();s=r='';g=1
for c in i:
s+=c
if g:r+=c;g=0;s=''
elif c.isalpha():r+=s*2
elif '/'<c<':':r+=s
else:g=1;r+=c
print r
Loops through characters, and saves/repeats if required.
Haskell, 183 181 bytes
import Data.Char
b(x:y)|h x=x:c[]y|2>1=x:b y
b[]=[]
h y=isDigit y||isAlpha y
c x l@(y:z)|h y=c(y:x)z|2>1=c x[]++b l
c x[]=reverse$g$x
g[]=[]
g a@(x:y)|isDigit x=a++g y|2>1=a++a++g y
Usage:
b"Hello, world!"
>"Heeelelellellelloello, wooorororlorlorldorld!"
Don't know if it is essentially different with Leif Willerts' answer. But here's the algorithm.
- Search for valid letter(Letter or Num)
- If it letter, begin collecting letter. However, it is stored reversely, make the string reversed. And first item is omitted.
- As it hit invalid letter, do the question's transform with reversed input. The question's transform is reverse.g.reverse, but since it is already reversed, then we just do reverse.g
I somehow keep redudant brackets.
Haskell, 215 200 bytes
import Data.List
import Data.Char
c=concatMap
p c=[isDigit c,isAlpha c]
r(Just n)=c id.replicate(n+1)
r _=id
f(h:t)=h:(c(\s->r(findIndex id.p$last s)s).tail.inits)t
m=c f.groupBy(\a b->or$p a==or$p b)
It's a mess.
Have fun reading all the weird quirks, I never thought I would use id this much.
Too bad the stuff that already does half the work here needs to be imported (isDigit,isAlpha,inits).
T-SQL, 357 bytes
create proc x(@s varchar(99),@z int=0,@i int=0,@y varchar(99)='',@x varchar(99)='',@w varchar(99)='') as
while LEN(@s)>0
begin
select @z=patindex('%[^0-z]%',@s+' '),@i=1,@w+=LEFT(@s,1)
while @i<@z-1
begin
select @y=SUBSTRING(@s,2,@i)
,@x+=@y
,@i+=1
if @y like'%[a-z]'set @x+=@y
end
select @w+=@x+SUBSTRING(@s,@z,1)
,@x=''
,@s=REPLACE(@s,left(@s,@z),'')
end
select @w
ex:
exec x(@s='1337 numb3r5')
returns 1333337 nuuumumumbumbumb3umb3rumb3rumb3r5
JavaScript (ES6), 92 88 87
f=>f.replace(/[^_\W]+/g,m=>m[0]+[...m].slice(1).reduce((a,e)=>a+=(y+=e,++e?y:y+y),y=''))
I have no idea how to golf this down...
Thanks Mwir247 for the golfed down regex and ETHproductions for one byte golf in reduce function.
Gema, 57 characters
\X?=?@set{s;}
<D1>=@append{s;$0}$s
<L1>=@append{s;$0}$s$s
Sample run:
bash-4.3$ gema '\X?=?@set{s;};<D1>=@append{s;$0}$s;<L1>=@append{s;$0}$s$s' <<< '1337 numb3r5'
1333337 nuuumumumbumbumb3umb3rumb3rumb3r5
JavaScript(ES6) 82
Using the regexp optimization suggested by Mwr247
s=>s.replace(/([\W_])|./g,(x,y)=>y?(f=0,x):f?(p+=x,p+(-x-1?z:p)):(p=z,f=x),f=z='')
Test running the snippet below in any recent browser
F=s=>s.replace(/([\W_])|./g,(x,y)=>y?(f=0,x):f?(p+=x,p+(-x-1?z:p)):(p=z,f=x),f=z='')
// document.write(F(prompt('Insert string')))
// TEST
console.log=x=>O.innerHTML+=x+'\n'
function test()
{
O.innerHTML=F(I.value)
}
;[
['Mess up text','Meeesesessess upp teeexexextext']
,['This is some longer text.',
'Thhhihihishis iss sooomomomeome looononongongongeongeongeronger teeexexextext.']
,['CAPS LOCK && "Punc-tua"+\'tion\'',
'CAAAPAPAPSAPS LOOOCOCOCKOCK && "Puuunununcunc-tuuuaua"+\'tiiioioionion\'']
,['CaPs wItHIn loWERs'
,'CaaaPaPaPsaPs wIIItItItHItHItHIItHIItHInItHIn loooWoWoWEoWEoWERoWERoWERsoWERs']
,['1337 numb3r5','1333337 nuuumumumbumbumb3umb3rumb3rumb3r5']
,['abcdefghij 0123456789'
,'abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefgbcdefghbcdefghbcdefghibcdefghibcdefghijbcdefghij 0112123123412345123456123456712345678123456789']
,['Code-golf is the best!'
,'Cooodododeode-gooolololfolf iss thhhehe beeesesestest!']
].forEach(t => (
i=t[0],x=t[1],r=F(i),
console.log('Test ' + (r==x?'OK':'Fail')+'\nInput: '+i+'\nResult: '+r+'\nCheck: '+x+'\n')
))
#I { width: 50% }
Your test<input id=I><button onclick="test()">-></button>
<pre id=O></pre>
Retina, 43 bytes
.(?<=[^_\W]([^_\W]+))(?<=(\1)(?<=\D))?
$1$2
Run the code from a single file with the -s flag. Since this is just a single regex substitution, you can test it here (click the Context tab to see the results).
Explanation
This matches any digit and letter which is not the first in a run. While . can match any non-linefeed character, the lookbehinds ensure the other conditions:
(?<=[^_\W]([^_\W]+))
This matches backwards from the position after the .. First it matches one or more alphanumeric characters and captures them into group 1 with ([^_\W]+). This immediately ensures that the . corresponds to an alphanumeric character itself. Then the additional [^_\W] makes sure that there is one more alphanumeric character in the current run, which we don't include the match. Now group 1 is what we want to replace the match with if it is a digit - if it is a letter we want to replace it with twice this string. That's where the second lookbehind comes into play:
(?<=(\1)(?<=\D))?
This one is optional, so if it fails it won't affect the match at all. It first ensures that the . was not a digit via (?<=\D) - so this lookbehind is only relevant when we're matching letters. In that case, we match group \1 once more (this always matches, because we've captured the group from the same position), but capture it into group 2.
Hence, we simply replace the regex with the contents of both groups ($1$2). If the match was a digit, $2 will still be empty and we write back the prefix only once. If it was a letter, $2 is the same as $1 and we write it back twice.