PDA

View Full Version : Python 3 --> Ackermann Function



danbaron
16-06-2010, 21:39
[font=courier new][size=8pt]Here is a program that does exactly the same thing as the thinBasic script, here:

http://community.thinbasic.com/index.php?topic=3403.0

Only, this one is written in Python 3.

(Python 3.x is not necessarily backwardly compatible with Python 2.x.)

:oops:
Dan :x :P


#------------------------------------------------------

def ack1(m, n):
if (m < 0) or (n < 0):
return -1
if m == 0:
return n + 1
if n == 0:
return ack1(m - 1, 1)
return ack1(m - 1, ack1(m, n - 1))

#------------------------------------------------------

def ack2(m, n):
s = &#91;]

if (m < 0) or (n < 0):
return -1

while 1:

if m == 0:
if len(s) == 0:
return n + 1
else:
m = s.pop(0)
n += 1
continue

if n == 0:
m -= 1
n = 1
continue

s.insert(0, m - 1)
n -= 1

#------------------------------------------------------

print(" ack1 ack2")
print()
for i in range(0, 4):
for j in range(0, 8):
print('ack(%1d, %1d) = %04d %04d' % (i, j, ack1(i, j), ack2(i, j)))
print('ack(%1d, %1d) = %04d %04d' % (4, 0, ack1(4, 0), ack2(4, 0)))

print()
print("Done.")
print("Press a key.")
input()

#------------------------------------------------------

kryton9
17-06-2010, 03:13
Please tell me they got rid of using underscores and double underscores. That is why I never really got into python, I see that in the code listings and it pushes me away.

danbaron
17-06-2010, 07:28
[font=courier new][size=8pt]I'd like to be able to tell you that, Kent, but I don't think I can.

I don't like any kind of underscores.

For instance, I don't like the identifier, "long_black_train".

One thing I like about Lisp, is that you can have dashes ("-") in names. And, the language uses them for its library functions. So, you could have the identifier, "long-black-train".

For my own identifiers, I never make one that requires me to use the shift key. And, I don't know how to type, either. I use only one finger from each hand. And, normally, I never indent any code (Python forces you to, but, I got the Wingware Personal Python IDE ($35), and it does it for you).

I do remember that Python uses built-in identifiers, like, "__self__", (4 underscores). I agree, they test your patience.

Some languages force you to use mixed case, like, "openWindow" (Java). That is almost beyond my capacity.

Here are the changes in Python 3.0.

http://docs.python.org/py3k/whatsnew/3.0.html

kryton9
17-06-2010, 17:22
Looks like they removed some underscores and put them back in other places, too bad.

Charles Pegge
17-06-2010, 18:04
_I_ use a few of these scruffy beasts in Oxygen for system variables and labels. Normally you will never see them. The underscores are there to prevent conflicts with user defined names.

__Charles

kryton9
17-06-2010, 22:48
I know Charles, that is what Python does too to some extent. I would rather see something like sysName instead of _Name_ or resName for reserved or even them spelled out systemName or reservedName a lot easier on the eyes if you ask me. Look at this code from python, yikes!

>>> class MyClass():
... def __init__(self):
... self.__superprivate = "Hello"
... self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_myClass__superprivate': 'Hello', '_semiprivate': ', world!'}