def frequencies(ciphertext): freq=[0]*26 for i in ciphertext: int_letter = ord(i) - ord('a') freq[int_letter]+=1 return freq def index(freq, cipher_length): sum =0 i = 0 while(i<=25): sum = sum + freq[i]*(freq[i]-1) i+=1 denominator=cipher_length*(cipher_length-1) return float(sum)/(denominator) def key_length(c_index, c_l): k = (0.0265*c_l)/((0.065-c_index)+c_l*(c_index-0.0385)) return k def main(): ciphertextInput = input("input ciphertext ") ciphertextInput = ciphertextInput.replace(" ", "") ciphertextInput = ciphertextInput.replace("\t", "") ciphertextInput = ciphertextInput.replace(",", "") ciphertextInput = ciphertextInput.replace(";", "") ciphertextInput = ciphertextInput.replace("!", "") ciphertextInput = ciphertextInput.replace(":", "") ciphertextInputNoSpaces = ciphertextInput.replace(".", "") print("after removing all whitespaces and punctuations") print(ciphertextInputNoSpaces) ciphertext = ciphertextInputNoSpaces.lower() print("after transferring to lowercase letters only") print(ciphertext) cipher_length = len(ciphertext) print("cipher length is ") print(cipher_length) freq = frequencies(ciphertext) print("frequencies are ") print(freq) c_index=index(freq, cipher_length) print("index of coincindence is") print(c_index) print("key length ") print(key_length(c_index, cipher_length)) main()