This function find the entropy given the string given by the formula: https://www.reddit.com/r/dailyprogrammer/comments/4fc896/20160418_challenge_263_easy_calculating_shannon/ Parameters ------- s: str String of which we have to find shannon entropy Returns --
(s)
| 319 | |
| 320 | |
| 321 | def entropy(s): |
| 322 | """ |
| 323 | |
| 324 | This function find the entropy given the string given by the formula: |
| 325 | https://www.reddit.com/r/dailyprogrammer/comments/4fc896/20160418_challenge_263_easy_calculating_shannon/ |
| 326 | |
| 327 | Parameters |
| 328 | ------- |
| 329 | s: str |
| 330 | String of which we have to find shannon entropy |
| 331 | |
| 332 | Returns |
| 333 | -------- |
| 334 | int |
| 335 | integer which will represent the randomness of the string, higher value will be high randomness. |
| 336 | """ |
| 337 | return -sum(i / len(s) * log2(i / len(s)) for i in Counter(s).values()) |
| 338 | |
| 339 | |
| 340 | def getDomain(url): |
no outgoing calls
no test coverage detected