Skip to content

Instantly share code, notes, and snippets.

@lcharles123
Last active May 4, 2022 16:25
Show Gist options
  • Save lcharles123/6d26367f29aa41968631f0002abe4083 to your computer and use it in GitHub Desktop.
Save lcharles123/6d26367f29aa41968631f0002abe4083 to your computer and use it in GitHub Desktop.
Compute index of coincidence, python
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
"""
IC = sum i=1 to c(n_i * (n_i-1)) / N(N-1)/c
c = len(alphabet)
N = len(text)
"""
from collections import Counter
from functools import reduce
import string
enc = "kyfiuq eeecbl fmoezog eof ia favdivib zigmsdmtbkwnv rqstfrbkoy malweqek ibq keddkagkidume pbfin ta hum sfhyqr hn wanbwarfihv humbwm jtagvca qf sckqd ywf uiinbx ronvrrl vstfdmtbwbv gmsfdy pqhu i yfbuyaks dnxrw o dmtamanbvhoc fhxwfl ws hcdyugqqnbvtb ntivp vr xhgzzehxl wa vvsskqegnchzgdszsta ps va nqgf ieet yawjs tfd fhcbqqal rzsimiz pqehizf dxawtv gmsfdy bv bvvrysvztaqfggfjjvz wamb na n ykvztrwbr grfffxd figgmex rvsrxm ggcqjbk mt mps zifxottulmhga vsgkutnbs bn gjqyzoewul pr bfffe aqg gprxwj pefwbfbefhzzg mpog myjqkdiviz nxcqwtmtbwbf ws gcfxetv oyorgfr oontr pwaxhigcm ibl tblwtml gcarzvhoc deeihvwaxvzb saibawa hcefrbjigmq yc kte yqsyl bk qikpmibntlxwj rok vogqbsoc peymbfm qzfzzg pwfyl jff zu igkzhlvsu yus ycbqizjbkml pwfx wa hcuqbkmoxqal oep sxkiem gjzvoofuiaqpfhzanl qb aqajhvqnmpweblykf ehtvbbv rshvdew bvr canjvdsbbm bn znqyugtv kumej vv ial qbgzbiitqd mw hum jtfb af zmceor gcfxe am ueiqzokqd bv bvvrysvztaqfggfjjvz wbbv geb gotteewff lrlfvqs: hvs vv rqstfrbkoy malweqekqbt iai hyq ompse qa roktefihvkf nb eunxbsrvgmwifylql fpnsbfz bxooa pvx uimdnihr agzrzqs bv sympyfzoae mbtqajsiunz ih zqg bvvde am kbzxjr fz vtvbrdnw plehl lwsnrwsefitt oaiydnvd ag moetl fbrxoz kczxhysi ihbts fbhimzzg mps pwzuzzoammr nl utq turvcwga bk hyus tvoygmjf jtagvca lrxwxzew akvbpmwes cbzqhqgx preew wb owbqsj oogkscbf nb eunxbsrvgmwifylmjrv uj kiatx pwf unxhvds wmuemr yvveil i gluotzzo agizlavx cw deeim nvq xkzfcaqbt kvwqlutl i dnxrw tiam mpwf bujgze wta dhjyngyqd bv bvvrysvztaqfggrnuyf ig bvva jtfb ehtvbbv cwcmqd mpog pvx gnutvpwao pnftgima qbcyi pv gsxl hb avrdcufr bvr iewoesefmbg ws yvv qlxkhewzjqymnbkoy zrqope taih jmej ijqd mpsa qa yscqpawbr knqz iaumqbt ajnhttel vskb uj sobaglsq bung tanvmdg xetjzzg mpog bujgv oikkivbf hclxd lwzim nqz gdoutsza gmok nohtsnv nquvnrt kchtq xcche bv hum yfgk ohtxhrz uj diqsxvhrl qnoxdafa cs arasiml vqfpcvyg zzcecrvvt f tfgr uqh scyq oupek cgvvt yvze pkwdrzgd cw qlxkheqpfz jiimkvra gt wdblxusab ytuzo il bvr nhsrryegboy kbsqvbt mpog caisixixa oyt rqstfrhvwp lvlwkml vwaccgjfj ehtvbbvf bciw bxkozm gms wauglogqbs cw pizqhnt pnftgim lsfqts oj ut umqnur bwuqlr sbbea nb kte xtspbenqrx egowamrwwes chuahvvym ugrbvu nvq ftkqr pwfyl jff kio mps gprtfvfiviz eqttf fr saibawax kfdk lcdrzfjrvp tam oq pbh avfhhlg gpny vrp pkmjnqyjr gdeoqchayd vfiakl unzqssi oaetsq aufbeanl bvravx dfesbjzl buj afet budbzgfbk mnw izfw gms dasm vcgmq rojfeka humfng fr tam qrvgzfp ehtvbbv ejqvuvxl vva cmr wdof uwg qa sweqtxmbswhwhp hagvsiie gijt htl ghotjgkqd mpog aufbean lpchtq bciw og pwf lvxgvdttbwbv ny hyq chtr fxenbx takjce tngcimthzm vv bwrvd th lsimytd r yampszignqrx fhzahtnywfz fhz arvqjzzmn zmbrbvhg ktil zsfmnwqy delczgmq nb jtagvcaa cmr ktelqg piyqsu mn tturjef tfd tamcemgnqrx gxvsgqpx we zigmhrmakcldtr avnvatb sqctus n vnywfzae zsfmnwqy reetcj ig yvv unlbwgcgj tfd awdoakri gkgdr qb czvsqvfog vsj rrwgvk ig xfvvpjhfz saibawa mou fhx wdcweyieutr bc qqfhije hba wqmnx kzfh bvtycrshzml lkwrvgngke agl anbujarfivqoaa fzqy ms amfzias kvkl tvr wwus jfz nxcanva fbu te ttgb pni ctoalqcaiy jbtaugbsea jnhy mlumfg mvsgkqig ibq shwh xaewmz fpnsbfz whzyrl swsvxy tkfbaf iwjoiitwamf fbu fhba ooqynhp yar poim ptbkdiuchrl gt vze ltbse lrascapfmbg ws roktefihvknq werokuogqbs hyqokg"
ni = reduce(lambda acc, x: acc + Counter(x), enc, Counter())
soma = 0
N = 0
for l in string.ascii_lowercase:
soma += ni[l] * ni[l]-1
N += ni[l]
IC = soma / (N * (N-1)) / 26
print(IC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment