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 1318
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 discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by C Gillespie 
last post: by

18 posts
views
Thread by Joe 
last post: by

2 posts
views
Thread by James Marshall 
last post: by

2 posts
views
Thread by Matt Smolic 
last post: by

23 posts
views
Thread by Russ Chinoy 
last post: by

14 posts
views
Thread by James Thiele 
last post: by

12 posts
views
Thread by Steve Blinkhorn 
last post: by

4 posts
views
Thread by rognon 
last post: by

89 posts
views
Thread by Cuthbert 
last post: by

3 posts
views
Thread by BAnderton 
last post: by
          