Решение на Разлика в сумите от Марина Попова

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

Към профила на Марина Попова

Резултати

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

Код

package main
import (
"bufio"
"fmt"
"log"
"strings"
)
func ExtractColumn(logContents string, column uint8) {
scanner := bufio.NewScanner(strings.NewReader(logContents))
for scanner.Scan() {
fmt.Println(parseSplit(scanner.Text(), column))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func parseSplit(s string, column uint8) (t string, ip, msg string) {
parts := strings.SplitN(s, " ", 4)
if column == 0 {
t = strings.Join(parts[0:2], " ")
ip, msg = "", ""
}
if column == 1 {
t = ""
ip, msg = parts[2], ""
}
if column == 2 {
t = ""
ip, msg = "", parts[3]
}
return t, ip, msg
}
func main() {
logCon := `2015-08-23 12:37:03 8.8.8.8 As far as we can tell this is a DNS
2015-08-23 12:37:04 8.8.4.4 Yet another DNS, how quaint!
2015-08-23 12:37:05 208.122.23.23 There is definitely some trend here
`
ExtractColumn(logCon, 0)
//ExtractColumn(logCon, 1)
//ExtractColumn(logCon, 2)
}

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

# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]
# _/tmp/d20151103-24541-1e87l7x
./solution_test.go:257: ExtractColumn(logContents, column) used as value
FAIL	_/tmp/d20151103-24541-1e87l7x [build failed]

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

Марина обнови решението на 25.10.2015 14:34 (преди над 2 години)

+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+ "strings"
+)
+
+func ExtractColumn(filename string, column uint8) {
+ file, err := os.Open(filename)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer file.Close()
+
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ fmt.Println(parseSplit(scanner.Text(), column))
+ }
+
+ if err := scanner.Err(); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func parseSplit(s string, column uint8) (t string, ip, msg string) {
+ parts := strings.SplitN(s, " ", 4)
+
+ if column == 0 {
+ t = strings.Join(parts[0:2], " ")
+ ip, msg = "", ""
+ }
+
+ if column == 1 {
+ t = ""
+ ip, msg = parts[2], ""
+ }
+
+ if column == 2 {
+ t = ""
+ ip, msg = "", parts[3]
+ }
+
+ return t, ip, msg
+}
+
+func main() {
+ //the log file with the content
+ filename := "log.txt"
+ ExtractColumn(filename, 0)
+ //ExtractColumn(filename, 1)
+ //ExtractColumn(filename, 2)
+}

Прочети условието отново - първия аргумент на ExractColumn е съдържанието на лог, не името на файл в който има лог.

Препоръчвам да си пускаш тестовете котио сме дали вместо да ползваш main за тестване. Може да ползваш И тестовете на Таня.

Марина обнови решението на 26.10.2015 17:26 (преди над 2 години)

package main
import (
- "bufio"
- "fmt"
- "log"
- "os"
- "strings"
+ "bufio"
+ "fmt"
+ "log"
+ "strings"
)
-func ExtractColumn(filename string, column uint8) {
- file, err := os.Open(filename)
- if err != nil {
- log.Fatal(err)
- }
- defer file.Close()
+func ExtractColumn(logContents string, column uint8) {
+ scanner := bufio.NewScanner(strings.NewReader(logContents))
+ for scanner.Scan() {
+ fmt.Println(parseSplit(scanner.Text(), column))
+ }
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- fmt.Println(parseSplit(scanner.Text(), column))
- }
-
- if err := scanner.Err(); err != nil {
- log.Fatal(err)
- }
+ if err := scanner.Err(); err != nil {
+ log.Fatal(err)
+ }
}
func parseSplit(s string, column uint8) (t string, ip, msg string) {
- parts := strings.SplitN(s, " ", 4)
+ parts := strings.SplitN(s, " ", 4)
- if column == 0 {
- t = strings.Join(parts[0:2], " ")
- ip, msg = "", ""
- }
+ if column == 0 {
+ t = strings.Join(parts[0:2], " ")
+ ip, msg = "", ""
+ }
- if column == 1 {
- t = ""
- ip, msg = parts[2], ""
- }
+ if column == 1 {
+ t = ""
+ ip, msg = parts[2], ""
+ }
- if column == 2 {
- t = ""
- ip, msg = "", parts[3]
- }
+ if column == 2 {
+ t = ""
+ ip, msg = "", parts[3]
+ }
- return t, ip, msg
+ return t, ip, msg
}
func main() {
- //the log file with the content
- filename := "log.txt"
+ logCon := `2015-08-23 12:37:03 8.8.8.8 As far as we can tell this is a DNS
- ExtractColumn(filename, 0)
+2015-08-23 12:37:04 8.8.4.4 Yet another DNS, how quaint!
- //ExtractColumn(filename, 1)
+2015-08-23 12:37:05 208.122.23.23 There is definitely some trend here
- //ExtractColumn(filename, 2)
+`
-}
+ ExtractColumn(logCon, 0)
+ //ExtractColumn(logCon, 1)
+ //ExtractColumn(logCon, 2)
+}