147 posts
Location
My Computer
Posted 23 June 2015 - 05:10 PM
On the stackexchange code golf website there was a topic called "
stretching words" and here is the basic concept:
Write a program or function that duplicates letters in a word, so that all the duplicated letters arranged in order would form the input array.
For example:
Input: abcdefghi, abc
Output: aabbccdefghi
Here is my shot at it in lua (76 bytes):
function f(x,y)for i=1,#x do g=y:sub(i,i);x=x:gsub(g,g..g,1)end print(x)end
My question is, can this code be golfed down anymore/is there a different way to approach this that is shorter?
Thanks!
227 posts
Location
Germany
Posted 23 June 2015 - 05:25 PM
How about this? That's 51 bytes:
function f(a,b)print(a:gsub("["..b.."]","%1%1"))end
Edited on 23 June 2015 - 03:26 PM
147 posts
Location
My Computer
Posted 23 June 2015 - 05:38 PM
Returns the correct string, but also returns an extra int. I'm pretty sure that isn't allowed
227 posts
Location
Germany
Posted 23 June 2015 - 06:01 PM
Well, that's the return of gsub. If you run this
function f(a,b)return a:gsub("["..b.."]","%1%1")end
local str = f( "abcdef", "ab" )
print( str )
It'll just print 'aabbcdef'. So that's a property of print.
However if you only want one return value, that's still 58 bytes
function f(a,b)print(({a:gsub("["..b.."]","%1%1")})[1])end
or with a return ( 57 bytes ):
function f(a,b)return({a:gsub("["..b.."]","%1%1")})[1]end
Edited on 23 June 2015 - 04:03 PM
1426 posts
Location
Does anyone put something serious here?
Posted 23 June 2015 - 06:09 PM
function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.
Edited on 23 June 2015 - 04:10 PM
81 posts
Posted 23 June 2015 - 06:45 PM
How about this? That's 51 bytes:
function f(a,b)print(a:gsub("["..b.."]","%1%1"))end
Untested but this should work
function f(a,b)print(a:gsub("["..b.."]","%1%1")[1])end[\code]
Since it'll only take the first return from gsub rather than gsub plus the number if string cut into
227 posts
Location
Germany
Posted 23 June 2015 - 06:51 PM
function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.
Didn't know that property of brackets yet - interesting.
Untested but this should work
function f(a,b)print(a:gsub("["..b.."]","%1%1")[1])end
Since it'll only take the first return from gsub rather than gsub plus the number if string cut into
Won't actually work - I already posted the proper way:
function f(a,b)print(({a:gsub("["..b.."]","%1%1")})[1])end
Edited on 23 June 2015 - 04:51 PM
81 posts
Posted 24 June 2015 - 02:36 AM
function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.
Didn't know that property of brackets yet - interesting.
Untested but this should work
function f(a,b)print(a:gsub("["..b.."]","%1%1")[1])end
Since it'll only take the first return from gsub rather than gsub plus the number if string cut into
Won't actually work - I already posted the proper way:
function f(a,b)print(({a:gsub("["..b.."]","%1%1")})[1])end
Didn't think the extra parentheses and braces were needed, good to know!