Решение на Разлика в сумите от Адриана Стефанова

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

Към профила на Адриана Стефанова

Резултати

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

Код

package main
import (
"fmt"
"strings"
)
func ExtractColumn(logContents string, column uint8) string {
lines := getLines(logContents)
parts := getParts(lines)
return getColumn(column, parts)
}
func getLines(logContents string) []string {
lines := strings.Split(logContents, "\n")
for i, element := range lines {
if element == "" {
lines = append(lines[:i], lines[i+1:]...)
}
}
return lines
}
func getParts(lines []string) [][]string {
parts := make([][]string, len(lines))
for j, line := range lines {
parts[j] = strings.SplitN(line, " ", 4)
}
return parts
}
func getColumn(column uint8, parts [][]string) string {
var result string
if column == 0 {
for _, part := range parts {
result = fmt.Sprintf("%s%s %s\n", result, part[0], part[1])
}
} else if column == 1 {
for _, part := range parts {
result = fmt.Sprintf("%s%s\n", result, part[2])
}
} else if column == 2 {
for _, part := range parts {
result = fmt.Sprintf("%s%s\n", result, part[3])
}
} else {
result = "There is no such column!"
}
return result
}

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

PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
--- FAIL: TestWithOnlyOneNewLine (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: slice bounds out of range
	solution_test.go:253: There was a panic while testing: runtime error: slice bounds out of range
	solution_test.go:253: There was a panic while testing: runtime error: slice bounds out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s
PASS
ok  	_/tmp/d20151103-24541-hh1hi3	0.003s

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

Адриана обнови решението на 02.11.2015 17:35 (преди над 2 години)

+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func ExtractColumn(logContents string, column uint8) string {
+ lines := getLines(logContents)
+ parts := getParts(lines)
+
+ return getColumn(column, parts)
+}
+
+func getLines(logContents string) []string {
+ lines := strings.Split(logContents, "\n")
+
+ for i, element := range lines {
+ if element == "" {
+ lines = append(lines[:i], lines[i+1:]...)
+ }
+ }
+ return lines
+}
+
+func getParts(lines []string) [][]string {
+ parts := make([][]string, len(lines))
+ for j, line := range lines {
+ parts[j] = strings.SplitN(line, " ", 4)
+ }
+ return parts
+}
+
+func getColumn(column uint8, parts [][]string) string {
+ var result string
+
+ if column == 0 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s %s\n", result, part[0], part[1])
+ }
+ } else if column == 1 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s\n", result, part[2])
+ }
+ } else if column == 2 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s\n", result, part[3])
+ }
+ } else {
+ result = "There is no such column!"
+ }
+ return result
+}