add trigraphcount

This commit is contained in:
Phillip Kühne 2018-10-29 23:22:23 +01:00
parent 40485c4172
commit ef1636dfef

View File

@ -45,10 +45,37 @@ def decrypt(string,key):
resultstring = resultstring+chr(c+97)
print(resultstring)
def trigraphcount(string):
distances = {}
for i in range(0,len(string)-2):
trigraph = string[i:i+3]
index = 0
while index < len(string):
index = string.find(trigraph, index)
if index == -1:
break
print(trigraph+' found at', index)
if not trigraph in distances and index!=i:
distances[trigraph] = index - i
index += 3
#print(distances)
print(sorted(distances.items(), key=lambda x: x[1]))
def lettercount(string,dist):
letters = {}
for i in range(0,len(string)-dist,dist):
print(string[i:i+dist])
for c in string:
if c in letters:
letters[c] = letters[c]+1
else:
letters[c] = 1
print(letters)
def main(argv):
key = ""
try:
opts, args = getopt.getopt(argv,"hk:e:d:",["key=","encrypt=","decrypt="])
opts, args = getopt.getopt(argv,"hk:e:d:t:l:",["key=","encrypt=","decrypt=","trigraphs=","lettercount="])
except getopt.GetoptError:
print(os.path.basename(__file__)+' -k <key> -e <plain text>')
print(os.path.basename(__file__)+' -k <key> -d <encrypted text>')
@ -64,5 +91,9 @@ def main(argv):
encrypt(arg,key)
elif opt in ("-d", "--decrypt"):
decrypt(arg,key)
elif opt in ("-t", "--trigraphs"):
trigraphcount(arg)
elif opt in ("-l", "--lettercount"):
lettercount(arg,int(key))
if __name__ == '__main__':
main(sys.argv[1:])