Consider a string of length N, such as
Peanut Butter with N = 13. Notice that there are N-1 pairs of neighboring characters in the string. For
Peanut Butter, the first of the 12 pairs is
Pe, the second is
ea, the last is
When the pairs are mostly different characters, the string has a chunky quality, e.g.
When these pairs are mostly the same character, the string has a smooth quality, e.g.
Define the chunkiness of a string to be the ratio of the number of pairs with two different characters to the total number of pairs (N-1).
Define the smoothness of a string to be the ratio of the number of pairs with two identical characters to the total number of pairs (N-1).
Peanut Butter only has one pair with identical characters (
tt), so its smoothness is 1/12 or 0.0833 and its chunkiness is 11/12 or 0.9167.
Empty strings and strings with only one character are defined to be 100% smooth and 0% chunky.
Write a program that takes in a string of arbitrary length and outputs either its chunkiness or smoothness ratio as a floating point value.
- Take input via stdin or the command line, or you may write a function that takes a string.
- You can assume the input string only contains printable ASCII characters (and hence is single-line).
- Print the float to stdout to 4 or more decimal places, or you can choose to return it if you wrote a function. Decimal places that convey no information are not required, e.g.
0is fine instead of
- Choose chunkiness or smoothness as you prefer. Just be sure to say which one your program outputs.
The shortest code in bytes wins.
Peanut Butter → Chunkiness:
chUnky → Chunkiness:
sssmmsss → Chunkiness:
999 → Chunkiness:
AA → Chunkiness:
Aa → Chunkiness:
! → Chunkiness:
[empty string] → Chunkiness: