Page 3 of 5

Re: Code Puzzles

Posted: Mon Dec 07, 2015 7:12 pm
by Ranguna259
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.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 7:18 pm
by zorg
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)
Put that into http://www.lua.org/cgi-bin/demo
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)

Re: Code Puzzles

Posted: Mon Dec 07, 2015 8:02 pm
by NickRock
Oh that explains everything, well looks like I did something wrong sorry :P

Re: Code Puzzles

Posted: Tue Dec 08, 2015 4:08 am
by davisdude
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.
I pretty much explained it in the code's comments, but here's the actual post.

Re: Code Puzzles

Posted: Tue Dec 08, 2015 1:25 pm
by Ranguna259
davisdude wrote:
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.
I pretty much explained it in the code's comments, but here's the actual post.
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 13 :rofl:

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
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)
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.
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)
But what if the variables had arbitrary names like abel and bill ?

Code: Select all

local abel = 15
local bill = 10
swap(abel,bill)
print(abel) --10
print(bill) --15
We'd probably have to use the debug library.

Re: Code Puzzles

Posted: Tue Dec 08, 2015 3:09 pm
by zorg
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. :3

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 :3

Re: Code Puzzles

Posted: Tue Dec 08, 2015 3:56 pm
by Ranguna259
zorg 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. :3
...
Gotcha :D
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.
...
That rule possibly renders the puzzle impossible to solve, at least in Lua.
zorg wrote:...
p.s., your quote's off a bit :3
It is ? Damn..
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 :P

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
EDIT: I did this one without testing it, so it might prove itself to be difficult..

Re: Code Puzzles

Posted: Tue Dec 08, 2015 9:17 pm
by davisdude

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
There's probably a better way to do this, but oh well.

Re: Code Puzzles

Posted: Tue Dec 08, 2015 10:38 pm
by airstruck

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
Still feels like there's an easier way.

Re: Code Puzzles

Posted: Tue Dec 08, 2015 11:02 pm
by davisdude
airstruck wrote:-insert amazing code here-
Still feels like there's an easier way.
I have been defeated...