Решение на Разлика в сумите от Добромир Иванов

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

Към профила на Добромир Иванов

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 1 неуспешни тест(а)

Код

package main
import (
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
lines := strings.Split(strings.TrimSpace(logContents), "\n")
var buffer bytes.Buffer
for _, line := range lines {
if line == "" {
continue
}
columns := strings.Split(line, " ")
var columnContents string
switch column {
case 0:
columnContents = strings.Join(columns[column:column+2], " ")
case 1:
columnContents = columns[column+1]
case 2:
columnContents = strings.Join(columns[column+1:], " ")
}
buffer.WriteString(columnContents + "\n")
}
return buffer.String()
}

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

PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.004s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.004s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
--- FAIL: TestSpacesAtTheStartOrEndOfALine (0.00s)
	solution_test.go:260: Expected
		---
		As far as we can tell this is a DNS
		Yet another DNS, how quaint!
		There is definitely some trend here
		 Some spaces at the beginning of this line
		Nothing particularly interesting happened 
		
		---
		but found
		---
		As far as we can tell this is a DNS
		Yet another DNS, how quaint!
		There is definitely some trend here
		 Some spaces at the beginning of this line
		Nothing particularly interesting happened
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.005s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s
PASS
ok  	_/tmp/d20151103-24541-yuxxc1	0.003s

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

Добромир обнови решението на 24.10.2015 01:16 (преди над 2 години)

+package main
+
+import "strings"
+import "bytes"
+import "regexp"
+
+func ExtractColumn(logContents string, column uint8) string {
+ var columnFormats = [...]string{"\\d{2}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", "([A-Za-z\\s]+)\\z"}
+ lines := strings.Split(logContents, "\n")
+ var buffer bytes.Buffer
+
+ for _, line := range lines[0 : len(lines)-1] {
+ re := regexp.MustCompile(columnFormats[column])
+ match := strings.TrimSpace(re.FindString(line))
+ buffer.WriteString(match + "\n")
+ }
+
+ return buffer.String()
+}

Интересно решение, но ето няколко забележки:

  1. не сме ви го преподавали(ще е в следващата лекция) но при синтаксиса lines[a:b] елемента с индекс b не се включва. В твоя случай няма нужда да reslice-ваш изобщо тоест може да е само lines.
  2. нищо не пречи третата "колона" да има същата/подобна стойност като на първите две.

Препоръчвам ти да си пуснеш тестовете на Таня

Добромир обнови решението на 31.10.2015 14:13 (преди над 2 години)

package main
-import "strings"
-import "bytes"
-import "regexp"
+import (
+ "bytes"
+ "strings"
+)
func ExtractColumn(logContents string, column uint8) string {
- var columnFormats = [...]string{"\\d{2}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", "([A-Za-z\\s]+)\\z"}
- lines := strings.Split(logContents, "\n")
+ lines := strings.Split(strings.TrimSpace(logContents), "\n")
var buffer bytes.Buffer
+ for _, line := range lines {
+ if line == "" {
+ continue
+ }
+ columns := strings.Split(line, " ")
+ var columnContents string
- for _, line := range lines[0 : len(lines)-1] {
- re := regexp.MustCompile(columnFormats[column])
- match := strings.TrimSpace(re.FindString(line))
- buffer.WriteString(match + "\n")
+ switch column {
+ case 0:
+ columnContents = strings.Join(columns[column:column+2], " ")
+ case 1:
+ columnContents = columns[column+1]
+ case 2:
+ columnContents = strings.Join(columns[column+1:], " ")
+ }
+
+ buffer.WriteString(columnContents + "\n")
}
return buffer.String()
-}
+}