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 1392
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...@go oglemail.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
UnboundLocalErr or: 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,b 1,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,b 1,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 one-element 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://www-zo.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 technique in Javascript that I might
be able to use/adapt?
I have a timer set within a prototype thus:
;HControl.prototype.setFLTimeout = function() {
|
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 if they point to the same string variable.
Alternately, is there a way to access the call object in a function, i.e.
the object that function-local variables are a part of? How about the
chain of call objects, e.g. if function a() calls...
|
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 msgBox(dbprogram) call. When I try to display it
in the form in a text box I get a #Name? error instead. I was told I have to
write a function to display it. I wrote a public function in the startup
module to return the value of a given string variable. Below...
|
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 "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
|
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 particular and
involves automatically generated code. I know several other ways of
attacking my problem, but this would be the cleanest if it could be made
to work.
A little more context. I use C as the output of a code generating
system which...
|
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 to do :
Class.method(InstanceOfClass);
The method would then access a private function from Class by doing
something like
|
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 used."
Could anybody tell me why gets() function is dangerous??
Thank you very much.
Cuthbert
|
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 non-working example of what I'm trying to do:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |