[Tutor] Best way to store space locations?
W W
srilyk at gmail.com
Thu May 29 17:46:23 CEST 2008
For kicks and giggles/exercise, I'm creating a program that will
decrypt a simple cypher (i.e. cesar cypher/rot13, or a=1, b=2, c=3),
right now I've got my letter count, but I foresee a slight issue:
dealing with spaces.
For instance: "The quick brown fox jumps over the lazy dog" will give this data:
(encoded as rot13)
Gur dhvpx oebja sbk whzcf bire gur ynml qbt
[(8, ' '), (4, 'b'), (3, 'r'), (2, 'u'), (2, 'h'), (2, 'g'), (2, 'e'),
(1, 'z'), (1, 'y'), (1, 'x'), (1, 'w'), (1, 'v'), (1, 't'), (1, 's'),
(1, 'q'), (1, 'p'), (1, 'o'), (1, 'n'), (1, 'm'), (1, 'l'), (1, 'k'),
(1, 'j'), (1, 'i'), (1, 'f'), (1, 'd'), (1, 'c'), (1, 'a')]
The specific part I'm looking at is the location of spaces... what
would be the best way to keep the distribution? I have a few options
that I know of:
1) Store the locations of spaces in a list or a tuple in the
dictionary. This seems horribly complex and perhaps slow(ish)?
2) When the program figures out the most likely key, I could have it
reiterate over the original string, replacing letters. This also seems
like it would be slow... actually, now that I think of it, perhaps
this is the best method?
Once the program creates the dictionary to convert values, I could
have it loop something similar to this:
for letter in mystring:
if letter = ' ':
newstring += letter
else:
newstring += cypher_key(letter)
Would that be the optimal conversion?
TIA,
Wayne
--
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
More information about the Tutor
mailing list