/
7-06.go
58 lines (51 loc) · 1.1 KB
/
7-06.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* DAILY PROGRAMMER - 7/06/2015
* Balancing Words
* Author: Muhan Guclu
* Description: https://www.reddit.com/r/dailyprogrammer/comments/3c9a9h/20150706_challenge_222_easy_balancing_words/
*/
package main
import (
"fmt"
"strconv"
)
func getAlphaPos(l rune) int {
return int(l) - 64
}
func sumSides(a string, b string) (int, int) {
sumA, sumB := 0, 0
for idxA, charA := range a {
sumA += getAlphaPos(charA) * (len(a) - idxA)
}
for idxB, charB := range b {
sumB += getAlphaPos(charB) * (idxB + 1)
}
return sumA, sumB
}
func getCenter(w string) string {
result := ""
for idx, letter := range w {
sideA, sideB := w[:idx], w[(idx+1):]
sumA, sumB := sumSides(sideA, sideB)
if sumA-sumB == 0 {
result = fmt.Sprint(sideA, " ", strconv.QuoteRuneToASCII(letter), " ", sideB, " - ", sumA)
break
}
}
if result == "" {
result = fmt.Sprint(w, " DOES NOT BALANCE")
}
return result
}
func main() {
challengeInput := []string{
"STEAD",
"CONSUBSTANTIATION",
"WRONGHEADED",
"UNINTELLIGIBILITY",
"SUPERGLUE",
}
for _, input := range challengeInput {
fmt.Println(getCenter(input))
}
}