Решение на Разлика в сумите от Таня Христова

Обратно към всички решения

Към профила на Таня Христова

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
import (
"bytes"
"strings"
"unicode/utf8"
)
//Ugly solution. Will rewrite.
const COLUMN_COUNT int = 4
func ExtractColumn(logContents string, column uint8) string {
rows := strings.Split(logContents, "\n")
var segmentBuffer bytes.Buffer
for _, row := range rows {
segmentBuffer.WriteString(getWantedRowSegment(row, column))
}
return segmentBuffer.String()
}
func getWantedRowSegment(row string, column uint8) string {
rowSegments := strings.SplitN(row, " ", COLUMN_COUNT)
if len(rowSegments) < COLUMN_COUNT {
return ""
}
rowSegment := getSegment(rowSegments, column)
if utf8.RuneCountInString(rowSegment) > 0 {
rowSegment += "\n"
}
return rowSegment
}
func getSegment(segs []string, column uint8) string {
switch column {
case 0:
return segs[0] + " " + segs[1]
default:
return segs[column+1]
}
}

Лог от изпълнението

PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.004s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.028s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.008s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.015s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.004s
PASS
ok  	_/tmp/d20151103-24541-1xoiv6j	0.003s

История (1 версия и 0 коментара)

Таня обнови решението на 23.10.2015 08:33 (преди над 2 години)

+package main
+
+import (
+ "bytes"
+ "strings"
+ "unicode/utf8"
+)
+
+//Ugly solution. Will rewrite.
+
+const COLUMN_COUNT int = 4
+
+func ExtractColumn(logContents string, column uint8) string {
+ rows := strings.Split(logContents, "\n")
+ var segmentBuffer bytes.Buffer
+ for _, row := range rows {
+ segmentBuffer.WriteString(getWantedRowSegment(row, column))
+ }
+ return segmentBuffer.String()
+}
+
+func getWantedRowSegment(row string, column uint8) string {
+ rowSegments := strings.SplitN(row, " ", COLUMN_COUNT)
+ if len(rowSegments) < COLUMN_COUNT {
+ return ""
+ }
+ rowSegment := getSegment(rowSegments, column)
+ if utf8.RuneCountInString(rowSegment) > 0 {
+ rowSegment += "\n"
+ }
+ return rowSegment
+}
+
+func getSegment(segs []string, column uint8) string {
+ switch column {
+ case 0:
+ return segs[0] + " " + segs[1]
+ default:
+ return segs[column+1]
+ }
+}