Balancing Words
Pure bash (no coreutils or other utilities), 125
Standard center of mass calculation using moments about the origin:
for((;i<${#1};w=36#${1:i:1}-9,m+=w,M+=w*++i)){ :;}
((M%m))&&echo $1 DOES NOT BALANCE||echo ${1:0:M/m-1} ${1:M/m-1:1} ${1:M/m}
Test output:
$ for t in \
> STEAD \
> CONSUBSTANTIATION \
> WRONGHEADED \
> UNINTELLIGIBILITY \
> SUPERGLUE
> do ./wordbal.sh $t; done
S T EAD
CONSUBST A NTIATION
WRO N GHEADED
UNINTELL I GIBILITY
SUPERGLUE DOES NOT BALANCE
$
Python 3, 124
w=input()
i=a=b=0
for c in w:n=ord(c)-64;a+=n;b+=n*i;i+=1
m=b//a
print(*[w[:m],w,w[m],"DOES NOT BALANCE",w[m+1:]][b%a>0::2])
This code doesn't test potential fulcrums, but rather finds the "center of mass" and checks if it's an integer. It does so by summing the total mass a
and the position-weighted mass b
, to find the center of mass m=b/a
. It then prints either the string split at position m
, or the string plus "DOES NOT BALANCE"
, chosen by the [_::2]
list-slicing trick.
CJam, 57 bytes
l_,,_f{f-W$'@fm.*:+}0#:I){ISIW$=S++t}" DOES NOT BALANCE"?
This can still be golfed a bit.
Try it online here