go - Can anybody solve this programming challenge? -
can solve next question using programming language go?
james got hold of love letter friend harry has written girlfriend. beingness prankster james is, decides meddle it. changes words in letter palindromes.
while modifying letters of word, follows 2 rules:
he reduces value of letter, e.g. changes 'd' 'c', not alter 'c' 'd'.
if has repeatedly cut down value of letter, can until letter becomes 'a'. 1 time letter has been changed 'a', can no longer changed.
each reduction in value of letter counted single operation. find minimum number of operations carries out convert given string palindrome.
input formatthe first line contains integer t, i.e., number of test cases. next t lines contain string each.
output formata single line containing number of minimum operations corresponding each test case.
constraints1 ≤ t ≤ 10 1 ≤ length of string ≤ 104 characters lower cased.
sample input#00 3 abc abcba abcd
sample output#00 2 0 4
explanationfor first test case, ab*c* -> ab*b* -> ab*a*. sec test case, abcba palindromic string. 3rd test case, abc*d* -> abc*c* -> abc*b* -> abc*a* = ab*c*a -> ab*b*a.
the puzzle taken hackerrank.com golang newbie , couldn't solve puzzle using language.
i don't see particularly special solution in go programming language. example,
// love-letter mystery // https://www.hackerrank.com/challenges/the-love-letter-mystery bundle main import ( "bufio" "fmt" "io" "os" "strconv" ) // palindrome returns minimum number of operations carried out // convert word palindrome. // word set of lower-case ascii letters. func palindrome(word string) (ops int) { i, j := 0, len(word)-1; < j; i, j = i+1, j-1 { n := int(word[i]) - int(word[j]) if n < 0 { n = -n } ops += n } homecoming ops } func main() { scanner := bufio.newscanner(os.stdin) if scanner.scan() { t, err := strconv.atoi(scanner.text()) if err != nil { fmt.fprintln(os.stderr, "reading input:", err) t = 0 } ; t > 0 && scanner.scan(); t-- { fmt.println(palindrome(scanner.text())) } if t > 0 { fmt.fprintln(os.stderr, "reading input:", io.errunexpectedeof) } } if err := scanner.err(); err != nil { fmt.fprintln(os.stderr, "reading input:", err) } } input:
3 abc abcba abcd output:
2 0 4 input:
3 cba cbabc dcba output:
2 0 4 go
No comments:
Post a Comment