Решение на Разлика в сумите от Анонимен Потребител 1

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

Към профила на Анонимен Потребител 1

Резултати

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

Код

package main
import (
"regexp"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
lines := strings.Split(logContents, "\n")
numLines := len(lines)
var result []string
for i := 0; i < numLines; i++ {
result = append(result, ProcessLine(lines[i], column))
}
return strings.Join(result, "")
}
func ProcessLine(line string, column uint8) string {
re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) (.*)`)
if !re.MatchString(line) {
return ""
}
return re.FindStringSubmatch(line)[column+1] + "\n"
}

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

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

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

Антоан обнови решението на 23.10.2015 00:50 (преди над 2 години)

+package main
+
+import "strings"
+
+func ExtractColumn(logContents string, column uint8) string {
+ lines := strings.Split(logContents, "\n")
+ numLines := len(lines)
+ var result []string
+
+ for i := 0; i < numLines; i++ {
+ result = append(result, ProcessLine(lines[i], column))
+ }
+
+ return strings.Join(result, "")
+}
+
+func ProcessLine(line string, column uint8) string {
+ separated := strings.SplitN(line, " ", 4)
+
+ if len(separated) == 1 {
+ return ""
+ }
+
+ result := []string{strings.Join(separated[:2], " ")}
+ result = append(result, separated[2:]...)
+ return result[column] + "\n"
+}

Антоан обнови решението на 25.10.2015 02:50 (преди над 2 години)

package main
-import "strings"
+import (
+ "regexp"
+ "strings"
+)
func ExtractColumn(logContents string, column uint8) string {
lines := strings.Split(logContents, "\n")
numLines := len(lines)
var result []string
for i := 0; i < numLines; i++ {
result = append(result, ProcessLine(lines[i], column))
}
return strings.Join(result, "")
}
func ProcessLine(line string, column uint8) string {
- separated := strings.SplitN(line, " ", 4)
+ re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) (.*)`)
- if len(separated) == 1 {
+ if !re.MatchString(line) {
return ""
}
- result := []string{strings.Join(separated[:2], " ")}
- result = append(result, separated[2:]...)
- return result[column] + "\n"
+ return re.FindStringSubmatch(line)[column+1] + "\n"
}