Hi,
I m playing around with extended euclids algorithm from Knuth. I m
trying to build a function with a function inside it.
def exteuclid(m,n):
a,a1,b,b1,c,d = 0,1,1,0,m,n
def euclid(c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?
Many Thanks in advance,
Nathan 6 1371
Nathan Harmston wrote:
Hi,
I m playing around with extended euclids algorithm from Knuth. I m
trying to build a function with a function inside it.
def exteuclid(m,n):
a,a1,b,b1,c,d = 0,1,1,0,m,n
def euclid(c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?
Well, it would be simpler to pass through all the variables rather than
relying on variables in a wider scope.

Michael Hoffman
On Jun 24, 11:55 am, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,
I m playing around with extended euclids algorithm from Knuth. I m
trying to build a function with a function inside it.
def exteuclid(m,n):
a,a1,b,b1,c,d = 0,1,1,0,m,n
def euclid(c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?
Many Thanks in advance,
Nathan
ef outer():
a = 10
def inner():
print a
return inner
f = outer()
f()
output:
10
On Sun, 24 Jun, 7stud wrote:
ef outer():
a = 10
def inner():
print a
return inner
f = outer()
f()
output:
10
>>def outer():
.... a = 10
.... def inner():
.... a = a + 1
.... print a
.... return inner
....
>>f=outer() f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'a' referenced before assignment

Stefan Bellon
Nathan Harmston wrote:
Hi,
I m playing around with extended euclids algorithm from Knuth. I m
trying to build a function with a function inside it.
def exteuclid(m,n):
a,a1,b,b1,c,d = 0,1,1,0,m,n
def euclid(c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?
Many Thanks in advance,
Nathan
That last return statement does not match indentation of another block.
But this is probably what you mean:
def exteuclid(m,n):
x = 0,1,1,0,m,n
def euclid(c,d,x=x):
a,a1,b,b1,c,d = x
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
James
James Stroud wrote:
Nathan Harmston wrote:
def exteuclid(m,n):
x = 0,1,1,0,m,n
def euclid(c,d,x=x):
a,a1,b,b1,c,d = x
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)
James
My answer above is wrong because c and d take the wrong default values.
Also, you have some ambiguity in your code. Are nested calls to euclid
supposed to have the original values of a, a1, b, & b1, or are they to
take the original values 0, 1, 1, 0? This type of ambiguity is one
reason why the interpreter does not like reference before assignment.
This is my best guess at what you want because I'm not familiar with how
euclid's algorithm works:
def exteuclid(m,n):
def euclid(a,a1,b,b1,c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t  q * b
a = t  q * a
c,d,a1,b1 = d,r,a,b
return euclid(a,a1,b,b1,c,d)
return euclid(0,1,1,0,m,n)
Nathan Harmston wrote:
Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?
The canonical recommendations are: use attributes of the inner
function or oneelement lists as writable variables visible
in the outer function.
Another possibility is to modify the bytecode of the functions
by an appropriate decorator. See http://wwwzo.iinf.polsl.gliwice.pl/...ware/expose.py
(you'll need the byteplay module by Noam Raphael to make it work).
Marcin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: C Gillespie 
last post by:
Dear All,
I have 2 arrays
var A1 = new Array();
A1 ="Y2";
var B1 = new Array();
B1 ="Y1";
B1 ="sink";

by: Joe 
last post by:
Hi,
I am trying to alter the refresh rate of an online webpage in a webbrowser
control using MFC. However the Timer ID is stored in a local variable and I
don't know how to access it. Is there a...

by: James Marshall 
last post by:
Does anyone know....
In JavaScript, is there any way to get a reference to a string variable
(not an object), like Perl's "\" operator? I want to be able to compare
two such references and know...

by: Matt Smolic 
last post by:
02.06.04
I need some help displaying a public variable on a form. The variable is
declared and initilazied in a module at startup (and declared Public). I
have verified this with a...

by: Russ Chinoy 
last post by:
Hi,
This may be a totally newbie question, but I'm stumped.
If I have a function such as:
function DoSomething(strVarName) {
.....
}

by: James Thiele 
last post by:
I'd like to access the name of a function from inside the function. My
first idea didn't work.
>>> def foo():
.... print func_name
....
>>> foo()
Traceback (most recent call last):
File...

by: Steve Blinkhorn 
last post by:
Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function? Please no
homilies on why it's bad practice: the context is very...

by: rognon 
last post by:
Hi there,
I'm trying to do something, but I don't know if it's possible.
Basically, I want to have a public static class method that could
access a private object's method. I would like to be able...

by: Cuthbert 
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be...

by: BAnderton 
last post by:
Hello all,
Question: Is there any way to access a javascript variable from
within psp code?
I'm aware of how to do the reverse of this (js_var='<%=psp_var%>').
Here's a nonworking...

by: lllomh 
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{

by: Aliciasmith 
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...

by: tracyyun 
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...

by: giovanniandrean 
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...

by: NeoPa 
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...

by: nia12 
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...

by: NeoPa 
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

by: GKJR 
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
 