Page 2 of 5

Re: Code Puzzles

Posted: Sun Dec 06, 2015 2:06 pm
by davisdude
I can't claim the last puzzle as mine- it's an Euler project puzzle.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 1:18 pm
by NickRock
Try this without using a third variable to swap the numbers

Code: Select all

local a = 10
local b = 15

swap(a,b)
--[[
	Output:
	Current value of a is 15
	Current value of b is 10
]]

Re: Code Puzzles

Posted: Mon Dec 07, 2015 3:50 pm
by bartbes
You're probably going for the usual trick of a = a + b, b = a - b, a = a - b, but since lua has no call-by-reference semantics no such function can actually exist. (At least not without going into the debug library.)

Re: Code Puzzles

Posted: Mon Dec 07, 2015 4:00 pm
by zorg
bartbes wrote:You're probably going for the usual trick of a = a + b, b = a - b, a = a - b, but since lua has no call-by-reference semantics no such function can actually exist. (At least not without going into the debug library.)
So, you're saying that

Code: Select all

function swap(a,b) a, b = b, a end
won't work?

Edit: Ah, i see the real problem; yeah, would work with returning them.

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.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 4:03 pm
by undef
zorg wrote:
bartbes wrote:You're probably going for the usual trick of a = a + b, b = a - b, a = a - b, but since lua has no call-by-reference semantics no such function can actually exist. (At least not without going into the debug library.)
So, you're saying that

Code: Select all

function swap(a,b) a, b = b, a end
won't work?
No, but this will:

Code: Select all

function swap(a,b) _G.a, _G.b = b, a end

Edit:

No it won't, because the variables are local.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 4:05 pm
by zorg
undef wrote:No, but this will:

Code: Select all

function swap(a,b) _G.a, _G.b = b, a end
Except a and b are locals, not globals, so sadly that won't work either. The debug library could probably get the two upvalues and then it might be possible though, as bartbes said before.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 4:09 pm
by undef
Yep, just noticed that too... Debug libary then.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 4:24 pm
by NickRock
I just did this

Code: Select all

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
and worked perfectly fine

Re: Code Puzzles

Posted: Mon Dec 07, 2015 5:36 pm
by Ranguna259
NickRock wrote:I just did this

Code: Select all

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
and worked perfectly fine
Why did you give the answer :(
davisdude wrote:I can't claim the last puzzle as mine- it's an Euler project puzzle.
I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.

Re: Code Puzzles

Posted: Mon Dec 07, 2015 6:01 pm
by NickRock
Ranguna259 wrote:Why did you give the answer :(
Ah damn I'm sorry, I just wanted to make sure that I didn't make any mistakes explaining the puzzle :(