Brandon Williams
Brandon Williams

Reputation: 79

Can't understand why recursion depth is exceeding the limit

I have a program that is a simple encrypt algorithm that takes in a string and changes the letters into a new string of letters.

A function puts scrambled letters from the alphabet into the code table. In this function I have a checkRepeat function to ensure that a letter doesn't repeat. When I do this by hand (other than the random integer generation part) it makes sense but my computer doesn't like it and exceeds the 'recursion depth'.

from random import *;

string="Hello I am a computer";
alphTable=['a','b','c','d','e','f','g','h','i','j','k','l',
           'm','n','o','p','q','r','s','t','u','v','w','x',
           'y','z'];
def checkRepeat(array,val):
    global alphTable
    for i in range(len(array)):
        if val==array[i]:
            location=randint(0,25);
            array.append(alphTable[location]);
            checkRepeat(array,val);

def makeEncryptTable():
    encryptTable=[];
    global alphTable;
    for i in range (26):
        location=randint(0,25);
        encryptTable.append(alphTable[location]);
        checkRepeat(encryptTable,encryptTable[i]);
    return encryptTable;

array1=makeEncryptTable();
print(array1);

Here is the error code:

Traceback (most recent call last):
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 38, in <module>
    array1=makeEncryptTable();
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 35, in makeEncryptTable
    checkRepeat(encryptTable,encryptTable[i]);
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 27, in checkRepeat
    checkRepeat(array,val);

...

File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 27, in checkRepeat
    checkRepeat(array,val);
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 25, in checkRepeat
    location=randint(0,25);
  File "/usr/lib/python3.4/random.py", line 218, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.4/random.py", line 194, in randrange
    return istart + self._randbelow(width)
  File "/usr/lib/python3.4/random.py", line 228, in _randbelow
    if type(random) is BuiltinMethod or type(getrandbits) is Method:
RuntimeError: maximum recursion depth exceeded while calling a Python object

Upvotes: 2

Views: 855

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37529

You could do this very easily using the random and string libraries

import random
import string

encrypt_table = list(string.ascii_lowercase)
print encrypt_table
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

random.shuffle(encrypt_table)
print encrypt_table
# ['d', 'v', 'f', 't', 'c', 'r', 's', 'q', 'e', 'u', 'm', 'w', 'p', 'g', 'x', 'i', 'b', 'n', 'z', 'y', 'k', 'h', 'a', 'o', 'l', 'j']

Also, you don't need to end lines in python with semi-colons; it's heavily discouraged and makes your code harder to read. You should also avoid using global variables in most cases.

Also, if you're intending to use this as a cipher, you should probably use a dictionary instead of a list.

cipher = {k: v for k, v in zip(string.ascii_letters, encrypt_table}
word = 'test'
encrypted = ''.join(cipher[x] for x in word)
# 'yczy'

Or, just use the built-in cipher tools.

cipher = string.maketrans(string.ascii_lowercase, encrypt_table)
string.translate('test', cipher)
# 'yczy'

Upvotes: 1

Vasanth
Vasanth

Reputation: 1278

Your checkRepeat(array,val) does not have an exit condition.

You are calling the checkRepeat(array,val) with the same val every time and it matches the first element in the array, add another element to the array and repeats the same matching the first element and appending another element.

Upvotes: 1

Related Questions