Chunky vs. Smooth Strings
CJam, 19 bytes
q_1>_@.=:!1b\,d/4mO
100% chunky source code that calculates chunkiness.
Try this chunky goodness online.
How it works
q_ e# Read from STDIN and push a copy.
1>_ e# Discard the first character and push a copy.
@ e# Rotate the original on top.
.= e# Vectorized comparison (1 if equal, 0 if not).
:! e# Mapped logical NOT (0 if equal, 1 if not).
1b e# Base 1 conversion (sum).
\, e# Swap with the shortened string and push its length.
d/ e# Cast to double and divide.
4mO e# Round to four decimal places.
Obviously, NaN rounded to 4 decimal places is 0.
Pyth, 13 12 bytes
csJnVztz|lJ1
Fully chunky code calculating chunkiness.
Demonstration. Test harness.
csJnVztz|lJ1
Implicit: z = input()
nV n, !=, vectorized over
z z
tz z[:-1]
V implitly truncates.
J Store it in J.
s sum J
c floating point divided by
| logical or
lJ len(J)
1 1
TI-BASIC, 46 bytes
Input Str1
If 2≤length(Str1
mean(seq(sub(Str1,X,1)=sub(Str1,X-1,1),X,2,length(Str1
Ans
sub(x1,x2,x3
gives the substring of string x1
starting (one-based) at number x2
and ending at number x3
, then seq(
builds a sequence.
Gives the smoothness value. The Ans
variable is 0
by default, so we don't need an Else
to the If
statement, or to store anything to Ans
beforehand.