Re: Code Puzzles
Posted: Mon Dec 07, 2015 7:12 pm
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
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
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)
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)
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.
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)
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
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
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
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
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
I have been defeated...airstruck wrote:-insert amazing code here-
Still feels like there's an easier way.