Решение на Разлика в сумите от Михаил Здравков

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

Към профила на Михаил Здравков

Резултати

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

Код

package main
import "strings"
func ExtractColumn(logContents string, column uint8) string {
rows := strings.SplitAfter(logContents, "\n")
result := ""
for _, row := range rows {
columns := strings.SplitN(row, " ", 4)
if len(columns) < 4 {
continue
}
var toAppend string
switch column {
case 0:
toAppend = strings.Join(columns[:2], " ") + "\n"
case 1:
toAppend = columns[2] + "\n"
case 2:
toAppend = columns[3]
}
result = strings.Join([]string{result, toAppend}, "")
}
if len(result) > 0 && result[len(result)-1] != '\n' {
result = strings.Join([]string{result, "\n"}, "")
}
return result
}

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

PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.004s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.004s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s
PASS
ok  	_/tmp/d20151103-24541-1b6pnhd	0.003s

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

Михаил обнови решението на 31.10.2015 11:53 (преди над 2 години)

+package main
+
+import "strings"
+
+func ExtractColumn(logContents string, column uint8) string {
+ rows := strings.SplitAfter(logContents, "\n")
+ result := ""
+ for _, row := range rows {
+ columns := strings.SplitN(row, " ", 4)
+ if len(columns) < 4 {
+ continue
+ }
+
+ var toAppend string
+ switch column {
+ case 0:
+ toAppend = strings.Join(columns[:2], " ") + "\n"
+ case 1:
+ toAppend = columns[2] + "\n"
+ case 2:
+ toAppend = columns[3]
+ }
+ result = strings.Join([]string{result, toAppend}, "")
+ }
+
+ if len(result) > 0 && result[len(result)-1] != '\n' {
+ result = strings.Join([]string{result, "\n"}, "")
+ }
+ return result
+}