Jump to content




Can this program be golfed any more?


  • You cannot reply to this topic
7 replies to this topic

#1 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

Posted 23 June 2015 - 04: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!


#2 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 23 June 2015 - 04:25 PM

How about this? That's 51 bytes:
function f(a,b)print(a:gsub("["..b.."]","%1%1"))end

Edited by wieselkatze, 23 June 2015 - 04:26 PM.


#3 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

Posted 23 June 2015 - 04:38 PM

Returns the correct string, but also returns an extra int. I'm pretty sure that isn't allowed

#4 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 23 June 2015 - 05: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 by wieselkatze, 23 June 2015 - 05:03 PM.


#5 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 23 June 2015 - 05:09 PM

function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.

Edited by SquidDev, 23 June 2015 - 05:10 PM.


#6 jerimo

  • Members
  • 74 posts

Posted 23 June 2015 - 05:45 PM

View Postwieselkatze, on 23 June 2015 - 04:25 PM, said:

How about this? That's 51 bytes:
function f(a,b)print(a:gsub("["..b.."]","%1%1"))end
Untested but this should work
[code]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

#7 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 23 June 2015 - 05:51 PM

View PostSquidDev, on 23 June 2015 - 05:09 PM, said:

function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.

Didn't know that property of brackets yet - interesting.

View Postjerimo, on 23 June 2015 - 05:45 PM, said:

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 by wieselkatze, 23 June 2015 - 05:51 PM.


#8 jerimo

  • Members
  • 74 posts

Posted 24 June 2015 - 01:36 AM

View Postwieselkatze, on 23 June 2015 - 05:51 PM, said:

View PostSquidDev, on 23 June 2015 - 05:09 PM, said:

function f(a,b)print((a:gsub("["..b.."]","%1%1")))end
Note extra brackets.

Didn't know that property of brackets yet - interesting.

View Postjerimo, on 23 June 2015 - 05:45 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users