Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Faster way?
#1
I am playing with breaking ancient ciphers.
In this particular puzzle, it is not allowed for a letter to encrypt to itself.
I am using word patterns to get a list of candidates for each cipher word.
I want to throw out words that would violate this rule.
I tried using a while loop to iterate through each letter and see if they match or not. It is taking forever.
Is there a faster way? What is the fastest way to check for this?

Example:

XKKTU could be APPLE but AKKTU could not be APPLE, because it means the A would have encrypted to itself.
I want to go through the possible words that AKKTU could be and throw out apple form the list.
I just need to know the fastest way to compare the letters in the possible word with the encrypted word.

Thanks
Reply
#2
I'm not sure if this will speed things up, but assuming that you know that the encrypted and decrypted word are the same length, you could calculate the distance between the words, so that you can discard the words not fitting. The nltk library could help. example:
In [1]: import nltk

In [2]: word1 = "AKKTU"

In [3]: word2 = "APPLE"

In [4]: word3 = "XKKTU"

In [5]: nltk.edit_distance(word1, word2)
Out[5]: 4

In [6]: nltk.edit_distance(word3, word2)
Out[6]: 5

In [7]: nltk.edit_distance(word2, word2)
Out[7]: 0
As you can see the difference between the word and itself would be 0, ofcourse. The difference between AKKTU and APPLE would be 4, since one letter already fits. The difference between XKKTU and APPLE would be 5, since every letter is different. So if the difference and the length of the words fit together you know it should not be discarded. But beware:
In [8]: word4 = "apple"

In [9]: word5 = "XKKTUE"

In [10]: nltk.edit_distance(word2, word4)
Out[10]: 5

In [11]: nltk.edit_distance(word2, word5)
Out[11]: 5
the difference between apple and APPLE is five, since one is upper case and the other one is lower case. The distance between XKKTUE and APPLE is 5, since the E already fits. So make sure that there is a difference between lower and upper case and the length is equal.
As I mentioned, I don't know if this speeds things up, but it may be a little faster than comparing each letter seperately :)
Reply
#3
Turns out it was a bug causing the problem. Thanks for your help though. I think it is interesting.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020