Code Puzzles
Re: Code Puzzles
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
Re: Code Puzzles
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!!
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Code Puzzles
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.)
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Code Puzzles
So, you're saying thatbartbes 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.)
Code: Select all
function swap(a,b) a, b = b, a end
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 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
No, but this will:zorg wrote:So, you're saying thatbartbes 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.)won't work?Code: Select all
function swap(a,b) a, b = b, a end
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Code Puzzles
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.undef wrote:No, but this will:Code: Select all
function swap(a,b) _G.a, _G.b = b, a end
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
Yep, just noticed that too... Debug libary then.
Re: Code Puzzles
I just did this
and worked perfectly fine
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
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Code Puzzles
Why did you give the answerNickRock wrote:I just did thisand worked perfectly fineCode: 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
I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.davisdude wrote:I can't claim the last puzzle as mine- it's an Euler project puzzle.
Re: Code Puzzles
Ah damn I'm sorry, I just wanted to make sure that I didn't make any mistakes explaining the puzzleRanguna259 wrote:Why did you give the answer
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Who is online
Users browsing this forum: Semrush [Bot] and 5 guests