Given a text and a number N
, output, in a human readable format, the set of the character N-grams with their respective percentages.
The smallprint
- The text can be read from a file, stdin, passed as string parameter to a function or any other common way i forgot to mention
- The number
N
can be hardcoded, but only if it needs to be changed in one single place to change the functionality, the number counts as one byte regardless of the number of digits (T=input();N=100;f(T,N)
would count as 20 bytes, not 22) - No case sensitivity and all non-letter characters are treated the same. Inputs
Abra Cadabra!
andaBRa#caDAbrA5
should produce the same output. The character for non-letter can be any printable non-letter, not necessarily contained in the input - You may assume
N < text.length
- Output can be written to a file or displayed on screen. If you use a function you can
return
the result instead of printing as long as it is human readable (a JSON object or array like in the examples below is fine, a custom class or a sparse matrix with27^N
entries - not so much)
Examples with text = 'Abra Kadabra!'
N = 1
a 0.385
b 0.154
r 0.154
_ 0.154
k 0.076923
d 0.076923
N = 12
abra_kababra 0.5
bra_kadabra_ 0.5
N = 2
(dont worry about number format or rounding as long as it sums up to 0.99 < x < 1.01
)
ab 0.1666666
br 0.16666
ra 0.1666
a. 0.166667
.k 0.0833333
ka 0.0833333
ad 8.33333e-2
da 0.0833333
a tree structure is fine too
{'a':{'b':0.166, '.':0.166, 'd': 0.0833},
'b':{'r':0.166},
'r':{'a':0.166},
'.':{'k':0.0833},
'k':{'a':0.0833},
'd':{'a':0.0833}}
or a list of lists
[['A', 'B', 0.166], ['B', 'R', 0.166], ...]
N
how do we count the bytes? Without the value ofN
? Or for a single-digitN
? \$\endgroup\$