29
3
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 er.
When the pairs are mostly different characters, the string has a chunky quality, e.g. chUnky.
When these pairs are mostly the same character, the string has a smooth quality, e.g. sssmmsss.
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).
For example, 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.
Challenge
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 of0.0000. - Choose chunkiness or smoothness as you prefer. Just be sure to say which one your program outputs.
The shortest code in bytes wins.
Examples
Peanut Butter → Chunkiness: 0.91666666666, Smoothness: 0.08333333333
chUnky → Chunkiness: 1.0, Smoothness: 0.0
sssmmsss → Chunkiness: 0.28571428571, Smoothness: 0.71428571428
999 → Chunkiness: 0.0, Smoothness: 1.0
AA → Chunkiness: 0.0, Smoothness: 1.0
Aa → Chunkiness: 1.0, Smoothness: 0.0
! → Chunkiness: 0.0, Smoothness: 1.0
[empty string] → Chunkiness: 0.0, Smoothness: 1.0
Bonus question: Which do you prefer, chunky or smooth strings?
8-1 No overhanded tag. – Dennis – 2015-07-14T02:45:57.070
22+1 Conclusive proof that chunky peanut butter should be the default. – BrainSteel – 2015-07-14T03:25:03.030
Some languages have a hard time reading no input at all. Would it be admissible to assume that the input is newline-terminated? – Dennis – 2015-07-14T04:25:04.097
@Dennis Yes, that's fine. – Calvin's Hobbies – 2015-07-14T04:32:42.003
9@BrainSteel Chunky should only be the default if you're a computer; they like having chunks available. Peanut butter made for people should hide those implementation details, and be smooth as silk for the user. – Geobits – 2015-07-14T13:01:11.870
Hm.. with all the fully chunky chunkiness solutions, I wonder whether there are fully smooth smoothness solutions?
– ojdo – 2015-07-15T14:12:44.680Will the input contain tabs? – kirbyfan64sos – 2015-07-30T02:00:11.033
@kirbyfan64sos Tabs aren't printable ASCII. So no. – Calvin's Hobbies – 2015-07-30T05:31:03.447