Решение на Разлика в сумите от Йончо Йончев

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

Към профила на Йончо Йончев

Резултати

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

Код

package main
import (
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
var buffer bytes.Buffer
logContents += "\n"
content := strings.Split(logContents, "\n")
for i := 0; i < len(content)-1; i++ {
if len(content[i]) > 0 {
columns := strings.Split(content[i], " ")
switch column {
case 0:
buffer.WriteString(columns[0])
buffer.WriteString(" ")
buffer.WriteString(columns[1])
case 1:
buffer.WriteString(columns[2])
case 2:
buffer.WriteString(strings.SplitN(content[i], " ", 4)[3])
}
buffer.WriteString("\n")
}
}
return buffer.String()
}

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

PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s
PASS
ok  	_/tmp/d20151103-24541-2ks48i	0.003s

История (3 версии и 4 коментара)

Йончо обнови решението на 21.10.2015 15:25 (преди над 2 години)

+package main
+
+import (
+ "bytes"
+ "strings"
+)
+
+func ExtractColumn(logContents string, column uint8) string {
+
+ var buffer bytes.Buffer
+ content := strings.Split(logContents, "\n")
+ for i := 0; i < len(content)-1; i++ {
+ columns := strings.Split(content[i], " ")
+ if column == 0 {
+ buffer.WriteString(columns[0])
+ buffer.WriteString(" ")
+ buffer.WriteString(columns[1])
+ buffer.WriteString(" ")
+ }
+ if column == 1 {
+ buffer.WriteString(columns[2])
+ buffer.WriteString(" ")
+ }
+ if column == 2 {
+ for j := 3; j < len(columns); j++ {
+ buffer.WriteString(columns[j])
+ buffer.WriteString(" ")
+ }
+ }
+
+ buffer.WriteString("\n")
+ }
+ return buffer.String()
+}

Йончо обнови решението на 22.10.2015 15:00 (преди над 2 години)

package main
import (
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
var buffer bytes.Buffer
content := strings.Split(logContents, "\n")
for i := 0; i < len(content)-1; i++ {
columns := strings.Split(content[i], " ")
- if column == 0 {
+ switch column {
+ case 0:
buffer.WriteString(columns[0])
buffer.WriteString(" ")
buffer.WriteString(columns[1])
- buffer.WriteString(" ")
- }
- if column == 1 {
+ case 1:
buffer.WriteString(columns[2])
- buffer.WriteString(" ")
+ case 2:
+ buffer.WriteString(strings.SplitN(content[i], " ", 4)[3])
}
- if column == 2 {
- for j := 3; j < len(columns); j++ {
- buffer.WriteString(columns[j])
- buffer.WriteString(" ")
- }
- }
-
buffer.WriteString("\n")
}
return buffer.String()
}

Йончо обнови решението на 03.11.2015 11:35 (преди над 2 години)

package main
import (
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
var buffer bytes.Buffer
+ logContents += "\n"
content := strings.Split(logContents, "\n")
for i := 0; i < len(content)-1; i++ {
- columns := strings.Split(content[i], " ")
- switch column {
- case 0:
- buffer.WriteString(columns[0])
- buffer.WriteString(" ")
- buffer.WriteString(columns[1])
- case 1:
- buffer.WriteString(columns[2])
- case 2:
- buffer.WriteString(strings.SplitN(content[i], " ", 4)[3])
+ if len(content[i]) > 0 {
+ columns := strings.Split(content[i], " ")
+ switch column {
+ case 0:
+ buffer.WriteString(columns[0])
+ buffer.WriteString(" ")
+ buffer.WriteString(columns[1])
+ case 1:
+ buffer.WriteString(columns[2])
+ case 2:
+ buffer.WriteString(strings.SplitN(content[i], " ", 4)[3])
+ }
+ buffer.WriteString("\n")
}
- buffer.WriteString("\n")
}
return buffer.String()
}