Решение на Разлика в сумите от Ралица Великова

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

Към профила на Ралица Великова

Резултати

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

Код

package main
import (
"bufio"
"bytes"
"strings"
)
func extractDate(logLine string) string {
var (
date bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 3)
)
date.WriteString(linePieces[0])
date.WriteString(" ")
date.WriteString(linePieces[1])
return date.String()
}
func extractIp(logLine string) string {
var (
ipBuffer bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 4)
)
ipBuffer.WriteString(linePieces[2])
return ipBuffer.String()
}
func extractMessage(logLine string) string {
var (
messageBuffer bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 4)
)
messageBuffer.WriteString(linePieces[3])
return messageBuffer.String()
}
func ExtractColumn(logContents string, column uint8) string {
var (
reader = strings.NewReader(logContents)
scanner = bufio.NewScanner(reader)
resultBuffer bytes.Buffer
)
for scanner.Scan() {
var line = scanner.Text()
if strings.Trim(line, " \n\t\v") == "" {
continue
}
if column == 0 {
resultBuffer.WriteString(extractDate(line))
} else if column == 1 {
resultBuffer.WriteString(extractIp(line))
} else if column == 2 {
resultBuffer.WriteString(extractMessage(line))
}
resultBuffer.WriteString("\n")
}
return resultBuffer.String()
}

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

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

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

Ралица обнови решението на 25.10.2015 17:22 (преди над 2 години)

+package main
+
+import (
+ "bytes"
+ "strings"
+)
+
+func ExtractColumn(logContents string, column uint8) string {
+ var lines = strings.Split(logContents, "\n")
+
+ if len(lines) == 0 {
+ return ""
+ }
+
+ var resultBuffer bytes.Buffer
+
+ for lineCount := 0; lineCount < len(lines); lineCount++ {
+ var line = lines[lineCount]
+ line = strings.Trim(line, " \n")
+
+ if line != "" {
+ var pieces []string = strings.SplitN(line, " ", 4)
+
+ switch {
+ case pieces != nil && column == 0:
+ resultBuffer.WriteString(pieces[0])
+ resultBuffer.WriteString(" ")
+ resultBuffer.WriteString(pieces[1])
+ case pieces != nil && column == 1:
+ resultBuffer.WriteString(pieces[2])
+ case pieces != nil && column == 2:
+ resultBuffer.WriteString(pieces[3])
+ }
+
+ resultBuffer.WriteString("\n")
+ }
+ }
+
+ return resultBuffer.String()
+}

Ралица обнови решението на 26.10.2015 00:53 (преди над 2 години)

package main
import (
+ "bufio"
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
- var lines = strings.Split(logContents, "\n")
+ var reader = strings.NewReader(logContents)
+ var scanner = bufio.NewScanner(reader)
- if len(lines) == 0 {
- return ""
- }
-
var resultBuffer bytes.Buffer
- for lineCount := 0; lineCount < len(lines); lineCount++ {
- var line = lines[lineCount]
- line = strings.Trim(line, " \n")
+ for scanner.Scan() {
+ var line = scanner.Text()
- if line != "" {
- var pieces []string = strings.SplitN(line, " ", 4)
+ if strings.Trim(line, " \n\t\v") != "" {
+ if column == 0 {
+ resultBuffer.WriteString(line[:19])
+ } else {
+ var pieces []string = strings.SplitN(line, " ", 4)
- switch {
- case pieces != nil && column == 0:
- resultBuffer.WriteString(pieces[0])
- resultBuffer.WriteString(" ")
- resultBuffer.WriteString(pieces[1])
- case pieces != nil && column == 1:
- resultBuffer.WriteString(pieces[2])
- case pieces != nil && column == 2:
- resultBuffer.WriteString(pieces[3])
+ if pieces != nil && column == 1 {
+ resultBuffer.WriteString(pieces[2])
+ }
+ if pieces != nil && column == 2 {
+ resultBuffer.WriteString(pieces[3])
+ }
}
resultBuffer.WriteString("\n")
}
}
return resultBuffer.String()
}

Ралица обнови решението на 26.10.2015 01:36 (преди над 2 години)

package main
import (
"bufio"
"bytes"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
var reader = strings.NewReader(logContents)
var scanner = bufio.NewScanner(reader)
var resultBuffer bytes.Buffer
for scanner.Scan() {
var line = scanner.Text()
if strings.Trim(line, " \n\t\v") != "" {
if column == 0 {
resultBuffer.WriteString(line[:19])
} else {
var pieces []string = strings.SplitN(line, " ", 4)
- if pieces != nil && column == 1 {
+ if column == 1 {
resultBuffer.WriteString(pieces[2])
- }
- if pieces != nil && column == 2 {
+ } else if column == 2 {
resultBuffer.WriteString(pieces[3])
}
}
resultBuffer.WriteString("\n")
}
}
return resultBuffer.String()
}

радвам се че някой е открил scanner и го използва, но няколко стилистични забележки:

  1. ако имаш няколко последователни var | const е прието да направиш блок от тях
  2. в go а и в други езици е прието първо да се разглеждат "изключителните" ситуации и после да се разглеждат очакваните такива
  3. 19 ми е някакво магическо число
  4. може да разделиш логиката в няколко функции за да е по ясно какво се случва.

Ралица обнови решението на 26.10.2015 23:06 (преди над 2 години)

package main
import (
"bufio"
"bytes"
"strings"
)
-func ExtractColumn(logContents string, column uint8) string {
- var reader = strings.NewReader(logContents)
- var scanner = bufio.NewScanner(reader)
+func extractDate(logLine string) string {
+ var (
+ date bytes.Buffer
+ linePieces []string = strings.SplitN(logLine, " ", 3)
+ )
- var resultBuffer bytes.Buffer
+ date.WriteString(linePieces[0])
+ date.WriteString(" ")
+ date.WriteString(linePieces[1])
+ return date.String()
+}
+
+func extractIp(logLine string) string {
+ var (
+ ipBuffer bytes.Buffer
+ linePieces []string = strings.SplitN(logLine, " ", 4)
+ )
+
+ ipBuffer.WriteString(linePieces[2])
+
+ return ipBuffer.String()
+}
+
+func extractMessage(logLine string) string {
+ var (
+ messageBuffer bytes.Buffer
+ linePieces []string = strings.SplitN(logLine, " ", 4)
+ )
+
+ messageBuffer.WriteString(linePieces[3])
+
+ return messageBuffer.String()
+}
+
+func ExtractColumn(logContents string, column uint8) string {
+ var (
+ reader = strings.NewReader(logContents)
+ scanner = bufio.NewScanner(reader)
+ resultBuffer bytes.Buffer
+ )
+
for scanner.Scan() {
var line = scanner.Text()
- if strings.Trim(line, " \n\t\v") != "" {
+ if strings.Trim(line, " \n\t\v") == "" {
+ continue
+ } else {
if column == 0 {
- resultBuffer.WriteString(line[:19])
- } else {
- var pieces []string = strings.SplitN(line, " ", 4)
-
- if column == 1 {
- resultBuffer.WriteString(pieces[2])
- } else if column == 2 {
- resultBuffer.WriteString(pieces[3])
- }
+ resultBuffer.WriteString(extractDate(line))
+ } else if column == 1 {
+ resultBuffer.WriteString(extractIp(line))
+ } else if column == 2 {
+ resultBuffer.WriteString(extractMessage(line))
}
resultBuffer.WriteString("\n")
}
}
return resultBuffer.String()
}

Ралица обнови решението на 26.10.2015 23:11 (преди над 2 години)

package main
import (
"bufio"
"bytes"
"strings"
)
func extractDate(logLine string) string {
var (
date bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 3)
)
date.WriteString(linePieces[0])
date.WriteString(" ")
date.WriteString(linePieces[1])
return date.String()
}
func extractIp(logLine string) string {
var (
ipBuffer bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 4)
)
ipBuffer.WriteString(linePieces[2])
return ipBuffer.String()
}
func extractMessage(logLine string) string {
var (
messageBuffer bytes.Buffer
linePieces []string = strings.SplitN(logLine, " ", 4)
)
messageBuffer.WriteString(linePieces[3])
return messageBuffer.String()
}
func ExtractColumn(logContents string, column uint8) string {
var (
reader = strings.NewReader(logContents)
scanner = bufio.NewScanner(reader)
resultBuffer bytes.Buffer
)
for scanner.Scan() {
var line = scanner.Text()
if strings.Trim(line, " \n\t\v") == "" {
continue
- } else {
- if column == 0 {
- resultBuffer.WriteString(extractDate(line))
- } else if column == 1 {
- resultBuffer.WriteString(extractIp(line))
- } else if column == 2 {
- resultBuffer.WriteString(extractMessage(line))
- }
+ }
- resultBuffer.WriteString("\n")
+ if column == 0 {
+ resultBuffer.WriteString(extractDate(line))
+ } else if column == 1 {
+ resultBuffer.WriteString(extractIp(line))
+ } else if column == 2 {
+ resultBuffer.WriteString(extractMessage(line))
}
+
+ resultBuffer.WriteString("\n")
}
return resultBuffer.String()
}