33
1
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.
Can we omit the spaces in the output of single letter words, e.g.
function (A)
->A
instead of ->A
? – nimi – 2015-07-07T21:03:11.2231@nimi Yes, you may omit spaces. – Kade – 2015-07-07T21:18:53.937
Should single character input be considered balanced at all? – some user – 2015-07-07T21:38:00.913
1@someuser Yes, because the "weight" on either side is 0. – Kade – 2015-07-07T21:53:55.180
Is trailing whitespace allowed? – Dennis – 2015-07-08T03:51:39.563
@Dennis A trailing newline is allowed. – Kade – 2015-07-08T11:48:39.690
14
BALANCE DOES NOT BALANCE
– Optimizer – 2015-07-08T14:29:32.650