|
| 17 Oct 2015 05:38 AM |
When a function is 19 lines long, I can't help but feel I went overboard. Is there a simpler way of accomplishing it?
My code is here: http://pastebin.com/Mz3Q9hei
It takes a table of strings as the argument, and finds the largest substring that exists in each one. If there are multiple strings that are larger than the rest, but of equal length, it will return a tuple. |
|
|
| Report Abuse |
|
|
FlyNormal
|
  |
| Joined: 30 Sep 2015 |
| Total Posts: 344 |
|
|
| 17 Oct 2015 06:35 AM |
| That is pretty impressive. I'm not sure if I can attain a more efficient and a shorter code than that. The logic used behind your function is pretty strict and effective. |
|
|
| Report Abuse |
|
|
brados123
|
  |
| Joined: 24 Nov 2012 |
| Total Posts: 2143 |
|
| |
|
|
| 17 Oct 2015 06:45 AM |
u just showin off
i make u math.sqrt() |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 07:47 AM |
| lol Thanks guys, but are you sure there isn't a better way? I'm certainly not the best scripter here, for sure there's a shorter way? My function takes over half the script editor XD |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 07:49 AM |
| I think I can change those 2 if statements to an if-elseif, because if it doesn't match, it'll break anyway. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 08:06 AM |
| I'm I'm positive it's not the most efficient. I sacrificed some efficiency to make it take up less of the script editor. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 08:09 AM |
Yeah, here's an 18 liner:
http://pastebin.com/4GpCe7i2 |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 08:11 AM |
well, I'm not a master at reading someone elses code, so to actually come up with an accurate diagnoses, I'd have to recreate it myself, which I feel lazy to do, but from what I saw though, it's all pretty good
"My Life is going Good... but..." |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 08:19 AM |
I can't help but feel as if this part here can be simplified:
for i = 1, #firststring do for ii = i, #firststring do matches[#matches+1] = firststring:sub(i, ii) end end
Can that be turned into 1 loop? |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 09:26 AM |
yeah, OF COURSE THERE IS! just shove it all into one line |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 09:30 AM |
function FindLargestSubstring(strings) local matches, substrings, length, firststring = {}, {}, 0, strings[1]; for i = 1, #firststring do; for ii = i, #firststring do; matches[#matches+1] = firststring:sub(i, ii);end end for i,v in pairs(matches) do; for ii,vv in pairs(strings) do; if not vv:match(v) then break; end if not next(strings, ii) then; length, substrings, substrings[#substrings+1] = #v > length and #v or length, #v > length and {v} or substrings, #v == length and v or nil; end end end ; return unpack(substrings) end
there you go buddy |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 09:58 AM |
@Above I can make that shorter by removing ;s and some whitespace.
Besides, when beautified that still expands out. It don't count lol. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:04 AM |
"I can make that shorter by removing ;s and some whitespace.
Besides, when beautified that still expands out. It don't count lol."
the ;s end the variables so that you aren't including random values
and what does this term 'beautified' mean |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:07 AM |
You clearly don't understand the use of ;
And beautified means when a working beautifier plugin runs it, it creates a new script, and instead of 1 line, it will look readable. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:08 AM |
"You clearly don't understand the use of ;" its to close off lines/variables, of course |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:11 AM |
| The only REAL use of ; is to solve ambiguous syntax errors |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:12 AM |
"The only REAL use of ; is to solve ambiguous syntax errors"
I'm pretty sure I know my python and c# code :/ |
|
|
| Report Abuse |
|
|
| |
|
|
| 17 Oct 2015 10:24 AM |
"But sadly not Lua"
I'm pretty sure I know lua too :/
it is used to end lines and variables so that they don't get confused
just like in python and c# but lua automatically puts it at the end of lines for you invisibly, anyways |
|
|
| Report Abuse |
|
|
| |
|
|
| 17 Oct 2015 10:28 AM |
"Lua does no such thing..."
think what you want dude :/ but lua is a scripting language and it is designed to already do all these tthings for you ://// |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:36 AM |
| Lua doesn't insert semicolons. cntkillme said that. If you want to argue with him go ahead but I swear the only thing semicolons do (and no; they don't improve speed!) is solve ambiguous syntax errors. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2015 10:39 AM |
"but lua automatically puts it at the end of lines for you invisibly, anyways"
trolol |
|
|
| Report Abuse |
|
|
| |
|