Code Puzzles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

I can't claim the last puzzle as mine- it's an Euler project puzzle.
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
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Code Puzzles

Post 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
]]
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code Puzzles

Post 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.)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Code Puzzles

Post 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.
Last edited by zorg on Mon Dec 07, 2015 5:51 pm, edited 3 times in total.
Me and my stuff :3True 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.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Code Puzzles

Post 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.
Last edited by undef on Mon Dec 07, 2015 4:06 pm, edited 1 time in total.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Code Puzzles

Post 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.
Me and my stuff :3True 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.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Code Puzzles

Post by undef »

Yep, just noticed that too... Debug libary then.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Code Puzzles

Post 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
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Code Puzzles

Post 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.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Code Puzzles

Post 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 :(
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests