trying to make a hash type algorithm without encryption

What I got so far:
Thinking I may add the regular expressions so any string can be returned as an integer.
Trying to make a consistent algorithm to return a integer for the value of a string:
2-12-18:  Working on a way to test it...  test so far at the bottom.  (pytest).  Change made, error found
still testing.  Purple words are adjustment.
2-12-18:  Newest version... still needs testing.  Think I'm way closer.
2-13-18:  All of this, was because I was trying to find a good way to use strings as keys in a binary tree... but they have to be converted to integers to be placed... python hash is semi-random so it just seemed wrong. Zed actually gave me a link in class the other night to 'ord ()' but it just now popped back in my head.  I came up with this thing today trying to look up a way to simply do it without this stuff I was playing with:   'ConvertString'
2-19-18:  Results today worked well. 



alist = ['Amy', 'Axle', 'Barb', 'Corwin', 'Peter', 'Zoey']
## go from last letter to first...  Add the ords by place value.
## ones' place.... last letter,  ten's place,  next  to last... so on... and on..
def ConvertString(astring):
word = list(astring)
numeric = 0
length = len(word) - 1
place = 1
for letter in word:
x = ord(word[length])
print(x)
numeric = numeric + x * place
length -= 1
place = place * 10
return numeric

## manual run ###
for i in range(0, len(alist)):
x = alist[i]
print(x)
print(ConvertString(x))








class AssignInteger(object):
#alphalexicon = [0, 1, 2, 'z','0','y','1','x','2','w','3','v','4','u','5','t','6','s','7','r','8','q','9','p',24,'o',26,'n',28,'m',
#30,'l',32,'k',34,'j',36,'i',38,'h',40,'g',42,'f',44,'e',46,'d',48,'c',50,'b',52,'a']
alphalexicon = [0, 1, 2, 'z','0','y','1','x','2','w','3','v','4','u','5','t','6','s','7','r','8','q','9','p','o','n','m',
'l','k','j','i','h','g','f','e','d','c','b','a']
# maybe use regex to strip non alphas?
def assign(self, word):
word = word.lower()
words = list(word)
alpha = AssignInteger.alphalexicon
print(word)
length_alpha = len(alpha) - 3
length_word = len(words)
propegate = length_alpha**length_word
prepegate = length_alpha**(length_word - 1)
numeric = prepegate
for i in range(0, length_word):
letter = words[i]
# a = 35, z = 3
if letter in alpha:
x = alpha.index(letter)
#y = length_alpha//x + 1
b = (length_alpha**i)
numeric += (b * x)
return numeric

# new alphalexicon is 35 long...  35 possibles with one character
# 1190 about possibles with 2 characters...  
# each minimal character should start with lowest possible then add index's?





yack = AssignInteger()
a = yack.assign('a')
z = yack.assign('z')
print(a, z)

aa = yack.assign('aa')
zz = yack.assign('zz')

print(aa, zz)

aaa = yack.assign('aaa')
zzz = yack.assign('zzz')

print(aaa, zzz)

aaaa = yack.assign('aaaa')
zzzz = yack.assign('zzzz')
print(aaaa, zzzz)


aaaaa = yack.assign('aaaaa')
zzzzz = yack.assign('zzzzz')
print(aaaaa, zzzzz)

aaaaaa = yack.assign('aaaaaa')
zzzzzz = yack.assign('zzzzzz')
print(aaaaaa, zzzzzz)




#####################  pytest file #####################


from AssignInteger import AssignInteger

ALPHALIST = ['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']
NUMERALS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
new = AssignInteger()
empty = []
empty2 = []

# because each case will return a unique integer from the AssignInteger...
# and all new cases are changed by the factor of the length of the word/string
# no duplicates will be made.  


def test_alphabet():
result = True
for i in range(0, len(ALPHALIST)):
letter = ALPHALIST[i]
x = new.assign(letter)
if x in empty:
result = False
empty.append(x)
else:
empty.append(x)

assert result == True
result2 = True
for i in range(0, len(NUMERALS)):
numeral = NUMERALS[i]
y = new.assign(numeral)
if y in empty2:
result2 = False
empty2.append(y)
else:
empty2.append(y)
assert result2 == True



Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame