Решение на Разлика в сумите от Станислав Гатев

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

Към профила на Станислав Гатев

Резултати

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

Код

package main
import (
"bytes"
"regexp"
)
const (
logDatePattern = `(\d+\-\d+\-\d+\s\d+:\d+:\d+)`
logIpPattern = `(\d+\.\d+\.\d+\.\d+)`
logMessagePattern = `(.*)`
logLinePattern = logDatePattern + `\s` + logIpPattern + `\s` + logMessagePattern
newLineCharacter = "\n"
)
var logLineRegExp = regexp.MustCompile(logLinePattern)
// Extracts column data from log string.
func ExtractColumn(logContents string, column uint8) string {
matchedLines := logLineRegExp.FindAllStringSubmatch(logContents, -1)
// Check if log is empty.
if matchedLines == nil {
return ""
}
// Aggregate the columns' data in a single buffer.
var buffer bytes.Buffer
for _, lineGroups := range matchedLines {
// 0 is the index of the whole match so column+1 is the index of the specific zero-indexed group.
buffer.WriteString(lineGroups[column+1])
buffer.WriteString(newLineCharacter)
}
return buffer.String()
}

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

PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.003s
PASS
ok  	_/tmp/d20151103-24541-y50my	0.004s

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

Станислав обнови решението на 21.10.2015 10:59 (преди над 2 години)

+package main
+
+import (
+ "bytes"
+ "regexp"
+)
+
+const (
+ logDatePattern = `(\d+\-\d+\-\d+\s\d+:\d+:\d+)`
+ logIpPattern = `(\d+\.\d+\.\d+\.\d+)`
+ logMessagePattern = `(.*)`
+ logLinePattern = logDatePattern + `\s` + logIpPattern + `\s` + logMessagePattern
+)
+
+var logLineRegExp = regexp.MustCompile(logLinePattern)
+
+// Extracts column data from log string.
+func ExtractColumn(logContents string, column uint8) string {
+ matchedLines := logLineRegExp.FindAllStringSubmatch(logContents, -1)
+
+ // Check if log is empty.
+ if matchedLines == nil {
+ return ""
+ }
+
+ // Aggregate the columns' data in a single buffer.
+ var buffer bytes.Buffer
+ for _, lineGroups := range matchedLines {
+ // 0 is the index of the whole match so column+1 is the index of the specific zero-indexed group.
+ buffer.WriteString(lineGroups[column+1] + "\n")
+ }
+
+ return buffer.String()
+}

Много добро решение, дълго време се чудех дали мога да добавя нещо.

Чак сега открих че принципно може да не ползваш bytes.Buffer и да правиш даже по-малко количество междинни стрингове със същото количество редове.

Станислав обнови решението на 26.10.2015 11:49 (преди над 2 години)

package main
import (
"bytes"
"regexp"
)
const (
logDatePattern = `(\d+\-\d+\-\d+\s\d+:\d+:\d+)`
logIpPattern = `(\d+\.\d+\.\d+\.\d+)`
logMessagePattern = `(.*)`
logLinePattern = logDatePattern + `\s` + logIpPattern + `\s` + logMessagePattern
+ newLineCharacter = "\n"
)
var logLineRegExp = regexp.MustCompile(logLinePattern)
// Extracts column data from log string.
func ExtractColumn(logContents string, column uint8) string {
matchedLines := logLineRegExp.FindAllStringSubmatch(logContents, -1)
// Check if log is empty.
if matchedLines == nil {
return ""
}
// Aggregate the columns' data in a single buffer.
var buffer bytes.Buffer
for _, lineGroups := range matchedLines {
// 0 is the index of the whole match so column+1 is the index of the specific zero-indexed group.
- buffer.WriteString(lineGroups[column+1] + "\n")
+ buffer.WriteString(lineGroups[column+1])
+ buffer.WriteString(newLineCharacter)
}
return buffer.String()
}