No worries, that was a nice puzzle though.NickRock wrote:Ah damn I'm sorry, I just wanted to make sure that I didn't make any mistakes explaining the puzzle
Code Puzzles
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Code Puzzles
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Code Puzzles
zorg wrote:Edit2: After the answer being posted, i realized that defining the function in that chunk means that the function can access the two vars as upvalues, if one doesn't pass them in as arguments. (The answer though is wrong, since they are being passed in, and the printing happens -inside- the function; it would fail (as in, give the wrong answer) if it was called outside, after calling swap.
Code: Select all
local a = 10
local b = 15
function swap(a, b)
a = a + b
b = a - b
a = a - b
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap(a,b)
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
Then try with this:
Code: Select all
local a = 10
local b = 15
function swap()
a = a + b
b = a - b
a = a - b
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap(a,b)
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Code Puzzles
Oh that explains everything, well looks like I did something wrong sorry
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Re: Code Puzzles
I pretty much explained it in the code's comments, but here's the actual post.Ranguna259 wrote:I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Code Puzzles
Ahh, I thought that the number of consecutive digits were the number of digits of the product, that as kinda dumb because the second product has 11 digits and not 13davisdude wrote:I pretty much explained it in the code's comments, but here's the actual post.Ranguna259 wrote:I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.
Code: Select all
function getLargestProduct(data,n)
local digits = {}
local product = 1
local largestPossible = 9^n --largest possible product
local largestPreviouse = 0
for number in data:gmatch('%d') do --iterates the data string number by number
if #digits < n then
table.insert(digits,number)
else
table.remove(digits,1)
table.insert(digits,number)
end
if #digits == n then
for i,v in ipairs(digits) do
product = product*v
end
if product == largestPossible then
return product
else
largestPreviouse = product > largestPreviouse and product or largestPreviouse
end
product = 1
end
end
return largestPreviouse
end
You don't really need to pass a and b to the swap function since it has access to the scope where both variables were created.zorg wrote: ...Code: Select all
local a = 10 local b = 15 function swap() a = a + b b = a - b a = a - b print("Current value of a is " .. a .. "\nCurrent value of b is " .. b) end swap(a,b) print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
This would work too:
Code: Select all
local a = 10
local b = 15
function swap()
a = a + b
b = a - b
a = a - b
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap()
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
Code: Select all
local abel = 15
local bill = 10
swap(abel,bill)
print(abel) --10
print(bill) --15
Last edited by Ranguna259 on Tue Dec 08, 2015 3:59 pm, edited 1 time in total.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Code Puzzles
Ranguna: True, but realize, i only showed that the original puzzle's solution was flawed; the swap function being able to work with arbitrary variables was not a stated goal, nor that it needed to work outside of the chunk where a and b were defined.
But yes, to make the swap function work, you either need the debug library, or you could use the FFI to cast the two parameters to int pointers, then swap those inside the function... may or may not work, i haven't tested it, and technically, it may break the "no temporary variable" rule as well... and so would the debug lib usage.
p.s., your quote's off a bit
But yes, to make the swap function work, you either need the debug library, or you could use the FFI to cast the two parameters to int pointers, then swap those inside the function... may or may not work, i haven't tested it, and technically, it may break the "no temporary variable" rule as well... and so would the debug lib usage.
p.s., your quote's off a bit
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Code Puzzles
Gotchazorg wrote:Ranguna: True, but realize, i only showed that the original puzzle's solution was flawed; the swap function being able to work with arbitrary variables was not a stated goal, nor that it needed to work outside of the chunk where a and b were defined.
...
That rule possibly renders the puzzle impossible to solve, at least in Lua.zorg wrote:...
But yes, to make the swap function work, you either need the debug library, or you could use the FFI to cast the two parameters to int pointers, then swap those inside the function... may or may not work, i haven't tested it, and technically, it may break the "no temporary variable" rule as well... and so would the debug lib usage.
...
It is ? Damn..zorg wrote:...
p.s., your quote's off a bit
I was going crazy with the editor, it was deleting my post and I ended up pasting everything into a notepad and pasting it back into the editor again, so it's bound to be messed up, sorry about that
Finder Puzzle:
Code: Select all
data = "124621257235067"
for number,index1,index2 in data:finder('1241') do
print(number,index1,index2)
end
--[[Output:
1 1 1
1 6 6
2 2 2
2 5 5
2 7 7
2 10 10
12 1 2
12 6 7
4 3 3
24 2 3
124 1 3
1 1 1
1 6 6
Re: Code Puzzles
Code: Select all
local strmt = getmetatable( '' )
strmt.__index['finder'] = function( self, search )
local searchIndex = 1
local searchLength = 1
local searchLast = 0
return function()
while searchIndex <= #search do
local current = search:sub( searchIndex - searchLength + 1, searchIndex )
local start, stop = self:find( current, searchLast )
if start then
searchLast = stop + 1
return current, start, stop
else
if searchLength < searchIndex then
searchLength = searchLength + 1
else
searchIndex = searchIndex + 1
searchLength = 1
end
searchLast = 1
end
end
end
end
data = "124621257235067"
for number, index1, index2 in data:finder('1241') do
print( number, index1, index2 )
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: Code Puzzles
Code: Select all
getmetatable('').__index.finder = function (self, value)
return coroutine.wrap(function ()
for i = 1, #value do
for j = i, 1, -1 do
for a, b, c in self:gmatch('()(' .. value:sub(j, i) .. ')()') do
coroutine.yield(b, a, c - 1)
end
end
end
end)
end
Re: Code Puzzles
I have been defeated...airstruck wrote:-insert amazing code here-
Still feels like there's an easier way.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests