This challenge was posted on the DailyProgrammer subreddit, and I figured it would be a great candidate for a code golf challenge. Determining if a letter balances is based on its distance from the point of balance, and the letter's value. The value of a letter can be determined by either taking its one-indexed position in the alphabet, or by subtracting 64 from its ASCII value. Furthermore, the value of a letter is multiplied by its distance from the balance point. Let's take a look at an example, STEAD
:
STEAD -> 19, 20, 5, 1, 4 ASCII values
This balances at T, and I'll show you why!
S T EAD -> 1*19 = 1*5 + 2*1 + 3*4
Each set of letters on either side sums to the same value, so
T is the anchor.
However, it should be noted that not all words balance. For example, the word WRONG
does not balance in any configuration. Also, words must balance on a letter, not between two letters. For example, SAAS
would balance if there was a letter in the middle of the two A
s, but since there is none it does not balance.
The Task
You should create a program or function that takes in an uppercase word as input or function arguments, and then produces one of two outputs:
If the word balances, then the word should be printed with the left side, a space, the anchor letter, another space, and the right side.
function (STEAD) -> S T EAD
If the word does not balance, you should print out the word, followed by
DOES NOT BALANCE
function (WRONG) -> WRONG DOES NOT BALANCE
You may assume that all input will be uppercase and there will only be alpha characters.
Example I/O
function (CONSUBSTANTIATION) -> CONSUBST A NTIATION
function (WRONGHEADED) -> WRO N GHEADED
function (UNINTELLIGIBILITY) -> UNINTELL I GIBILITY
function (SUPERGLUE) -> SUPERGLUE DOES NOT BALANCE
This is code-golf, so the shortest answer in bytes wins.
function (A)
->A
instead of -> ` A `? \$\endgroup\$BALANCE DOES NOT BALANCE
\$\endgroup\$