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

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

Към профила на Андон Мицов

Резултати

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

Код

package main
import "strings"
func ExtractColumn(logContents string, column uint8) (col string) {
var logLines []string = strings.Split(logContents, "\n")
if column > 2 {
col = ""
return
}
for _, value := range logLines {
var lineWords []string = strings.Split(value, " ")
switch column {
case 0:
col += lineWords[0] + " " + lineWords[1] + "\n"
case 1:
col += lineWords[2] + "\n"
case 2:
{
var columnWord string
for i := 3; i < len(lineWords); i++ {
columnWord += lineWords[i] + " "
}
col += columnWord + "\n"
}
}
}
return
}

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

--- FAIL: TestWithTheExampleTest (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestDifferentColumns (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	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
		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 
		Nothing particularly interesting happened 
		
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestWithEmptyLog (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:260: Expected
		---
		
		---
		but found
		---
		
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestWithOneLiner (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	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-vigmy5	0.003s
--- FAIL: TestNoNewLineAtEndOfInput (0.00s)
	solution_test.go:260: Expected
		---
		As far as we can tell this is a DNS
		There is definitely some trend here
		
		---
		but found
		---
		As far as we can tell this is a DNS 
		There is definitely some trend here 
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestIPOrDateAtTheEndOfALine (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:260: Expected
		---
		2015-08-23 12:37:03
		8.8.4.4
		
		---
		but found
		---
		2015-08-23 12:37:03 
		8.8.4.4 
		
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestWithOnlyOneNewLine (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
	solution_test.go:260: Expected
		---
		
		---
		but found
		---
		
		
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestExtractingIPs (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestExtractingTimes (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestExtractingTexts (0.00s)
	solution_test.go:260: Expected
		---
		As far as we can tell this is a DNS
		Yet another DNS(8.8.4.4), how quaint!
		There is definitely some trend here
		
		---
		but found
		---
		As far as we can tell this is a DNS 
		Yet another DNS(8.8.4.4), how quaint! 
		There is definitely some trend here 
		
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.006s
PASS
ok  	_/tmp/d20151103-24541-vigmy5	0.003s
PASS
ok  	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestWithSpaces (0.00s)
	solution_test.go:260: Expected
		---
		spaces   tabs		end
		
		---
		but found
		---
		spaces   tabs		end 
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.003s
--- FAIL: TestMoreLinesThanExample (0.00s)
	solution_test.go:253: There was a panic while testing: runtime error: index out of range
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-vigmy5	0.004s

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

Андон обнови решението на 27.10.2015 09:55 (преди над 2 години)

+package LogParser
+
+import "strings"
+
+func ExtractColumn(logContents string, column uint8) (col string) {
+ var logLines []string = strings.Split(logContents, "\n")
+ if column > 2 {
+ col = "Error"
+ return
+ }
+
+ for _, value := range logLines {
+ var lineWords []string = strings.Split(value, " ")
+
+ switch column {
+ case 0:
+ col += lineWords[0] + " " + lineWords[1] + "\n"
+
+ case 1:
+ col += lineWords[2] + "\n"
+
+ case 2:
+ {
+ var columnWord string
+ for i := 3; i < len(lineWords); i++ {
+ columnWord += lineWords[i] + " "
+ }
+
+ col += columnWord + "\n"
+ }
+ }
+
+ }
+
+ return
+}

Андон, решението ти изглежда добре. Но има едно нещо, което не ми харесва. Връщането на "Error" при грешен вход на фунцията не е добра идея. Тази стойност може да бъде интерпретирана като нормален резултат от функцията.

Следното нещо е съвсем валиден лог, който може да бъде даден на функцията ти:

2015-10-27 15:09:02 9.8.7.6 Error

По - генерално, проблема е за това как трябва да се сигнализира за грешка, когато имаш точно определен тип за връщане. За това има решение в езика и това е още една върната стойност. Начина, по който сме ви задали условието, обаче, не го позволява. Ще имаме специална лекция, посветена на точно това и ще ви покажем по - подробно точно такива ситуации. Като preview:

func ExtractColumn(logContents string, column uint8) (string, error)

А за сега бих ти препоръчал да премахнеш този ред и да оставиш резултата да бъде празен низ. Също така, във форума ви обещахме, че няма да даваме грешни входни данни.

Андон обнови решението на 27.10.2015 16:07 (преди над 2 години)

package LogParser
import "strings"
func ExtractColumn(logContents string, column uint8) (col string) {
var logLines []string = strings.Split(logContents, "\n")
if column > 2 {
- col = "Error"
+ col = ""
return
}
for _, value := range logLines {
var lineWords []string = strings.Split(value, " ")
switch column {
case 0:
col += lineWords[0] + " " + lineWords[1] + "\n"
case 1:
col += lineWords[2] + "\n"
case 2:
{
var columnWord string
for i := 3; i < len(lineWords); i++ {
columnWord += lineWords[i] + " "
}
col += columnWord + "\n"
}
}
}
return
}

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

-package LogParser
+package main
import "strings"
func ExtractColumn(logContents string, column uint8) (col string) {
var logLines []string = strings.Split(logContents, "\n")
if column > 2 {
col = ""
return
}
for _, value := range logLines {
var lineWords []string = strings.Split(value, " ")
switch column {
case 0:
col += lineWords[0] + " " + lineWords[1] + "\n"
case 1:
col += lineWords[2] + "\n"
case 2:
{
var columnWord string
for i := 3; i < len(lineWords); i++ {
columnWord += lineWords[i] + " "
}
col += columnWord + "\n"
}
}
}
return
}